Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-Runtime Monorepos / Importing Non-Deno Files #21408

Closed
bcheidemann opened this issue Nov 30, 2023 · 2 comments
Closed

Multi-Runtime Monorepos / Importing Non-Deno Files #21408

bcheidemann opened this issue Nov 30, 2023 · 2 comments

Comments

@bcheidemann
Copy link
Contributor

bcheidemann commented Nov 30, 2023

Background

One of Deno's greatest strengths is the feeling of "it just works" that you get when using it. This is largely because it makes build tools like Webpack redundant. This makes Deno an attractive option for a wide variety of projects. Recently, however, I found myself wanting to add a Deno app to an existing monorepo. My experience has not been as painless as I had hoped.

Problem statement

Deno is hard to integrate into existing monorepos. This is in large part down to the way imports are resolved and the requirement for ".ts" extensions. This repo is a minimal demonstration of the issues one might face when trying to integrate Deno into an existing Monorepo. It also demonstrates how painless this is to set up with Bun.

The main issue here is that existing libraries in monorepos do not adhere to the same import rules as Deno (i.e. requiring ".ts" extensions). It's possible to work around this using the "allowImportingTsExtensions" rule and tediously going through every single import in every singe library and adding a ".ts" extension. However, this means that tsc can no longer be used to transpile the library, because "allowImportingTsExtensions" requires the "noEmit" flag. It also won't necessarily play well with other non-tsc build solutions.

Why is this important?

  1. If Deno doesn't play nicely in existing project, this will limit future growth
  2. Deno is currently unable to compete with Bun on this front - which "just works" as expected

Proposed Solution

In an ideal world this use case would "just work" without any consideration from the developer. This would require:

  1. Deno to recognise when a ".ts" file is imported from a non-deno context
  2. Deno to respect the "tsconfig.json" for non-Deno files

Off the top of my head, there's a couple different ways Deno could automagically recognise when a file is from a non-deno context:

  1. Lack of deno.json - the issue here is that deno.json is not required in a Deno project
  2. Presence of a tsconfig.json file - likewise tsconfig.json files could be included in Deno projects
  3. Presence of a package.json file - same issue as point 2

Since automagically recognising when a file is a non-Deno file may not be possible/desirable for the above reasons, an alternative might be for developers to be able to indicate in some way when they are importing a non-Deno file.

The obvious solution would be to add a new URL scheme for this. There's already precedence for this with the npm: scheme. A node: URL scheme could therefore be introduced which would function similarly to the npm: scheme but for files from arbitrary URLs (not only packages published to NPM). Note: a different name would need to be used as node: is already used for importing Node built-in modules.

// Import local file with Node compatibility mode
import "node:./node-file.ts";

// Import remote file (bit janky due to "double scheme")
import "node:https://raw.githubusercontent.com/user/repo/main/index.ts";

Related Issues

@bcheidemann bcheidemann changed the title Multi-Runtime Monorepos Multi-Runtime Monorepos / Importing Non-Deno Files Nov 30, 2023
@bcheidemann
Copy link
Contributor Author

bcheidemann commented Dec 17, 2023

This has been largely addressed by Deno v1.39.0 introducing the --allow-sloppy-imports flag in #21464. However, this flag is not yet supported by the Deno VS Code extension and deno check yields errors when type checking the example repo.

EDIT - 17/12/2023

As per @dsherret's comment on denoland/vscode_deno#1012, sloppy imports are actually supported by the Deno VS Code extension. The suggested config also resolves the type checking issue. However, it seems that currently only one Deno project is supported (or a single deno.json file must be shared between all projects in a monorepo).

@bcheidemann
Copy link
Contributor Author

Since #21464 was recently shipped in Deno v1.39.0, the need for a new URL scheme is somewhat diminished. This is because Deno now supports "sloppy" Node style imports behind a flag. There are still some sharp edges but these can largely be worked around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant