summaryrefslogtreecommitdiff
path: root/cli/node/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/node/mod.rs')
-rw-r--r--cli/node/mod.rs51
1 files changed, 4 insertions, 47 deletions
diff --git a/cli/node/mod.rs b/cli/node/mod.rs
index 81a2f10e7..75fdfcd7d 100644
--- a/cli/node/mod.rs
+++ b/cli/node/mod.rs
@@ -255,11 +255,6 @@ static NODE_COMPAT_URL: Lazy<Url> = Lazy::new(|| {
pub static MODULE_ALL_URL: Lazy<Url> =
Lazy::new(|| NODE_COMPAT_URL.join("node/module_all.ts").unwrap());
-/// Suffix used for the CJS to ESM translation for Node code
-pub const CJS_TO_ESM_NODE_SUFFIX: &str = ".node_cjs_to_esm.mjs";
-/// Suffix used for the CJS to ESM translation for Deno code
-pub const CJS_TO_ESM_DENO_SUFFIX: &str = ".deno_cjs_to_esm.mjs";
-
fn find_builtin_node_module(specifier: &str) -> Option<&NodeModulePolyfill> {
SUPPORTED_MODULES.iter().find(|m| m.name == specifier)
}
@@ -417,15 +412,7 @@ pub fn node_resolve(
None => return Ok(None),
};
- let resolve_response = match url_to_node_resolution(url, npm_resolver)? {
- NodeResolution::CommonJs(mut url) => {
- // Use a suffix to say this cjs specifier should be translated from
- // cjs to esm for consumption by Node ESM code
- url.set_path(&format!("{}{}", url.path(), CJS_TO_ESM_NODE_SUFFIX));
- NodeResolution::CommonJs(url)
- }
- val => val,
- };
+ let resolve_response = url_to_node_resolution(url, npm_resolver)?;
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
// "preserveSymlinksMain"/"preserveSymlinks" options.
Ok(Some(resolve_response))
@@ -453,15 +440,7 @@ pub fn node_resolve_npm_reference(
})?;
let url = ModuleSpecifier::from_file_path(resolved_path).unwrap();
- let resolve_response = match url_to_node_resolution(url, npm_resolver)? {
- NodeResolution::CommonJs(mut url) => {
- // Use a suffix to say this cjs specifier should be translated from
- // cjs to esm for consumption by Deno ESM code
- url.set_path(&format!("{}{}", url.path(), CJS_TO_ESM_DENO_SUFFIX));
- NodeResolution::CommonJs(url)
- }
- val => val,
- };
+ let resolve_response = url_to_node_resolution(url, npm_resolver)?;
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
// "preserveSymlinksMain"/"preserveSymlinks" options.
Ok(Some(resolve_response))
@@ -722,12 +701,6 @@ fn add_export(
}
}
-#[derive(Clone, Copy, PartialEq, Eq)]
-pub enum CjsToEsmTranslateKind {
- Node,
- Deno,
-}
-
/// Translates given CJS module into ESM. This function will perform static
/// analysis on the file to find defined exports and reexports.
///
@@ -740,7 +713,6 @@ pub fn translate_cjs_to_esm(
code: String,
media_type: MediaType,
npm_resolver: &GlobalNpmPackageResolver,
- translate_kind: CjsToEsmTranslateKind,
) -> Result<String, AnyError> {
fn perform_cjs_analysis(
specifier: &str,
@@ -767,11 +739,6 @@ pub fn translate_cjs_to_esm(
let analysis = perform_cjs_analysis(specifier.as_str(), media_type, code)?;
- let root_exports = analysis
- .exports
- .iter()
- .map(|s| s.as_str())
- .collect::<HashSet<_>>();
let mut all_exports = analysis
.exports
.iter()
@@ -837,16 +804,8 @@ pub fn translate_cjs_to_esm(
.replace('\"', "\\\"")
));
- let mut had_default = false;
for export in &all_exports {
- if export.as_str() == "default" {
- if translate_kind == CjsToEsmTranslateKind::Deno
- && root_exports.contains("__esModule")
- {
- source.push(format!("export default mod[\"{}\"];", export));
- had_default = true;
- }
- } else {
+ if export.as_str() != "default" {
add_export(
&mut source,
export,
@@ -856,9 +815,7 @@ pub fn translate_cjs_to_esm(
}
}
- if !had_default {
- source.push("export default mod;".to_string());
- }
+ source.push("export default mod;".to_string());
let translated_source = source.join("\n");
Ok(translated_source)