Skip to content

Commit

Permalink
feat(glob-import): support { import: '*' } (#8071)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 9, 2022
1 parent 1878f46 commit 0b78b2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Expand Up @@ -38,6 +38,10 @@ export const eagerAs = {
\\"./modules/a.ts\\": __vite_glob_5_0,
\\"./modules/b.ts\\": __vite_glob_5_1
};
export const rawImportModule = {
\\"./modules/a.ts\\": () => import(\\"./modules/a.ts?raw\\"),
\\"./modules/b.ts\\": () => import(\\"./modules/b.ts?raw\\")
};
export const excludeSelf = {
\\"./sibling.ts\\": () => import(\\"./sibling.ts\\")
};
Expand Down Expand Up @@ -108,6 +112,10 @@ export const eagerAs = {
\\"./modules/a.ts\\": __vite_glob_5_0,
\\"./modules/b.ts\\": __vite_glob_5_1
};
export const rawImportModule = {
\\"./modules/a.ts\\": () => import(\\"./modules/a.ts?raw\\"),
\\"./modules/b.ts\\": () => import(\\"./modules/b.ts?raw\\")
};
export const excludeSelf = {
\\"./sibling.ts\\": () => import(\\"./sibling.ts\\")
};
Expand Down
Expand Up @@ -27,6 +27,11 @@ export const eagerAs = import.meta.glob<ModuleType>(
{ eager: true, as: 'raw' }
)

export const rawImportModule = import.meta.glob(
['./modules/*.ts', '!**/index.ts'],
{ as: 'raw', import: '*' }
)

export const excludeSelf = import.meta.glob(
'./*.ts'
// for test: annotation contain ")"
Expand Down
25 changes: 16 additions & 9 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -217,11 +217,15 @@ export async function parseImportGlob(
}

if (options.as && forceDefaultAs.includes(options.as)) {
if (options.import && options.import !== 'default')
if (
options.import &&
options.import !== 'default' &&
options.import !== '*'
)
throw err(
`Option "export" can only be "default" when "as" is "${options.as}", but got "${options.import}"`
`Option "import" can only be "default" or "*" when "as" is "${options.as}", but got "${options.import}"`
)
options.import = 'default'
options.import = options.import || 'default'
}

if (options.as && options.query)
Expand Down Expand Up @@ -358,21 +362,24 @@ export async function transformGlobImport(

importPath = `${importPath}${importQuery}`

const importKey =
options.import && options.import !== '*'
? options.import
: undefined

if (options.eager) {
const variableName = `${importPrefix}${index}_${i}`
const expression = options.import
? `{ ${options.import} as ${variableName} }`
const expression = importKey
? `{ ${importKey} as ${variableName} }`
: `* as ${variableName}`
staticImports.push(
`import ${expression} from ${JSON.stringify(importPath)}`
)
objectProps.push(`${JSON.stringify(filePath)}: ${variableName}`)
} else {
let importStatement = `import(${JSON.stringify(importPath)})`
if (options.import)
importStatement += `.then(m => m[${JSON.stringify(
options.import
)}])`
if (importKey)
importStatement += `.then(m => m[${JSON.stringify(importKey)}])`
objectProps.push(
`${JSON.stringify(filePath)}: () => ${importStatement}`
)
Expand Down

0 comments on commit 0b78b2a

Please sign in to comment.