Skip to content

Commit

Permalink
fix: support * as, close #78
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 24, 2021
1 parent a4b82d2 commit 84e14a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/core/transform.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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('')

Expand Down
9 changes: 7 additions & 2 deletions test/__snapshots__/transform.test.ts.snap
Expand Up @@ -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())
"
`;
Expand Down Expand Up @@ -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()
"
Expand Down Expand Up @@ -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)
"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/import-all.ts
@@ -0,0 +1 @@
const scene = new THREE.Scene();
3 changes: 3 additions & 0 deletions test/transform.test.ts
Expand Up @@ -28,6 +28,9 @@ describe('transform', () => {
'vue-dollar': [
'$',
],
'three.js': [
['*', 'THREE'],
],
},
],
ignore: [
Expand Down

0 comments on commit 84e14a8

Please sign in to comment.