fix: transform import with special identifier (#6499)

* fix: transform import with special identifier

* fix: test case
This commit is contained in:
ClarkXia 2023-08-30 13:28:54 +08:00 committed by GitHub
parent 99c0dd26e8
commit 101eadeaa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'@ice/shared-config': patch
---
fix: transform import with special identifier

View File

@ -44,7 +44,7 @@ const transformImport = async (source: string, coreJsPath: string) => {
const matchImport = importStr.match(/import\s+{\s+([\w*\s{},]*)\s+}\s+from\s+['"](.*)['"]/);
if (matchImport) {
const [,identifier] = matchImport;
const replaceModule = `var ${identifier.split('as')[1].trim()} = require('${targetImport.n.replace(/@swc\/helpers\/_\/(.*)$/,
const replaceModule = `var ${identifier.split(' as ')[1].trim()} = require('${targetImport.n.replace(/@swc\/helpers\/_\/(.*)$/,
(_, matched) => `@swc/helpers/cjs/${matched}.cjs`)}')._`;
str().overwrite(targetImport.ss, targetImport.se, replaceModule);
}

View File

@ -0,0 +1 @@
import { _ as _create_class } from '@swc/helpers/_/_create_class';module.exports = {};

View File

@ -47,6 +47,11 @@ describe('transform core js path', () => {
expect(await transformImport(orignalCode, coreJsPath))
.toBe('var _object_spread = require(\'@swc/helpers/cjs/_object_spread.cjs\')._;module.exports = {};');
});
it('@swc/helpers special identifier', async () => {
const orignalCode = fs.readFileSync(path.join(__dirname, './fixtures/transformImport/specialIdentifier.js'), 'utf-8');
expect(await transformImport(orignalCode, coreJsPath))
.toBe('var _create_class = require(\'@swc/helpers/cjs/_create_class.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))