-
Notifications
You must be signed in to change notification settings - Fork 81
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
feat: legacy interop #1638
base: master
Are you sure you want to change the base?
feat: legacy interop #1638
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,8 @@ impl MakoRuntime { | |
let helpers = SwcHelpers::full_helpers() | ||
.into_iter() | ||
.map(|source| { | ||
let code = Self::get_swc_helper_code(&source).unwrap(); | ||
let code = | ||
Self::get_swc_helper_code(&source, context.config.legacy_interop).unwrap(); | ||
let module_id: ModuleId = source.into(); | ||
let module_id = module_id.generate(context); | ||
format!("\"{}\": {}", module_id, code) | ||
|
@@ -65,7 +66,22 @@ impl MakoRuntime { | |
)) | ||
} | ||
|
||
fn get_swc_helper_code(path: &str) -> Result<String> { | ||
fn modify_legacy_code(origin_code: &str, legacy_interop: bool) -> String { | ||
let mut new_code = origin_code.to_string(); | ||
if legacy_interop { | ||
new_code = new_code.replace( | ||
"// ### legacy_interop ###", | ||
r#" | ||
if (typeof obj === "function") { | ||
return obj | ||
}; | ||
"#, | ||
); | ||
} | ||
new_code | ||
} | ||
|
||
fn get_swc_helper_code(path: &str, legacy_interop: bool) -> Result<String> { | ||
let code = match path { | ||
"@swc/helpers/_/_interop_require_default" => r#" | ||
function(module, exports, __mako_require__) { | ||
|
@@ -93,7 +109,8 @@ function(module, exports, __mako_require__) { | |
} | ||
} | ||
"#.trim(), | ||
"@swc/helpers/_/_interop_require_wildcard" => r#" | ||
"@swc/helpers/_/_interop_require_wildcard" => { | ||
let origin = r#" | ||
function(module, exports, __mako_require__) { | ||
__mako_require__.d(exports, "__esModule", { | ||
value: true | ||
|
@@ -125,6 +142,7 @@ function(module, exports, __mako_require__) { | |
if (obj === null || typeof obj !== "object" && typeof obj !== "function") return { | ||
default: obj | ||
}; | ||
// ### legacy_interop ### | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) return cache.get(obj); | ||
var newObj = {}; | ||
|
@@ -139,7 +157,9 @@ function(module, exports, __mako_require__) { | |
return newObj; | ||
} | ||
} | ||
"#.trim(), | ||
"#.trim(); | ||
&Self::modify_legacy_code(origin, legacy_interop) | ||
}, | ||
Comment on lines
+161
to
+162
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. 小心字符串替换的可靠性 在第 161-162 行中,使用了 |
||
"@swc/helpers/_/_export_star" => r#" | ||
function(module, exports, __mako_require__) { | ||
__mako_require__.d(exports, "__esModule", { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const { injectSimpleJest } = require("../../../scripts/test-utils"); | ||
|
||
injectSimpleJest() | ||
|
||
require("./dist/index.js"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"legacyInterop": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @ts-ignore | ||
import * as a from './a' | ||
|
||
|
||
|
||
|
||
it('should work when config legacyInterop',()=>{ | ||
expect(a()).toBe(1) | ||
}); | ||
Comment on lines
+7
to
+9
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 建议改进测试用例的实现和文档 当前的测试用例虽然功能正确,但可以通过以下方式进行改进:
建议修改如下: it('应该在启用 legacyInterop 配置时正确导入和使用模块 a', () => {
// 验证模块 a 被正确导入并按预期工作
expect(a()).toBe(1);
// 可以添加更多断言来测试其他方面的行为
// 例如:expect(typeof a).toBe('function');
}); |
||
|
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.
避免使用
unwrap()
以防止潜在的崩溃在第 49-50 行中,调用了
get_swc_helper_code
方法并直接使用了unwrap()
。如果该方法返回Err
,程序将会发生崩溃。建议使用?
运算符或匹配处理错误,以提高代码的稳健性。