Skip to content

Commit

Permalink
feat: support custom modifiers for glob imports
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Mar 7, 2022
1 parent 4944064 commit 2a41ae3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
18 changes: 18 additions & 0 deletions packages/playground/glob-import/__tests__/glob-import.spec.ts
Expand Up @@ -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)
Expand All @@ -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', '')
Expand Down
12 changes: 12 additions & 0 deletions packages/playground/glob-import/index.html
@@ -1,6 +1,7 @@
<pre class="result"></pre>
<pre class="result-node_modules"></pre>
<pre class="globraw"></pre>
<pre class="custom-modifier"></pre>

<script type="module" src="./dir/index.js"></script>
<script type="module">
Expand Down Expand Up @@ -52,3 +53,14 @@
2
)
</script>

<script type="module">
const modulesMeta = import.meta.globEager('/dir/*.js', {
as: 'meta'
})
document.querySelector('.custom-modifier').textContent = JSON.stringify(
modulesMeta,
null,
2
)
</script>
3 changes: 3 additions & 0 deletions packages/playground/glob-import/package.json
Expand Up @@ -7,5 +7,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"es-module-lexer": "^0.9.3"
}
}
33 changes: 31 additions & 2 deletions 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<readonly string[]> {
await init
const exportNames = parse(src)[1]
return exportNames
}
12 changes: 5 additions & 7 deletions packages/vite/src/node/importGlob.ts
Expand Up @@ -17,7 +17,6 @@ import {
} from './utils'
import type { RollupError } from 'rollup'
import type { Logger } from '.'
import colors from 'picocolors'

interface GlobParams {
base: string
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a41ae3

Please sign in to comment.