mirror of https://github.com/alibaba/ice.git
fix: transform import with special identifier (#6499)
* fix: transform import with special identifier * fix: test case
This commit is contained in:
parent
99c0dd26e8
commit
101eadeaa5
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@ice/shared-config': patch
|
||||
---
|
||||
|
||||
fix: transform import with special identifier
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
import { _ as _create_class } from '@swc/helpers/_/_create_class';module.exports = {};
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in New Issue