From 0b93cfd610f01035c296be3f39521a83b9af6746 Mon Sep 17 00:00:00 2001 From: userquin Date: Thu, 13 Apr 2023 12:59:31 +0200 Subject: [PATCH 1/3] fix(browser): failing to load vitest/utils --- packages/browser/src/node/index.ts | 16 ++- pnpm-lock.yaml | 151 +++++++++++++++++++---------- 2 files changed, 115 insertions(+), 52 deletions(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index d582f8908e0e..eab42601866d 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -39,7 +39,21 @@ export default (base = '/'): Plugin[] => { config() { return { optimizeDeps: { - exclude: [...polyfills, ...builtinModules], + exclude: [ + ...polyfills, + ...builtinModules, + 'vitest', + 'vitest/utils', + 'vitest/browser', + 'vitest/runners', + '@vitest/utils', + ], + include: [ + '@vitest/utils > concordance', + '@vitest/utils > loupe', + '@vitest/utils > pretty-format', + 'vitest > chai', + ], }, } }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb5c5f6e3a4f..5597c82bafd4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -239,7 +239,7 @@ importers: devDependencies: '@testing-library/react': 13.3.0_zpnidt7m3osuk7shl3s4oenomq '@types/node': 18.15.11 - '@types/react': 18.0.33 + '@types/react': 18.0.35 '@vitejs/plugin-react': 3.1.0 jsdom: 21.1.1 typescript: 4.8.4 @@ -290,7 +290,7 @@ importers: '@types/react-test-renderer': 17.0.2 '@vitejs/plugin-react': 3.1.0_vite@4.2.1 '@vitest/ui': link:../../packages/ui - happy-dom: 9.1.9 + happy-dom: 9.3.2 jsdom: 21.1.1 react-test-renderer: 17.0.2_react@17.0.2 vite: 4.2.1 @@ -1089,11 +1089,11 @@ importers: '@vitejs/plugin-vue': 4.1.0_vite@4.2.1+vue@3.2.47 '@vitest/browser': link:../../packages/browser '@vue/test-utils': 2.3.2_vue@3.2.47 - happy-dom: 9.1.9 + happy-dom: 9.3.2 vite: 4.2.1 vitest: link:../../packages/vitest vue: 3.2.47 - webdriverio: 8.7.0 + webdriverio: 8.8.2 test/css: specifiers: @@ -1316,7 +1316,7 @@ importers: strip-ansi: 7.0.1 vite: 4.2.1 vitest: link:../../packages/vitest - webdriverio: 8.7.0 + webdriverio: 8.8.2 test/web-worker: specifiers: @@ -5682,6 +5682,28 @@ packages: resolution: {integrity: sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==} dev: false + /@puppeteer/browsers/0.4.0: + resolution: {integrity: sha512-3iB5pWn9Sr55PKKwqFWSWjLsTKCOEhKNI+uV3BZesgXuA3IhsX8I3hW0HI+3ksMIPkh2mVYzKSpvgq3oicjG2Q==} + engines: {node: '>=14.1.0'} + hasBin: true + peerDependencies: + typescript: '>= 4.7.4' + peerDependenciesMeta: + typescript: + optional: true + dependencies: + debug: 4.3.4 + extract-zip: 2.0.1 + https-proxy-agent: 5.0.1 + progress: 2.0.3 + proxy-from-env: 1.1.0 + tar-fs: 2.1.1 + unbzip2-stream: 1.4.3 + yargs: 17.7.1 + transitivePeerDependencies: + - supports-color + dev: true + /@rollup/plugin-alias/4.0.2_rollup@3.20.2: resolution: {integrity: sha512-1hv7dBOZZwo3SEupxn4UA2N0EDThqSSS+wI1St1TNTBtOZvUchyIClyHcnDcjjrReTPZ47Faedrhblv4n+T5UQ==} engines: {node: '>=14.0.0'} @@ -7505,7 +7527,7 @@ packages: resolution: {integrity: sha512-xryQlOEIe1TduDWAOphR0ihfebKFSWOXpIsk+70JskCfRfW+xALdnJ0r1ZOTo85F9Qsjk6vtlU7edTYHbls9tA==} dependencies: '@types/cheerio': 0.22.31 - '@types/react': 18.0.33 + '@types/react': 18.0.35 dev: true /@types/eslint-scope/3.7.4: @@ -7744,19 +7766,19 @@ packages: /@types/react-dom/18.0.6: resolution: {integrity: sha512-/5OFZgfIPSwy+YuIBP/FgJnQnsxhZhjjrnxudMddeblOouIodEQ75X14Rr4wGSG/bknL+Omy9iWlLo1u/9GzAA==} dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.35 dev: true /@types/react-dom/18.0.8: resolution: {integrity: sha512-C3GYO0HLaOkk9dDAz3Dl4sbe4AKUGTCfFIZsz3n/82dPNN8Du533HzKatDxeUYWu24wJgMP1xICqkWk1YOLOIw==} dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.35 dev: true /@types/react-is/17.0.3: resolution: {integrity: sha512-aBTIWg1emtu95bLTLx0cpkxwGW3ueZv71nE2YFBpL8k/z5czEW8yYpOo8Dp+UUAFAtKwNaOsh/ioSeQnWlZcfw==} dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.35 dev: false /@types/react-test-renderer/17.0.2: @@ -7768,7 +7790,7 @@ packages: /@types/react-transition-group/4.4.5: resolution: {integrity: sha512-juKD/eiSM3/xZYzjuzH6ZwpP+/lejltmiS3QEzV/vmb/Q8+HfDmxu+Baga8UEMGBqV88Nbg4l2hY/K2DkyaLLA==} dependencies: - '@types/react': 18.0.33 + '@types/react': 18.0.35 dev: false /@types/react/17.0.49: @@ -7787,8 +7809,8 @@ packages: csstype: 3.1.0 dev: true - /@types/react/18.0.33: - resolution: {integrity: sha512-sHxzVxeanvQyQ1lr8NSHaj0kDzcNiGpILEVt69g9S31/7PfMvNCKLKcsHw4lYKjs3cGNJjXSP4mYzX43QlnjNA==} + /@types/react/18.0.35: + resolution: {integrity: sha512-6Laome31HpetaIUGFWl1VQ3mdSImwxtFZ39rh059a1MNnKGqBpC88J6NJ8n/Is3Qx7CefDGLgf/KhN/sYCf7ag==} dependencies: '@types/prop-types': 15.7.5 '@types/scheduler': 0.16.2 @@ -8455,7 +8477,7 @@ packages: resolution: {integrity: sha512-55Shns6WPxlYsz4WX7q9ZJBL77sKE1ZAYNYStLs6GbhIOMrNtjMvzcob6gu3cGlfpCR4bT7NXgyJ3tly2+Hx8Q==} dependencies: '@babel/parser': 7.20.7 - postcss: 8.4.19 + postcss: 8.4.21 source-map: 0.6.1 /@vue/compiler-sfc/3.2.39: @@ -8752,13 +8774,13 @@ packages: read-pkg-up: 9.1.0 dev: true - /@wdio/config/8.7.0: - resolution: {integrity: sha512-zGUaM8PVbp0iDDlhUqWzFpwn2V3nko2k6/C80VggYAqNtmoE1R20DcYqknl8wZUZVlRNcpFVx98G3HqWf5kCQw==} + /@wdio/config/8.8.0: + resolution: {integrity: sha512-gm8gXqpiIR0EU9Blkqmxe+xsEoKS2EXpWrKlx2JXyx3Yf7By0UNsZVZHMSO8lLunzUjYIntpWYpmKmBmnlrnKQ==} engines: {node: ^16.13 || >=18} dependencies: '@wdio/logger': 8.6.6 - '@wdio/types': 8.7.0 - '@wdio/utils': 8.7.0 + '@wdio/types': 8.8.0 + '@wdio/utils': 8.8.0 decamelize: 6.0.0 deepmerge-ts: 5.0.0 glob: 9.3.0 @@ -8780,6 +8802,10 @@ packages: resolution: {integrity: sha512-RC7cwvLvtxaQGNC+m5YwwPO/p+NxM0HVoJ5DC/udoJgNiEZay75rtFqpKsUxHq7DU5IKYAZV1FzVQJ776EZUfg==} dev: true + /@wdio/protocols/8.8.1: + resolution: {integrity: sha512-nDaycUcm/ATLxcjim0fJyRiezXyyj4ZCq+Xf0S2Cubc0k7+DceFBt7KIMsernNVh2pelzFHEEwxh4DLaqQ71Wg==} + dev: true + /@wdio/repl/8.6.6: resolution: {integrity: sha512-vyJzqHJ5yOmfVyk5WWo6pRsJ2xhgWl3DVIBdDNR0wKrtFcm/g1jnB+pNf6Eb7NhCDh3oGul25bmhAwWDoxcFYA==} engines: {node: ^16.13 || >=18} @@ -8794,8 +8820,8 @@ packages: '@types/node': 18.15.11 dev: true - /@wdio/types/8.7.0: - resolution: {integrity: sha512-baiWFVR28mOdI7gI9802cnicGmlbfSZvhLWjd0cD2ep8BhvdengEWyj4GG+qqjk9ZfraBgHunDT+cB6hdPIPow==} + /@wdio/types/8.8.0: + resolution: {integrity: sha512-Ai6yIlwWB32FUfvQKCqSa6nSyHIhSF5BOU9OfE7I2XYkLAJTxu8B6NORHQ+rgoppHSWc4D2V9r21y3etF8AGnQ==} engines: {node: ^16.13 || >=18} dependencies: '@types/node': 18.15.11 @@ -8811,12 +8837,12 @@ packages: p-iteration: 1.1.8 dev: true - /@wdio/utils/8.7.0: - resolution: {integrity: sha512-2g2CSNRp5yJEOww2S3cLtTmyJ0PEIEbhff0nL3R0d0bkDDxuO198iUr0Oszay3KmVqg+Jt0mZlhFlNIa593WnQ==} + /@wdio/utils/8.8.0: + resolution: {integrity: sha512-JUl1AwdtrJ3GzwtEmLyLohh29ycKkTKQ9S7K5Tc3p4kC3d9YmFKsifVj9riyJUFFrbICO0d35O63kNzsVMYj/w==} engines: {node: ^16.13 || >=18} dependencies: '@wdio/logger': 8.6.6 - '@wdio/types': 8.7.0 + '@wdio/types': 8.8.0 import-meta-resolve: 2.2.2 p-iteration: 1.1.8 dev: true @@ -10846,6 +10872,15 @@ packages: strip-ansi: 6.0.1 wrap-ansi: 7.0.0 + /cliui/8.0.1: + resolution: {integrity: sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==} + engines: {node: '>=12'} + dependencies: + string-width: 4.2.3 + strip-ansi: 6.0.1 + wrap-ansi: 7.0.0 + dev: true + /clone-deep/4.0.1: resolution: {integrity: sha512-neHB9xuzh/wk0dIHweyAXv2aPGZIVk3pLMe+/RNzINf17fe0OG96QroktYAUm7SM1PBnzTabaLboqqxDyMU+SQ==} engines: {node: '>=6'} @@ -11950,20 +11985,20 @@ packages: - utf-8-validate dev: true - /devtools/8.7.0: - resolution: {integrity: sha512-rl6yShHkh624wkCN9WWb7uUxGsgIfUAL2v6fcHQvPmI42BRRkbiXURq2oRds9fup3qyCv2UPeAlYtf4LQerkDQ==} + /devtools/8.8.2: + resolution: {integrity: sha512-iD/Xr5rMjx+VD08akiAuiMR5EhWKj9i489rcDm2vdC9iB8u8HY51s9FbmnIrN5OstYma0x2qOF2zKxM/muJTtw==} engines: {node: ^16.13 || >=18} dependencies: '@types/node': 18.15.11 - '@wdio/config': 8.7.0 + '@wdio/config': 8.8.0 '@wdio/logger': 8.6.6 - '@wdio/protocols': 8.6.6 - '@wdio/types': 8.7.0 - '@wdio/utils': 8.7.0 + '@wdio/protocols': 8.8.1 + '@wdio/types': 8.8.0 + '@wdio/utils': 8.8.0 chrome-launcher: 0.15.1 edge-paths: 3.0.5 import-meta-resolve: 2.2.2 - puppeteer-core: 19.8.3 + puppeteer-core: 19.8.5 query-selector-shadow-dom: 1.0.1 ua-parser-js: 1.0.34 uuid: 9.0.0 @@ -14635,8 +14670,8 @@ packages: - encoding dev: true - /happy-dom/9.1.9: - resolution: {integrity: sha512-OMbnoknA7iNNG/5fwt1JckCKc53QLLFo2ljzit1pCV9SC1TYwcQj0obq0QUTeqIf2p2skbFG69bo19YoSj/1DA==} + /happy-dom/9.3.2: + resolution: {integrity: sha512-WTsnOmYXCiHs4REtjWSpi4lWJfhFj3ag9NMRrl0a1gwmAXwjRGm7fppdf6e3v38HtOins1Lgv0WKY8+ZlL0kDQ==} dependencies: css.escape: 1.5.1 he: 1.2.0 @@ -17107,8 +17142,8 @@ packages: brace-expansion: 2.0.1 dev: true - /minimatch/8.0.3: - resolution: {integrity: sha512-tEEvU9TkZgnFDCtpnrEYnPsjT7iUx42aXfs4bzmQ5sMA09/6hZY0jeZcGkXyDagiBOvkUjNo8Viom+Me6+2x7g==} + /minimatch/9.0.0: + resolution: {integrity: sha512-0jJj8AvgKqWN05mrwuqi8QYKx1WmYSUoKSxu5Qhs9prezTz10sxAHGNZe9J9cqIJzta8DWsleh2KaVaLl6Ru2w==} engines: {node: '>=16 || 14 >=14.17'} dependencies: brace-expansion: 2.0.1 @@ -18897,8 +18932,8 @@ packages: - utf-8-validate dev: true - /puppeteer-core/19.8.3: - resolution: {integrity: sha512-+gPT43K3trUIr+7+tNjCg95vMgM9RkFIeeyck3SXUTBpGkrf6sxfWckncqY8MnzHfRcyapvztKNmqvJ+Ngukyg==} + /puppeteer-core/19.8.5: + resolution: {integrity: sha512-zoGhim/oBQbkND6h4Xz4X7l5DkWVH9wH7z0mVty5qa/c0P1Yad47t/npVtt2xS10BiQwzztWKx7Pa2nJ5yykdw==} engines: {node: '>=14.14.0'} peerDependencies: typescript: '>= 4.7.4' @@ -18906,6 +18941,7 @@ packages: typescript: optional: true dependencies: + '@puppeteer/browsers': 0.4.0 chromium-bidi: 0.4.6_7yd6ibrwer4fxzjxd6md3toxiy cross-fetch: 3.1.5 debug: 4.3.4 @@ -22702,17 +22738,17 @@ packages: - utf-8-validate dev: true - /webdriver/8.7.0: - resolution: {integrity: sha512-UYstwTX6OtYJUnkUv6hI1XmSzrPMPqglHPTcanhS+EI1Ve4JmA8bSmkzL08gmiAroBjgLtqzpEcL5KuyAPEiLw==} + /webdriver/8.8.2: + resolution: {integrity: sha512-ug+7n0JgRTLG5pGyD53VKValSLFcr0oll1QelH1Gueh/umt3Ipu+SDV+v+lyCZY6CKasCsv80W8oZfpLBj1ClQ==} engines: {node: ^16.13 || >=18} dependencies: '@types/node': 18.15.11 '@types/ws': 8.5.4 - '@wdio/config': 8.7.0 + '@wdio/config': 8.8.0 '@wdio/logger': 8.6.6 - '@wdio/protocols': 8.6.6 - '@wdio/types': 8.7.0 - '@wdio/utils': 8.7.0 + '@wdio/protocols': 8.8.1 + '@wdio/types': 8.8.0 + '@wdio/utils': 8.8.0 deepmerge-ts: 5.0.0 got: 12.6.0 ky: 0.33.3 @@ -22759,35 +22795,35 @@ packages: - utf-8-validate dev: true - /webdriverio/8.7.0: - resolution: {integrity: sha512-drZKdS1IdGeYOKxZWAh/AOHgbz9oGAM3YVcgKun3npLTZGNh7jSfsrUYRmJe+px8Xz7Ay5Qz30QgqtbEqk7kBQ==} + /webdriverio/8.8.2: + resolution: {integrity: sha512-H3h+sHLg9hfQCPLvpd30hi/ZW4lLy73m4zrZ5kJyuuUf38SZFLcfXD3W1RGig88RQ/75Ui164ugwHasTMiQEgw==} engines: {node: ^16.13 || >=18} dependencies: '@types/node': 18.15.11 - '@wdio/config': 8.7.0 + '@wdio/config': 8.8.0 '@wdio/logger': 8.6.6 - '@wdio/protocols': 8.6.6 + '@wdio/protocols': 8.8.1 '@wdio/repl': 8.6.6 - '@wdio/types': 8.7.0 - '@wdio/utils': 8.7.0 + '@wdio/types': 8.8.0 + '@wdio/utils': 8.8.0 archiver: 5.3.1 aria-query: 5.0.2 css-shorthand-properties: 1.1.1 css-value: 0.0.1 - devtools: 8.7.0 + devtools: 8.8.2 devtools-protocol: 0.0.1124027 grapheme-splitter: 1.0.4 import-meta-resolve: 2.2.2 is-plain-obj: 4.1.0 lodash.clonedeep: 4.5.0 lodash.zip: 4.2.0 - minimatch: 8.0.3 - puppeteer-core: 19.8.3 + minimatch: 9.0.0 + puppeteer-core: 19.8.5 query-selector-shadow-dom: 1.0.1 resq: 1.11.0 rgb2hex: 0.2.5 serialize-error: 8.1.0 - webdriver: 8.7.0 + webdriver: 8.8.2 transitivePeerDependencies: - bufferutil - encoding @@ -23412,6 +23448,19 @@ packages: yargs-parser: 21.1.1 dev: true + /yargs/17.7.1: + resolution: {integrity: sha512-cwiTb08Xuv5fqF4AovYacTFNxk62th7LKJ6BL9IGUpTJrWoU7/7WdQGTP2SjKf1dUNBGzDd28p/Yfs/GI6JrLw==} + engines: {node: '>=12'} + dependencies: + cliui: 8.0.1 + escalade: 3.1.1 + get-caller-file: 2.0.5 + require-directory: 2.1.1 + string-width: 4.2.3 + y18n: 5.0.8 + yargs-parser: 21.1.1 + dev: true + /yauzl/2.10.0: resolution: {integrity: sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==} dependencies: From d9225502253b3e72b28a6c408b6bd3dd4f047ff5 Mon Sep 17 00:00:00 2001 From: userquin Date: Fri, 14 Apr 2023 14:11:07 +0200 Subject: [PATCH 2/3] chore: remove version param when resolving tests paths --- packages/browser/src/client/main.ts | 4 ++-- packages/browser/src/client/runner.ts | 19 ++++++++++--------- packages/browser/src/node/index.ts | 25 ++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/browser/src/client/main.ts b/packages/browser/src/client/main.ts index 50a636cb7ca5..2efc78fea15c 100644 --- a/packages/browser/src/client/main.ts +++ b/packages/browser/src/client/main.ts @@ -21,7 +21,7 @@ export const ENTRY_URL = `${ let config: ResolvedConfig | undefined let runner: VitestRunner | undefined -const browserHashMap = new Map() +const browserHashMap = new Map() const url = new URL(location.href) const testId = url.searchParams.get('id') || 'unknown' @@ -113,7 +113,7 @@ async function runTests(paths: string[], config: ResolvedConfig) { }) const now = `${new Date().getTime()}` - files.forEach(i => browserHashMap.set(i, now)) + files.forEach(i => browserHashMap.set(i, [true, now])) for (const file of files) await startTests([file], runner) diff --git a/packages/browser/src/client/runner.ts b/packages/browser/src/client/runner.ts index 3bdc40c1f0d2..03346219672e 100644 --- a/packages/browser/src/client/runner.ts +++ b/packages/browser/src/client/runner.ts @@ -4,7 +4,7 @@ import type { ResolvedConfig } from '#types' interface BrowserRunnerOptions { config: ResolvedConfig - browserHashMap: Map + browserHashMap: Map } interface CoverageHandler { @@ -14,7 +14,7 @@ interface CoverageHandler { export function createBrowserRunner(original: any, coverageModule: CoverageHandler | null) { return class BrowserTestRunner extends original { public config: ResolvedConfig - hashMap = new Map() + hashMap = new Map() constructor(options: BrowserRunnerOptions) { super(options.config) @@ -44,15 +44,16 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl } async importFile(filepath: string) { - const match = filepath.match(/^(\w:\/)/) - let hash = this.hashMap.get(filepath) - if (!hash) { + let [test, hash] = this.hashMap.get(filepath) ?? [false, ''] + if (hash === '') { hash = Date.now().toString() - this.hashMap.set(filepath, hash) + this.hashMap.set(filepath, [false, hash]) } - const importpath = match - ? `/@fs/${filepath.slice(match[1].length)}?v=${hash}` - : `${filepath}?v=${hash}` + + // on Windows we need the unit to resolve the test file + const importpath = /^\w:/.test(filepath) + ? `/@fs/${filepath}?${test ? 'browserv' : 'v'}=${hash}` + : `${filepath}?${test ? 'browserv' : 'v'}=${hash}` await import(importpath) } } diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index eab42601866d..d9090f43e8b3 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -2,6 +2,7 @@ import { fileURLToPath } from 'node:url' import { resolve } from 'node:path' import { builtinModules } from 'node:module' +import { readFile } from 'node:fs/promises' import { polyfillPath } from 'modern-node-polyfills' import sirv from 'sirv' import type { Plugin } from 'vite' @@ -58,12 +59,34 @@ export default (base = '/'): Plugin[] => { } }, async resolveId(id) { - if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) + if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) { + if (/\?browserv=\w+$/.test(id)) { + let useId = id.slice(0, id.lastIndexOf('?')) + if (useId.startsWith('/@fs/')) + useId = useId.slice(5) + + if (/^\w:/.test(useId)) + useId = useId.replace(/\\/g, '/') + } + return + } id = normalizeId(id) return { id: await polyfillPath(id), moduleSideEffects: false } }, + async load(id) { + if (/\?browserv=\w+$/.test(id)) { + let useId = id.slice(0, id.lastIndexOf('?')) + if (useId.startsWith('/@fs/')) + useId = useId.slice(5) + + if (/^\w:/.test(useId)) + useId = useId.replace(/\\/g, '/') + + return await readFile(resolve(useId), 'utf-8') + } + }, }, ] } From d5fdf5f8368e60343fdc751dc3f2c0655f8959be Mon Sep 17 00:00:00 2001 From: userquin Date: Fri, 14 Apr 2023 14:37:29 +0200 Subject: [PATCH 3/3] chore: update browser version plugin logic --- packages/browser/src/node/index.ts | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index d9090f43e8b3..a335552c82e6 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -2,7 +2,6 @@ import { fileURLToPath } from 'node:url' import { resolve } from 'node:path' import { builtinModules } from 'node:module' -import { readFile } from 'node:fs/promises' import { polyfillPath } from 'modern-node-polyfills' import sirv from 'sirv' import type { Plugin } from 'vite' @@ -60,23 +59,9 @@ export default (base = '/'): Plugin[] => { }, async resolveId(id) { if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) { - if (/\?browserv=\w+$/.test(id)) { - let useId = id.slice(0, id.lastIndexOf('?')) - if (useId.startsWith('/@fs/')) - useId = useId.slice(5) + if (!/\?browserv=\w+$/.test(id)) + return - if (/^\w:/.test(useId)) - useId = useId.replace(/\\/g, '/') - } - - return - } - - id = normalizeId(id) - return { id: await polyfillPath(id), moduleSideEffects: false } - }, - async load(id) { - if (/\?browserv=\w+$/.test(id)) { let useId = id.slice(0, id.lastIndexOf('?')) if (useId.startsWith('/@fs/')) useId = useId.slice(5) @@ -84,8 +69,11 @@ export default (base = '/'): Plugin[] => { if (/^\w:/.test(useId)) useId = useId.replace(/\\/g, '/') - return await readFile(resolve(useId), 'utf-8') + return useId } + + id = normalizeId(id) + return { id: await polyfillPath(id), moduleSideEffects: false } }, }, ]