mirror of
https://github.com/denoland/deno.git
synced 2025-03-09 13:49:37 -04:00
fix(npm): remove export binding to match node (#15837)
This commit is contained in:
parent
008b9afc00
commit
e3090f11f3
9 changed files with 42 additions and 21 deletions
|
@ -814,20 +814,14 @@ pub fn translate_cjs_to_esm(
|
||||||
for export in &all_exports {
|
for export in &all_exports {
|
||||||
if export.as_str() == "default" {
|
if export.as_str() == "default" {
|
||||||
if root_exports.contains("__esModule") {
|
if root_exports.contains("__esModule") {
|
||||||
source.push(format!(
|
source.push(format!("export default mod[\"{}\"];", export));
|
||||||
"export default Deno[Deno.internal].require.bindExport(mod[\"{}\"], mod);",
|
|
||||||
export,
|
|
||||||
));
|
|
||||||
had_default = true;
|
had_default = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
add_export(
|
add_export(
|
||||||
&mut source,
|
&mut source,
|
||||||
export,
|
export,
|
||||||
&format!(
|
&format!("mod[\"{}\"]", export),
|
||||||
"Deno[Deno.internal].require.bindExport(mod[\"{}\"], mod)",
|
|
||||||
export
|
|
||||||
),
|
|
||||||
&mut temp_var_count,
|
&mut temp_var_count,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,14 @@ itest!(cjs_reexport_collision {
|
||||||
http_server: true,
|
http_server: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
itest!(cjs_this_in_exports {
|
||||||
|
args: "run --allow-read --unstable --quiet npm/cjs_this_in_exports/main.js",
|
||||||
|
output: "npm/cjs_this_in_exports/main.out",
|
||||||
|
envs: env_vars(),
|
||||||
|
http_server: true,
|
||||||
|
exit_code: 1,
|
||||||
|
});
|
||||||
|
|
||||||
itest!(translate_cjs_to_esm {
|
itest!(translate_cjs_to_esm {
|
||||||
args: "run --unstable -A --quiet npm/translate_cjs_to_esm/main.js",
|
args: "run --unstable -A --quiet npm/translate_cjs_to_esm/main.js",
|
||||||
output: "npm/translate_cjs_to_esm/main.out",
|
output: "npm/translate_cjs_to_esm/main.out",
|
||||||
|
@ -347,6 +355,7 @@ fn env_vars_no_sync_download() -> Vec<(String, String)> {
|
||||||
"DENO_NPM_REGISTRY".to_string(),
|
"DENO_NPM_REGISTRY".to_string(),
|
||||||
"http://localhost:4545/npm/registry/".to_string(),
|
"http://localhost:4545/npm/registry/".to_string(),
|
||||||
),
|
),
|
||||||
|
("NO_COLOR".to_string(), "1".to_string()),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
cli/tests/testdata/npm/cjs_this_in_exports/main.js
vendored
Normal file
11
cli/tests/testdata/npm/cjs_this_in_exports/main.js
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import defaultImport, { getValue } from "npm:@denotest/cjs-this-in-exports";
|
||||||
|
import * as namespaceImport from "npm:@denotest/cjs-this-in-exports";
|
||||||
|
|
||||||
|
console.log(defaultImport.getValue());
|
||||||
|
// In Node this actually fails, but it seems to work in Deno
|
||||||
|
// so I guess there's no harm in that.
|
||||||
|
console.log(namespaceImport.getValue());
|
||||||
|
|
||||||
|
// This will throw because it's lost its context.
|
||||||
|
// (same thing occurs with Node's cjs -> esm translation)
|
||||||
|
getValue();
|
5
cli/tests/testdata/npm/cjs_this_in_exports/main.out
vendored
Normal file
5
cli/tests/testdata/npm/cjs_this_in_exports/main.out
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
1
|
||||||
|
1
|
||||||
|
error: Uncaught TypeError: this.otherMethod is not a function
|
||||||
|
at getValue (file://[WILDCARD]/@denotest/cjs-this-in-exports/1.0.0/index.js:3:17)
|
||||||
|
at file://[WILDCARD]/testdata/npm/cjs_this_in_exports/main.js:11:1
|
8
cli/tests/testdata/npm/registry/@denotest/cjs-this-in-exports/1.0.0/index.js
vendored
Normal file
8
cli/tests/testdata/npm/registry/@denotest/cjs-this-in-exports/1.0.0/index.js
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
module.exports = {
|
||||||
|
getValue() {
|
||||||
|
return this.otherMethod();
|
||||||
|
},
|
||||||
|
otherMethod() {
|
||||||
|
return 1;
|
||||||
|
},
|
||||||
|
};
|
5
cli/tests/testdata/npm/registry/@denotest/cjs-this-in-exports/1.0.0/package.json
vendored
Normal file
5
cli/tests/testdata/npm/registry/@denotest/cjs-this-in-exports/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"name": "@denotest/cjs-this-in-exports",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "./index.js"
|
||||||
|
}
|
|
@ -1 +1 @@
|
||||||
[Function: bound Client]
|
[Function: Client]
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
[Function: access]
|
[Function: access]
|
||||||
[Function: bound createApp]
|
[Function: createApp]
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
ArrayPrototypeSlice,
|
ArrayPrototypeSlice,
|
||||||
ArrayPrototypeSplice,
|
ArrayPrototypeSplice,
|
||||||
FunctionPrototypeBind,
|
|
||||||
ObjectGetOwnPropertyDescriptor,
|
ObjectGetOwnPropertyDescriptor,
|
||||||
ObjectGetPrototypeOf,
|
ObjectGetPrototypeOf,
|
||||||
ObjectPrototypeHasOwnProperty,
|
ObjectPrototypeHasOwnProperty,
|
||||||
|
@ -864,15 +863,6 @@
|
||||||
throw new Error("not implemented");
|
throw new Error("not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
function bindExport(value, mod) {
|
|
||||||
// ensure exported functions are bound to their module object
|
|
||||||
if (typeof value === "function") {
|
|
||||||
return FunctionPrototypeBind(value, mod);
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @param specifier {string} */
|
/** @param specifier {string} */
|
||||||
function packageSpecifierSubPath(specifier) {
|
function packageSpecifierSubPath(specifier) {
|
||||||
let parts = specifier.split("/");
|
let parts = specifier.split("/");
|
||||||
|
@ -892,7 +882,6 @@
|
||||||
toRealPath,
|
toRealPath,
|
||||||
cjsParseCache,
|
cjsParseCache,
|
||||||
readPackageScope,
|
readPackageScope,
|
||||||
bindExport,
|
|
||||||
moduleExports: m,
|
moduleExports: m,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue