Skip to content

Commit

Permalink
refactor: use import.meta.glob over globEager in tests (#8066)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 8, 2022
1 parent 5e45428 commit 1878f46
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/playground/css/main.js
Expand Up @@ -87,7 +87,7 @@ Promise.all(
})

// globEager
const globEager = import.meta.globEager('./glob-import/*.css')
const globEager = import.meta.glob('./glob-import/*.css', { eager: true })
text('.imported-css-globEager', JSON.stringify(globEager, null, 2))

import postcssSourceInput from './postcss-source-input.css?query=foo'
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/glob-import/dir/index.js
@@ -1,4 +1,4 @@
const modules = import.meta.globEager('./*.(js|ts)')
const globWithAlias = import.meta.globEager('@dir/al*.js')
const modules = import.meta.glob('./*.(js|ts)', { eager: true })
const globWithAlias = import.meta.glob('@dir/al*.js', { eager: true })

export { modules, globWithAlias }
2 changes: 1 addition & 1 deletion packages/playground/glob-import/dir/nested/bar.js
@@ -1,4 +1,4 @@
const modules = import.meta.globEager('../*.json')
const modules = import.meta.glob('../*.json', { eager: true })

export const msg = 'bar'
export { modules }
15 changes: 7 additions & 8 deletions packages/playground/glob-import/index.html
Expand Up @@ -40,8 +40,9 @@
</script>

<script type="module">
const rawModules = import.meta.globEager('/dir/*.json', {
as: 'raw'
const rawModules = import.meta.glob('/dir/*.json', {
as: 'raw',
eager: true
})
const globraw = {}
Object.keys(rawModules).forEach((key) => {
Expand All @@ -55,12 +56,10 @@
</script>

<script type="module">
const relativeRawModules = import.meta.globEager(
'../glob-import/dir/*.json',
{
as: 'raw'
}
)
const relativeRawModules = import.meta.glob('../glob-import/dir/*.json', {
as: 'raw',
eager: true
})
const relativeGlobRaw = {}
Object.keys(relativeRawModules).forEach((key) => {
relativeGlobRaw[key] = JSON.parse(relativeRawModules[key])
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/optimize-deps/index.html
Expand Up @@ -76,7 +76,7 @@ <h2>Reused variable names</h2>

<script type="module">
// test dep detection in globbed files
const globbed = import.meta.globEager('./glob/*.js')
const globbed = import.meta.glob('./glob/*.js', { eager: true })

import { camelCase } from 'dep-linked'
text('.deps-linked', camelCase('foo-bar-baz'))
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/ssr-react/src/App.jsx
Expand Up @@ -2,7 +2,7 @@ import { Link, Route, Switch } from 'react-router-dom'

// Auto generates routes from files under ./pages
// https://vitejs.dev/guide/features.html#glob-import
const pages = import.meta.globEager('./pages/*.jsx')
const pages = import.meta.glob('./pages/*.jsx', { eager: true })

const routes = Object.keys(pages).map((path) => {
const name = path.match(/\.\/pages\/(.*)\.jsx$/)[1]
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/worker/__tests__/es/es-worker.spec.ts
Expand Up @@ -118,6 +118,6 @@ test('import.meta.glob in worker', async () => {
expect(await page.textContent('.importMetaGlob-worker')).toMatch('["')
})

test('import.meta.globEager in worker', async () => {
test('import.meta.glob with eager in worker', async () => {
expect(await page.textContent('.importMetaGlobEager-worker')).toMatch('["')
})
2 changes: 1 addition & 1 deletion packages/playground/worker/__tests__/iife/worker.spec.ts
Expand Up @@ -105,6 +105,6 @@ test('classic worker', async () => {
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic')
})

test('import.meta.globEager in worker', async () => {
test('import.meta.glob eager in worker', async () => {
expect(await page.textContent('.importMetaGlobEager-worker')).toMatch('["')
})
2 changes: 1 addition & 1 deletion packages/playground/worker/importMetaGlobEager.worker.js
@@ -1,4 +1,4 @@
const modules = import.meta.globEager('./modules/*js')
const modules = import.meta.glob('./modules/*js', { eager: true })

self.onmessage = function (e) {
self.postMessage(Object.keys(modules))
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/worker/index.html
Expand Up @@ -73,7 +73,7 @@ <h2 class="format-iife">format iife:</h2>
<code class="classic-shared-worker"></code>

<p>
use import.meta.globEager in iife worker
use import.meta.glob with eager in iife worker
<span class="classname">.importMetaGlobEager-worker</span>
</p>
<code class="importMetaGlobEager-worker"></code>
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -12,7 +12,7 @@ import { stripLiteral } from 'strip-literal'
* ```
* new URL(`./dir/${name}.png`, import.meta.url)
* // transformed to
* import.meta.globEager('./dir/**.png')[`./dir/${name}.png`].default
* import.meta.glob('./dir/**.png', { eager: true, import: 'default' })[`./dir/${name}.png`]
* ```
*/
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
Expand Down
3 changes: 0 additions & 3 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -157,9 +157,6 @@ export async function parseImportGlob(
throw err('Could only use literals')
}

// if (!globs.every(i => i.match(/^[.\/!]/)))
// throw err('pattern must start with "." or "/" (relative to project root) or alias path')

// arg2
const options: GeneralImportGlobOptions = {}
if (arg2) {
Expand Down

0 comments on commit 1878f46

Please sign in to comment.