Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(glob-import): support { import: '*' } #8071

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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