diff --git a/packages/playground/glob-import/__tests__/glob-import.spec.ts b/packages/playground/glob-import/__tests__/glob-import.spec.ts index fff8d9fe202ebc..6eb50d2b5841da 100644 --- a/packages/playground/glob-import/__tests__/glob-import.spec.ts +++ b/packages/playground/glob-import/__tests__/glob-import.spec.ts @@ -66,6 +66,18 @@ const rawResult = { } } +const customModifierResult = { + '/dir/alias.js': { + exportNames: ['default'] + }, + '/dir/foo.js': { + exportNames: ['msg'] + }, + '/dir/index.js': { + exportNames: ['modules', 'globWithAlias'] + } +} + test('should work', async () => { expect(await page.textContent('.result')).toBe( JSON.stringify(allResult, null, 2) @@ -81,6 +93,12 @@ test('import glob raw', async () => { ) }) +test('custom modifer', async () => { + expect(await page.textContent('.custom-modifier')).toBe( + JSON.stringify(customModifierResult, null, 2) + ) +}) + if (!isBuild) { test('hmr for adding/removing files', async () => { addFile('dir/a.js', '') diff --git a/packages/playground/glob-import/index.html b/packages/playground/glob-import/index.html index 52d41b817a169c..b88a0083ef790b 100644 --- a/packages/playground/glob-import/index.html +++ b/packages/playground/glob-import/index.html @@ -1,6 +1,7 @@

 

 

+

 
 
 
+
+
diff --git a/packages/playground/glob-import/package.json b/packages/playground/glob-import/package.json
index 32abb932424757..bb342aec5e3838 100644
--- a/packages/playground/glob-import/package.json
+++ b/packages/playground/glob-import/package.json
@@ -7,5 +7,8 @@
     "build": "vite build",
     "debug": "node --inspect-brk ../../vite/bin/vite",
     "preview": "vite preview"
+  },
+  "dependencies": {
+    "es-module-lexer": "^0.9.3"
   }
 }
diff --git a/packages/playground/glob-import/vite.config.ts b/packages/playground/glob-import/vite.config.ts
index abc75b51656503..6a1682fb1776b4 100644
--- a/packages/playground/glob-import/vite.config.ts
+++ b/packages/playground/glob-import/vite.config.ts
@@ -1,10 +1,39 @@
 import path from 'path'
-import { defineConfig } from 'vite'
+import { defineConfig, Plugin } from 'vite'
+import { init, parse } from 'es-module-lexer'
 
 export default defineConfig({
   resolve: {
     alias: {
       '@dir': path.resolve(__dirname, './dir/')
     }
-  }
+  },
+  plugins: [customModifierPlugin()]
 })
+
+function customModifierPlugin() {
+  const metaRE = /(\?|&)meta(?:&|$)/
+  return {
+    name: 'custom-modifier',
+    async transform(src, id) {
+      if (!metaRE.test(id)) {
+        return
+      }
+
+      const exportNames = await getExportNames(src)
+
+      const code = `export const exportNames = ${JSON.stringify(exportNames)};`
+
+      return {
+        code,
+        map: { mappings: '' }
+      }
+    }
+  } as Plugin
+}
+
+async function getExportNames(src: string): Promise {
+  await init
+  const exportNames = parse(src)[1]
+  return exportNames
+}
diff --git a/packages/vite/src/node/importGlob.ts b/packages/vite/src/node/importGlob.ts
index 80217eb74fb5d4..f048380f6fb249 100644
--- a/packages/vite/src/node/importGlob.ts
+++ b/packages/vite/src/node/importGlob.ts
@@ -17,7 +17,6 @@ import {
 } from './utils'
 import type { RollupError } from 'rollup'
 import type { Logger } from '.'
-import colors from 'picocolors'
 
 interface GlobParams {
   base: string
@@ -136,17 +135,16 @@ export async function transformImportGlob(
       options?.assert?.type === 'raw'
     ) {
       logger.warn(
-        colors.yellow(
-          colors.bold(
-            "(!) Use `import.meta.globEager('/dir/*.js', { as: 'raw' })` instead of `import.meta.globEager('/dir/*.js', { assert: { type: 'raw' } })` (it will be deprecated in Vite 3.0)."
-          )
-        )
+        "Use `import.meta.globEager('/dir/*.js', { as: 'raw' })` instead of `import.meta.globEager('/dir/*.js', { assert: { type: 'raw' } })` (it will be deprecated in Vite 3.0)."
       )
       entries += ` ${JSON.stringify(file)}: ${JSON.stringify(
         await fsp.readFile(path.join(base, file), 'utf-8')
       )},`
     } else {
-      const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
+      let importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
+      if (options?.as) {
+        importeeUrl = `${importee}?${options.as}`
+      }
       if (isEager) {
         const identifier = `__glob_${importIndex}_${i}`
         // css imports injecting a ?used query to export the css string
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9982e48276c096..2cd44025599da9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -198,7 +198,10 @@ importers:
     specifiers: {}
 
   packages/playground/glob-import:
-    specifiers: {}
+    specifiers:
+      es-module-lexer: ^0.9.3
+    dependencies:
+      es-module-lexer: 0.9.3
 
   packages/playground/hmr:
     specifiers: {}
@@ -4327,7 +4330,6 @@ packages:
 
   /es-module-lexer/0.9.3:
     resolution: {integrity: sha512-1HQ2M2sPtxwnvOvT1ZClHyQDiggdNjURWpY2we6aMKCQiUVxTmVs2UYPLIrD84sS+kMdUwfBSylbJPwNnBrnHQ==}
-    dev: true
 
   /es-to-primitive/1.2.1:
     resolution: {integrity: sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==}