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