fix: compatible with code has import.meta (#6264)

* fix: changelog

* fix: test case
This commit is contained in:
ClarkXia 2023-05-24 13:51:26 +08:00 committed by GitHub
parent 390a0ce211
commit a4b851449a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'@ice/webpack-config': patch
---
fix: compatible with code has import.meta

View File

@ -12,7 +12,8 @@ const transformImport = async (source: string, coreJsPath: string) => {
const str = () => s || (s = new MagicString(source));
const isESM = exports.length > 0 || imports.some((targetImport) => {
const importString = targetImport.n;
return !importString.includes('core-js') && !importString.includes('@swc/helpers');
// `targetImport.n` get undefined when code has `import.meta.*`.
return importString && !importString.includes('core-js') && !importString.includes('@swc/helpers');
});
imports.forEach((targetImport) => {
if (!targetImport.n) {

View File

@ -0,0 +1 @@
if (import.meta.rerender === 'client') console.log(true);

View File

@ -47,4 +47,9 @@ describe('transform core js path', () => {
expect(await transformImport(orignalCode, coreJsPath))
.toBe('var _object_spread = require(\'@swc/helpers/cjs/_object_spread.cjs\')._;module.exports = {};');
});
it('with import.meta', async () => {
const orignalCode = fs.readFileSync(path.join(__dirname, './fixtures/transformImport/importMeta.js'), 'utf-8');
expect(await transformImport(orignalCode, coreJsPath))
.toBe('if (import.meta.rerender === \'client\') console.log(true);');
});
});