mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
port check_if_should_use_esm_loader to rust (#12562)
This commit is contained in:
parent
1b684d333d
commit
b92019a847
7 changed files with 42 additions and 32 deletions
|
@ -926,6 +926,20 @@ struct PackageConfig {
|
|||
typ: String,
|
||||
}
|
||||
|
||||
pub fn check_if_should_use_esm_loader(
|
||||
main_module: &ModuleSpecifier,
|
||||
) -> Result<bool, AnyError> {
|
||||
let s = main_module.as_str();
|
||||
if s.ends_with(".mjs") {
|
||||
return Ok(true);
|
||||
}
|
||||
if s.ends_with(".cjs") {
|
||||
return Ok(false);
|
||||
}
|
||||
let package_config = get_package_scope_config(main_module)?;
|
||||
Ok(package_config.typ == "module")
|
||||
}
|
||||
|
||||
fn get_package_config(
|
||||
path: PathBuf,
|
||||
specifier: &str,
|
||||
|
@ -1219,4 +1233,18 @@ mod tests {
|
|||
assert!(is_relative_specifier("./foo.js"));
|
||||
assert!(!is_relative_specifier("https://deno.land/std/node/http.ts"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_if_should_use_esm_loader() {
|
||||
let basic = testdir("basic");
|
||||
let main = Url::from_file_path(basic.join("main.js")).unwrap();
|
||||
assert!(check_if_should_use_esm_loader(&main).unwrap());
|
||||
|
||||
let cjs = Url::from_file_path(basic.join("main.cjs")).unwrap();
|
||||
assert!(!check_if_should_use_esm_loader(&cjs).unwrap());
|
||||
|
||||
let not_esm = testdir("not_esm");
|
||||
let main = Url::from_file_path(not_esm.join("main.js")).unwrap();
|
||||
assert!(!check_if_should_use_esm_loader(&main).unwrap());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use deno_core::located_script_name;
|
|||
use deno_core::url::Url;
|
||||
use deno_core::JsRuntime;
|
||||
|
||||
pub use esm_resolver::check_if_should_use_esm_loader;
|
||||
pub(crate) use esm_resolver::NodeEsmResolver;
|
||||
|
||||
// TODO(bartlomieju): this needs to be bumped manually for
|
||||
|
@ -86,33 +87,6 @@ fn try_resolve_builtin_module(specifier: &str) -> Option<Url> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn check_if_should_use_esm_loader(
|
||||
js_runtime: &mut JsRuntime,
|
||||
main_module: &str,
|
||||
) -> Result<bool, AnyError> {
|
||||
// Decide if we're running with Node ESM loader or CJS loader.
|
||||
let source_code = &format!(
|
||||
r#"(async function checkIfEsm(main) {{
|
||||
const {{ resolveMainPath, shouldUseESMLoader }} = await import("{}");
|
||||
const resolvedMain = resolveMainPath(main);
|
||||
const useESMLoader = shouldUseESMLoader(resolvedMain);
|
||||
return useESMLoader;
|
||||
}})('{}');"#,
|
||||
MODULE_URL_STR.as_str(),
|
||||
escape_for_single_quote_string(main_module),
|
||||
);
|
||||
let result =
|
||||
js_runtime.execute_script(&located_script_name!(), source_code)?;
|
||||
let use_esm_loader_global = js_runtime.resolve_value(result).await?;
|
||||
let use_esm_loader = {
|
||||
let scope = &mut js_runtime.handle_scope();
|
||||
let use_esm_loader_local = use_esm_loader_global.open(scope);
|
||||
use_esm_loader_local.boolean_value(scope)
|
||||
};
|
||||
|
||||
Ok(use_esm_loader)
|
||||
}
|
||||
|
||||
pub(crate) fn load_cjs_module(
|
||||
js_runtime: &mut JsRuntime,
|
||||
main_module: &str,
|
||||
|
|
1
cli/compat/testdata/not_esm/main.js
vendored
Normal file
1
cli/compat/testdata/not_esm/main.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
require("foo");
|
0
cli/compat/testdata/not_esm/node_modules/foo/index.js
generated
vendored
Normal file
0
cli/compat/testdata/not_esm/node_modules/foo/index.js
generated
vendored
Normal file
5
cli/compat/testdata/not_esm/node_modules/foo/package.json
generated
vendored
Normal file
5
cli/compat/testdata/not_esm/node_modules/foo/package.json
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "foo",
|
||||
"type": "module",
|
||||
"exports": "./index.js"
|
||||
}
|
6
cli/compat/testdata/not_esm/package.json
vendored
Normal file
6
cli/compat/testdata/not_esm/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "bar",
|
||||
"dependencies": {
|
||||
"foo": "1.0.0"
|
||||
}
|
||||
}
|
|
@ -1153,11 +1153,7 @@ async fn run_command(
|
|||
// this file.
|
||||
worker.execute_side_module(&compat::MODULE_URL).await?;
|
||||
|
||||
let use_esm_loader = compat::check_if_should_use_esm_loader(
|
||||
&mut worker.js_runtime,
|
||||
&main_module.to_file_path().unwrap().display().to_string(),
|
||||
)
|
||||
.await?;
|
||||
let use_esm_loader = compat::check_if_should_use_esm_loader(&main_module)?;
|
||||
|
||||
if use_esm_loader {
|
||||
// ES module execution in Node compatiblity mode
|
||||
|
|
Loading…
Reference in a new issue