From f9b5c14c42bf0a5c7d4ca4b53160047306fb07c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 4 Jul 2022 01:53:28 +0900 Subject: [PATCH] refactor: remove ?used inject in glob plugin (#8900) --- .../vite/src/node/plugins/importMetaGlob.ts | 4 - .../glob-import/__tests__/glob-import.spec.ts | 137 ++++++++---------- playground/glob-import/dir/foo.css | 3 + playground/glob-import/index.html | 7 + playground/test-utils.ts | 19 +++ 5 files changed, 91 insertions(+), 79 deletions(-) create mode 100644 playground/glob-import/dir/foo.css diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 5e0f473317b798..7f3695178fdce9 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -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 @@ -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) diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index a7297511ea4649..7b9b0aa622381e 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/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': { @@ -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': { @@ -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' }, @@ -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 () => { @@ -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) + }) }) } diff --git a/playground/glob-import/dir/foo.css b/playground/glob-import/dir/foo.css new file mode 100644 index 00000000000000..94ff8d4b4895c6 --- /dev/null +++ b/playground/glob-import/dir/foo.css @@ -0,0 +1,3 @@ +.foo { + color: blue; +} diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index 6cff2aa0965c59..ca00c065e7402d 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -1,4 +1,5 @@

+

 

 

 

@@ -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')
diff --git a/playground/test-utils.ts b/playground/test-utils.ts
index d1c9da43d7e4c8..a0fedd0db426d6 100644
--- a/playground/test-utils.ts
+++ b/playground/test-utils.ts
@@ -162,6 +162,25 @@ export async function untilUpdated(
   }
 }
 
+/**
+ * Retry `func` until it does not throw error.
+ */
+export async function withRetry(
+  func: () => Promise,
+  runInBuild = false
+): Promise {
+  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,