From 84e14a835b671581e8aa75ceac49e2331e9a5a75 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 25 Nov 2021 00:33:45 +0800 Subject: [PATCH] fix: support `* as`, close #78 --- src/core/transform.ts | 23 ++++++++++++++++++----- test/__snapshots__/transform.test.ts.snap | 9 +++++++-- test/fixtures/import-all.ts | 1 + test/transform.test.ts | 3 +++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/import-all.ts diff --git a/src/core/transform.ts b/src/core/transform.ts index 9c4778b..31f4025 100644 --- a/src/core/transform.ts +++ b/src/core/transform.ts @@ -3,7 +3,7 @@ import { ImportInfo, TransformOptions, Resolver } from '../types' const excludeRE = [ // imported from other module - /\bimport\s*([\w_$]*?),?\s*(?:\{([\s\S]*?)\})?\s*from\b/g, + /\bimport\s*(.+?)\s*from\b/g, // defined as function /\bfunction\s*([\w_$]+?)\s*\(/g, // defined as local variable @@ -112,10 +112,23 @@ export function transform( // stringify import const importStatements = Object.entries(modules) .map(([moduleName, names]) => { - const imports = names - .map(({ name, from }) => from ? `${from} as ${name}` : name) - .join(', ') - return `import { ${imports} } from '${moduleName}';` + const imports: string[] = [] + const namedImports: string[] = [] + + names + .forEach(({ name, from }) => { + if (from === '*') + imports.push(`* as ${name}`) + else if (from === 'default') + imports.push(name) + else + namedImports.push(from ? `${from} as ${name}` : name) + }) + + if (namedImports.length) + imports.push(`{ ${namedImports.join(', ')} }`) + + return `import ${imports.join(', ')} from '${moduleName}';` }) .join('') diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index a3815f2..bdbf61c 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -19,7 +19,7 @@ const b = { class: \\"text-sm opacity-75\\" }; `; exports[`transform fixtures custom.js 1`] = ` -"import { default as customDefault, customNamed } from 'custom';const a = customDefault(customNamed()) +"import customDefault, { customNamed } from 'custom';const a = customDefault(customNamed()) " `; @@ -73,6 +73,11 @@ const msg = ref(\\"Global Imports\\"); " `; +exports[`transform fixtures import-all.ts 1`] = ` +"import * as THREE from 'three.js';const scene = new THREE.Scene(); +" +`; + exports[`transform fixtures non-target.js 1`] = ` "useNonTarget() " @@ -121,7 +126,7 @@ exports[`transform fixtures recursive.ts 1`] = ` `; exports[`transform fixtures resolver.ts 1`] = ` -"import { default as customResolved1 } from 'custom/resolved/1';import { default as customResolved2, _customNamedResolved2 as customNamedResolved2 } from 'custom/resolved/2';const a = customResolved1(1) +"import customResolved1 from 'custom/resolved/1';import customResolved2, { _customNamedResolved2 as customNamedResolved2 } from 'custom/resolved/2';const a = customResolved1(1) const b = customResolved2(1) const c = customNamedResolved2(1) " diff --git a/test/fixtures/import-all.ts b/test/fixtures/import-all.ts new file mode 100644 index 0000000..972ec6f --- /dev/null +++ b/test/fixtures/import-all.ts @@ -0,0 +1 @@ +const scene = new THREE.Scene(); diff --git a/test/transform.test.ts b/test/transform.test.ts index 416a898..8ce3001 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -28,6 +28,9 @@ describe('transform', () => { 'vue-dollar': [ '$', ], + 'three.js': [ + ['*', 'THREE'], + ], }, ], ignore: [