Skip to content

Commit

Permalink
refactor: remove ?used inject in glob plugin (#8900)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jul 3, 2022
1 parent ae34565 commit f9b5c14
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 79 deletions.
4 changes: 0 additions & 4 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -18,7 +18,6 @@ import type { ViteDevServer } from '../server'
import type { ModuleNode } from '../server/moduleGraph'
import type { ResolvedConfig } from '../config'
import { normalizePath, slash, transformStableResult } from '../utils'
import { isCSSRequest } from './css'

const { isMatch, scan } = micromatch

Expand Down Expand Up @@ -393,9 +392,6 @@ export async function transformGlobImport(
let importPath = paths.importPath
let importQuery = query

if (isCSSRequest(file))
importQuery = importQuery ? `${importQuery}&used` : '?used'

if (importQuery && importQuery !== '?raw') {
const fileExtension = basename(file).split('.').slice(-1)[0]
if (fileExtension && restoreQueryExtension)
Expand Down
137 changes: 62 additions & 75 deletions playground/glob-import/__tests__/glob-import.spec.ts
@@ -1,11 +1,4 @@
import {
addFile,
editFile,
isBuild,
page,
removeFile,
untilUpdated
} from '~utils'
import { addFile, editFile, isBuild, page, removeFile, withRetry } from '~utils'

const filteredResult = {
'./alias.js': {
Expand All @@ -16,21 +9,12 @@ const filteredResult = {
}
}

// json exports key order is altered during build, but it doesn't matter in
// terms of behavior since module exports are not ordered anyway
const json = isBuild
? {
msg: 'baz',
default: {
msg: 'baz'
}
}
: {
default: {
msg: 'baz'
},
msg: 'baz'
}
const json = {
msg: 'baz',
default: {
msg: 'baz'
}
}

const globWithAlias = {
'/dir/alias.js': {
Expand All @@ -44,6 +28,13 @@ const allResult = {
default: 'hi'
},
'/dir/baz.json': json,
'/dir/foo.css': isBuild
? {
default: '.foo{color:#00f}\n'
}
: {
default: '.foo {\n color: blue;\n}\n'
},
'/dir/foo.js': {
msg: 'foo'
},
Expand Down Expand Up @@ -81,16 +72,18 @@ const relativeRawResult = {
}

test('should work', async () => {
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(allResult, null, 2),
true
)
await untilUpdated(
() => page.textContent('.result-node_modules'),
JSON.stringify(nodeModulesResult, null, 2),
true
)
await withRetry(async () => {
const actual = await page.textContent('.result')
expect(JSON.parse(actual)).toStrictEqual(allResult)
}, true)
await withRetry(async () => {
const actualEager = await page.textContent('.result-eager')
expect(JSON.parse(actualEager)).toStrictEqual(allResult)
}, true)
await withRetry(async () => {
const actualNodeModules = await page.textContent('.result-node_modules')
expect(JSON.parse(actualNodeModules)).toStrictEqual(nodeModulesResult)
}, true)
})

test('import glob raw', async () => {
Expand All @@ -113,55 +106,49 @@ test('unassigned import processes', async () => {

if (!isBuild) {
test('hmr for adding/removing files', async () => {
const resultElement = page.locator('.result')

addFile('dir/a.js', '')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(
{
'/dir/a.js': {},
...allResult,
'/dir/index.js': {
...allResult['/dir/index.js'],
modules: {
'./a.js': {},
...allResult['/dir/index.js'].modules
}
await withRetry(async () => {
const actualAdd = await resultElement.textContent()
expect(JSON.parse(actualAdd)).toStrictEqual({
'/dir/a.js': {},
...allResult,
'/dir/index.js': {
...allResult['/dir/index.js'],
modules: {
'./a.js': {},
...allResult['/dir/index.js'].modules
}
},
null,
2
)
)
}
})
})

// edit the added file
editFile('dir/a.js', () => 'export const msg ="a"')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(
{
'/dir/a.js': {
msg: 'a'
},
...allResult,
'/dir/index.js': {
...allResult['/dir/index.js'],
modules: {
'./a.js': {
msg: 'a'
},
...allResult['/dir/index.js'].modules
}
}
await withRetry(async () => {
const actualEdit = await resultElement.textContent()
expect(JSON.parse(actualEdit)).toStrictEqual({
'/dir/a.js': {
msg: 'a'
},
null,
2
)
)
...allResult,
'/dir/index.js': {
...allResult['/dir/index.js'],
modules: {
'./a.js': {
msg: 'a'
},
...allResult['/dir/index.js'].modules
}
}
})
})

removeFile('dir/a.js')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(allResult, null, 2)
)
await withRetry(async () => {
const actualRemove = await resultElement.textContent()
expect(JSON.parse(actualRemove)).toStrictEqual(allResult)
})
})
}
3 changes: 3 additions & 0 deletions playground/glob-import/dir/foo.css
@@ -0,0 +1,3 @@
.foo {
color: blue;
}
7 changes: 7 additions & 0 deletions playground/glob-import/index.html
@@ -1,4 +1,5 @@
<pre class="result"></pre>
<pre class="result-eager"></pre>
<pre class="result-node_modules"></pre>
<pre class="globraw"></pre>
<pre class="relative-glob-raw"></pre>
Expand Down Expand Up @@ -35,6 +36,12 @@
* */
)
useImports(modules, '.result')
const eagerModules = import.meta.glob('/dir/**', { eager: true })
document.querySelector('.result-eager').textContent = JSON.stringify(
eagerModules,
null,
2
)

const nodeModules = import.meta.glob('/dir/node_modules/**')
useImports(nodeModules, '.result-node_modules')
Expand Down
19 changes: 19 additions & 0 deletions playground/test-utils.ts
Expand Up @@ -162,6 +162,25 @@ export async function untilUpdated(
}
}

/**
* Retry `func` until it does not throw error.
*/
export async function withRetry(
func: () => Promise<void>,
runInBuild = false
): Promise<void> {
if (isBuild && !runInBuild) return
const maxTries = process.env.CI ? 200 : 50
for (let tries = 0; tries < maxTries; tries++) {
try {
await func()
return
} catch {}
await timeout(50)
}
await func()
}

export async function untilBrowserLogAfter(
operation: () => any,
target: string | RegExp | Array<string | RegExp>,
Expand Down

0 comments on commit f9b5c14

Please sign in to comment.