-
Notifications
You must be signed in to change notification settings - Fork 107
fix: umd should be import as cjs #1642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 }) | ||
| } | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 为公共函数添加文档注释
|
||
|
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 () { | ||
stormslowly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 1 | ||
| })) | ||
This file contains hidden or 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) | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
为公共结构体添加文档注释
AmdDefineOverrides结构体是公共的,建议添加文档注释,以提高代码的可读性和维护性。