-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: umd should be import as cjs (#1642)
* fix: umd should be import as cjs * refactor: 🎨 just short circuit typeof define to prevent define takes effect * revert: ⏪ binding.d.ts --------- Co-authored-by: pshu <[email protected]>
- Loading branch information
1 parent
9172ca7
commit ca24e14
Showing
8 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use swc_core::common::{Mark, DUMMY_SP}; | ||
use swc_core::ecma::ast::*; | ||
use swc_core::ecma::visit::{as_folder, Fold, VisitMut}; | ||
|
||
use crate::ast::utils::is_ident_undefined; | ||
|
||
pub struct AmdDefineOverrides { | ||
unresolved_mark: Mark, | ||
} | ||
|
||
pub fn amd_define_overrides(unresolved_mark: Mark) -> impl VisitMut + Fold { | ||
as_folder(AmdDefineOverrides { unresolved_mark }) | ||
} | ||
|
||
impl VisitMut for AmdDefineOverrides { | ||
fn visit_mut_unary_expr(&mut self, node: &mut UnaryExpr) { | ||
if node.op == UnaryOp::TypeOf | ||
&& let Some(arg_ident) = node.arg.as_ident() | ||
&& is_ident_undefined(arg_ident, "define", &self.unresolved_mark) | ||
{ | ||
node.arg = Expr::undefined(DUMMY_SP) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use swc_core::common::GLOBALS; | ||
use swc_core::ecma::visit::VisitMutWith; | ||
|
||
use super::*; | ||
use crate::ast::tests::TestUtils; | ||
|
||
#[test] | ||
fn unresolve_typeof_define_change_to_undefined() { | ||
let mut tu = TestUtils::gen_js_ast( | ||
r#"if(typeof define ==="function" && define.amd) { | ||
console.log("amd") | ||
}"#, | ||
); | ||
let js = tu.ast.js_mut(); | ||
let unresolved_mark = js.unresolved_mark; | ||
GLOBALS.set(&tu.context.meta.script.globals, || { | ||
js.ast | ||
.visit_mut_with(&mut amd_define_overrides(unresolved_mark)); | ||
}); | ||
|
||
let code = tu.js_ast_to_code(); | ||
|
||
assert_eq!( | ||
code, | ||
r#"if (typeof void 0 === "function" && define.amd) { | ||
console.log("amd"); | ||
}"# | ||
) | ||
} | ||
|
||
#[test] | ||
fn id_define_is_declared() { | ||
let mut tu = TestUtils::gen_js_ast( | ||
r#"let define = 1; if(typeof define ==="number") { | ||
console.log("number") | ||
}"#, | ||
); | ||
let js = tu.ast.js_mut(); | ||
let unresolved_mark = js.unresolved_mark; | ||
GLOBALS.set(&tu.context.meta.script.globals, || { | ||
js.ast | ||
.visit_mut_with(&mut amd_define_overrides(unresolved_mark)); | ||
}); | ||
|
||
let code = tu.js_ast_to_code(); | ||
|
||
assert_eq!( | ||
code, | ||
r#"let define = 1; | ||
if (typeof define === "number") { | ||
console.log("number"); | ||
}"# | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const {parseBuildResult, injectSimpleJest} = require("../../../scripts/test-utils"); | ||
const {distDir} = parseBuildResult(__dirname); | ||
|
||
injectSimpleJest() | ||
require('./dist/index.js'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-nocheck | ||
(function (root, definition) { | ||
"use strict"; | ||
if (typeof define === 'function' && define.amd) { | ||
define(definition); | ||
} else if (typeof module === 'object' && module.exports) { | ||
module.exports = definition(); | ||
} else { | ||
root.log = definition(); | ||
} | ||
}(this, function () { | ||
return 1 | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-ignore | ||
import a from './a' | ||
|
||
it("amd/umd should be exports as commonjs", () => { | ||
expect(a).toEqual(1) | ||
}); |