summaryrefslogtreecommitdiff
path: root/cli/standalone/binary.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-11-01 12:27:00 -0400
committerGitHub <noreply@github.com>2024-11-01 12:27:00 -0400
commit826e42a5b5880c974ae019a7a21aade6a718062c (patch)
treea46502ecc3c73e4f7fc3a4517d83c7b2f3d0c0d3 /cli/standalone/binary.rs
parent4774eab64d5176e997b6431f03f075782321b3d9 (diff)
fix: improved support for cjs and cts modules (#26558)
* cts support * better cjs/cts type checking * deno compile cjs/cts support * More efficient detect cjs (going towards stabilization) * Determination of whether .js, .ts, .jsx, or .tsx is cjs or esm is only done after loading * Support `import x = require(...);` Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/standalone/binary.rs')
-rw-r--r--cli/standalone/binary.rs38
1 files changed, 25 insertions, 13 deletions
diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs
index f41f3003f..9e2651226 100644
--- a/cli/standalone/binary.rs
+++ b/cli/standalone/binary.rs
@@ -21,6 +21,7 @@ use std::process::Command;
use std::sync::Arc;
use deno_ast::MediaType;
+use deno_ast::ModuleKind;
use deno_ast::ModuleSpecifier;
use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::ResolverWorkspaceJsrPackage;
@@ -67,6 +68,7 @@ use crate::file_fetcher::FileFetcher;
use crate::http_util::HttpClientProvider;
use crate::npm::CliNpmResolver;
use crate::npm::InnerCliNpmResolverRef;
+use crate::resolver::CjsTracker;
use crate::shared::ReleaseChannel;
use crate::standalone::virtual_fs::VfsEntry;
use crate::util::archive;
@@ -257,6 +259,10 @@ impl StandaloneModules {
}
}
+ pub fn has_file(&self, path: &Path) -> bool {
+ self.vfs.file_entry(path).is_ok()
+ }
+
pub fn read<'a>(
&'a self,
specifier: &'a ModuleSpecifier,
@@ -353,6 +359,7 @@ pub fn extract_standalone(
}
pub struct DenoCompileBinaryWriter<'a> {
+ cjs_tracker: &'a CjsTracker,
deno_dir: &'a DenoDir,
emitter: &'a Emitter,
file_fetcher: &'a FileFetcher,
@@ -365,6 +372,7 @@ pub struct DenoCompileBinaryWriter<'a> {
impl<'a> DenoCompileBinaryWriter<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
+ cjs_tracker: &'a CjsTracker,
deno_dir: &'a DenoDir,
emitter: &'a Emitter,
file_fetcher: &'a FileFetcher,
@@ -374,6 +382,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
npm_system_info: NpmSystemInfo,
) -> Self {
Self {
+ cjs_tracker,
deno_dir,
emitter,
file_fetcher,
@@ -599,19 +608,21 @@ impl<'a> DenoCompileBinaryWriter<'a> {
}
let (maybe_source, media_type) = match module {
deno_graph::Module::Js(m) => {
- // todo(https://github.com/denoland/deno_media_type/pull/12): use is_emittable()
- let is_emittable = matches!(
- m.media_type,
- MediaType::TypeScript
- | MediaType::Mts
- | MediaType::Cts
- | MediaType::Jsx
- | MediaType::Tsx
- );
- let source = if is_emittable {
+ let source = if m.media_type.is_emittable() {
+ let is_cjs = self.cjs_tracker.is_cjs_with_known_is_script(
+ &m.specifier,
+ m.media_type,
+ m.is_script,
+ )?;
+ let module_kind = ModuleKind::from_is_cjs(is_cjs);
let source = self
.emitter
- .emit_parsed_source(&m.specifier, m.media_type, &m.source)
+ .emit_parsed_source(
+ &m.specifier,
+ m.media_type,
+ module_kind,
+ &m.source,
+ )
.await?;
source.into_bytes()
} else {
@@ -745,8 +756,9 @@ impl<'a> DenoCompileBinaryWriter<'a> {
} else {
// DO NOT include the user's registry url as it may contain credentials,
// but also don't make this dependent on the registry url
- let global_cache_root_path = npm_resolver.global_cache_root_folder();
- let mut builder = VfsBuilder::new(global_cache_root_path)?;
+ let global_cache_root_path = npm_resolver.global_cache_root_path();
+ let mut builder =
+ VfsBuilder::new(global_cache_root_path.to_path_buf())?;
let mut packages =
npm_resolver.all_system_packages(&self.npm_system_info);
packages.sort_by(|a, b| a.id.cmp(&b.id)); // determinism