diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-10-25 14:39:00 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-25 14:39:00 -0400 |
commit | be97170a193e8cecc5ce03ecd3c1d0add4a06bf7 (patch) | |
tree | fab7d266e208db93dcf0870dda70f7da56ade735 /cli/factory.rs | |
parent | 093b3eee58181ec45839d0fe10b8157326a102b2 (diff) |
feat(unstable): ability to `npm install` then `deno run main.ts` (#20967)
This PR adds a new unstable "bring your own node_modules" (BYONM)
functionality currently behind a `--unstable-byonm` flag (`"unstable":
["byonm"]` in a deno.json).
This enables users to run a separate install command (ex. `npm install`,
`pnpm install`) then run `deno run main.ts` and Deno will respect the
layout of the node_modules directory as setup by the separate install
command. It also works with npm/yarn/pnpm workspaces.
For this PR, the behaviour is opted into by specifying
`--unstable-byonm`/`"unstable": ["byonm"]`, but in the future we may
make this the default behaviour as outlined in
https://github.com/denoland/deno/issues/18967#issuecomment-1761248941
This is an extremely rough initial implementation. Errors are
terrible in this and the LSP requires frequent restarts. Improvements
will be done in follow up PRs.
Diffstat (limited to 'cli/factory.rs')
-rw-r--r-- | cli/factory.rs | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/cli/factory.rs b/cli/factory.rs index 0c887b720..b5240a85a 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -32,6 +32,7 @@ use crate::node::CliCjsCodeAnalyzer; use crate::node::CliNodeCodeTranslator; use crate::npm::create_cli_npm_resolver; use crate::npm::CliNpmResolver; +use crate::npm::CliNpmResolverByonmCreateOptions; use crate::npm::CliNpmResolverCreateOptions; use crate::npm::CliNpmResolverManagedCreateOptions; use crate::npm::CliNpmResolverManagedPackageJsonInstallerOption; @@ -300,7 +301,14 @@ impl CliFactory { .npm_resolver .get_or_try_init_async(async { let fs = self.fs(); - create_cli_npm_resolver( + create_cli_npm_resolver(if self.options.unstable_byonm() { + CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions { + fs: fs.clone(), + // todo(byonm): actually resolve this properly because the package.json + // might be in an ancestor directory + root_node_modules_dir: self.options.initial_cwd().join("node_modules"), + }) + } else { CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions { snapshot: match self.options.resolve_npm_resolution_snapshot()? { Some(snapshot) => { @@ -329,7 +337,7 @@ impl CliFactory { npm_system_info: self.options.npm_system_info(), npm_registry_url: crate::args::npm_registry_default_url().to_owned(), }) - ).await + }).await }) .await } @@ -365,24 +373,25 @@ impl CliFactory { .services .resolver .get_or_try_init_async(async { - Ok(Arc::new(CliGraphResolver::new( - if self.options.no_npm() { + Ok(Arc::new(CliGraphResolver::new(CliGraphResolverOptions { + fs: self.fs().clone(), + cjs_resolutions: Some(self.cjs_resolutions().clone()), + node_resolver: Some(self.node_resolver().await?.clone()), + npm_resolver: if self.options.no_npm() { None } else { Some(self.npm_resolver().await?.clone()) }, - self.package_json_deps_provider().clone(), - CliGraphResolverOptions { - maybe_jsx_import_source_config: self - .options - .to_maybe_jsx_import_source_config()?, - maybe_import_map: self.maybe_import_map().await?.clone(), - maybe_vendor_dir: self.options.vendor_dir_path(), - bare_node_builtins_enabled: self - .options - .unstable_bare_node_builtlins(), - }, - ))) + package_json_deps_provider: self.package_json_deps_provider().clone(), + maybe_jsx_import_source_config: self + .options + .to_maybe_jsx_import_source_config()?, + maybe_import_map: self.maybe_import_map().await?.clone(), + maybe_vendor_dir: self.options.vendor_dir_path(), + bare_node_builtins_enabled: self + .options + .unstable_bare_node_builtlins(), + }))) }) .await } |