From 54d5a8b22f0ab4f50f1368de7111db935fd40d79 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Mon, 4 Jul 2022 00:42:05 +0900 Subject: [PATCH 1/4] refactor: remove used inject --- packages/vite/src/node/plugins/importMetaGlob.ts | 4 ---- .../glob-import/__tests__/glob-import.spec.ts | 14 ++++++++++++++ playground/glob-import/dir/foo.css | 3 +++ playground/glob-import/index.html | 7 +++++++ 4 files changed, 24 insertions(+), 4 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..e3036ffe20d84a 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -32,6 +32,14 @@ const json = isBuild msg: 'baz' } +const css = isBuild + ? { + default: '.foo{color:#00f}\n' + } + : { + default: '.foo {\n color: blue;\n}\n' + } + const globWithAlias = { '/dir/alias.js': { default: 'hi' @@ -44,6 +52,7 @@ const allResult = { default: 'hi' }, '/dir/baz.json': json, + '/dir/foo.css': css, '/dir/foo.js': { msg: 'foo' }, @@ -86,6 +95,11 @@ test('should work', async () => { JSON.stringify(allResult, null, 2), true ) + await untilUpdated( + () => page.textContent('.result-eager'), + JSON.stringify(allResult, null, 2), + true + ) await untilUpdated( () => page.textContent('.result-node_modules'), JSON.stringify(nodeModulesResult, null, 2), 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')

From 1588bed9c9872ac43dfa49b1a8b8827c5edc27a2 Mon Sep 17 00:00:00 2001
From: sapphi-red 
Date: Mon, 4 Jul 2022 01:23:02 +0900
Subject: [PATCH 2/4] chore: fix test

---
 .../glob-import/__tests__/glob-import.spec.ts | 165 ++++++++----------
 playground/test-utils.ts                      |  19 ++
 2 files changed, 95 insertions(+), 89 deletions(-)

diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts
index e3036ffe20d84a..067f073f358c1d 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,29 +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 css = isBuild
-  ? {
-      default: '.foo{color:#00f}\n'
-    }
-  : {
-      default: '.foo {\n  color: blue;\n}\n'
-    }
+const json = {
+  msg: 'baz',
+  default: {
+    msg: 'baz'
+  }
+}
 
 const globWithAlias = {
   '/dir/alias.js': {
@@ -52,7 +28,13 @@ const allResult = {
     default: 'hi'
   },
   '/dir/baz.json': json,
-  '/dir/foo.css': css,
+  '/dir/foo.css': isBuild
+    ? {
+        default: '.foo{color:#00f}\n'
+      }
+    : {
+        default: '.foo {\n  color: blue;\n}\n'
+      },
   '/dir/foo.js': {
     msg: 'foo'
   },
@@ -90,21 +72,18 @@ const relativeRawResult = {
 }
 
 test('should work', async () => {
-  await untilUpdated(
-    () => page.textContent('.result'),
-    JSON.stringify(allResult, null, 2),
-    true
-  )
-  await untilUpdated(
-    () => page.textContent('.result-eager'),
-    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 () => {
@@ -127,55 +106,63 @@ 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({
+        '/dir/a.js': {
+          msg: 'a'
+        },
+        ...allResult,
+        '/dir/index.js': {
+          ...allResult['/dir/index.js'],
+          modules: {
+            './a.js': {
+              msg: 'a'
+            },
+            ...allResult['/dir/index.js'].modules
+          }
+        }
+      })
+    })
   })
 }
diff --git a/playground/test-utils.ts b/playground/test-utils.ts
index d1c9da43d7e4c8..92fd1c340f52b3 100644
--- a/playground/test-utils.ts
+++ b/playground/test-utils.ts
@@ -162,6 +162,25 @@ export async function untilUpdated(
   }
 }
 
+/**
+ * Poll a getter until the value it returns includes the expected value.
+ */
+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,

From caf4e9755e429469859d44ff4423d6a1c8a98a69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=BF=A0=20/=20green?= 
Date: Mon, 4 Jul 2022 01:24:14 +0900
Subject: [PATCH 3/4] chore: fix comment

---
 playground/test-utils.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/playground/test-utils.ts b/playground/test-utils.ts
index 92fd1c340f52b3..a0fedd0db426d6 100644
--- a/playground/test-utils.ts
+++ b/playground/test-utils.ts
@@ -163,7 +163,7 @@ export async function untilUpdated(
 }
 
 /**
- * Poll a getter until the value it returns includes the expected value.
+ * Retry `func` until it does not throw error.
  */
 export async function withRetry(
   func: () => Promise,

From 62d773a25efa00d39611f5d823d1a50b9ab704f3 Mon Sep 17 00:00:00 2001
From: sapphi-red 
Date: Mon, 4 Jul 2022 01:25:01 +0900
Subject: [PATCH 4/4] chore: fix test case

---
 .../glob-import/__tests__/glob-import.spec.ts    | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts
index 067f073f358c1d..7b9b0aa622381e 100644
--- a/playground/glob-import/__tests__/glob-import.spec.ts
+++ b/playground/glob-import/__tests__/glob-import.spec.ts
@@ -148,21 +148,7 @@ if (!isBuild) {
     removeFile('dir/a.js')
     await withRetry(async () => {
       const actualRemove = await resultElement.textContent()
-      expect(JSON.parse(actualRemove)).toStrictEqual({
-        '/dir/a.js': {
-          msg: 'a'
-        },
-        ...allResult,
-        '/dir/index.js': {
-          ...allResult['/dir/index.js'],
-          modules: {
-            './a.js': {
-              msg: 'a'
-            },
-            ...allResult['/dir/index.js'].modules
-          }
-        }
-      })
+      expect(JSON.parse(actualRemove)).toStrictEqual(allResult)
     })
   })
 }