diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-09-05 08:59:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-05 07:59:12 +0000 |
commit | dfc5eec43c481b1eeaa0ad069aeba8b7559d4440 (patch) | |
tree | 84e3bc63388a90163ccd4008165bd09761be12a7 /cli/resolver.rs | |
parent | 4554ab6aefb158f2d0a8a5ae5bb3d748e123fb00 (diff) |
feat: Allow importing .cjs files (#25426)
This commit adds support for executing top-level `.cjs` files,
as well as import `.cjs` files from within npm packages.
This works only for `.cjs` files, the contents of sibling `package.json`
are not consulted for the `"type"` field.
Closes https://github.com/denoland/deno/issues/25384
---------
Signed-off-by: David Sherret <dsherret@users.noreply.github.com>
Co-authored-by: Luca Casonato <hello@lcas.dev>
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
Diffstat (limited to 'cli/resolver.rs')
-rw-r--r-- | cli/resolver.rs | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/cli/resolver.rs b/cli/resolver.rs index 987e23ee1..5b657b895 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -327,7 +327,9 @@ impl NpmModuleLoader { specifier: &ModuleSpecifier, maybe_referrer: Option<&ModuleSpecifier>, ) -> Option<Result<ModuleCodeStringSource, AnyError>> { - if self.node_resolver.in_npm_package(specifier) { + if self.node_resolver.in_npm_package(specifier) + || (specifier.scheme() == "file" && specifier.path().ends_with(".cjs")) + { Some(self.load(specifier, maybe_referrer).await) } else { None @@ -376,7 +378,9 @@ impl NpmModuleLoader { } })?; - let code = if self.cjs_resolutions.contains(specifier) { + let code = if self.cjs_resolutions.contains(specifier) + || (specifier.scheme() == "file" && specifier.path().ends_with(".cjs")) + { // translate cjs to esm if it's cjs and inject node globals let code = match String::from_utf8_lossy(&code) { Cow::Owned(code) => code, |