From 9a1dbca84784bef70acce5d45d689c4a142c98d4 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 8 Feb 2024 21:36:08 +0900 Subject: [PATCH 01/11] refactor: use `@jridgewell/trace-mapping` --- package.json | 1 + packages/vite/package.json | 1 + packages/vite/rollup.config.ts | 48 +++- .../src/node/ssr/runtime/sourcemap/decoder.ts | 213 +----------------- patches/@jridgewell__resolve-uri@3.1.1.patch | 25 ++ pnpm-lock.yaml | 30 ++- 6 files changed, 109 insertions(+), 209 deletions(-) create mode 100644 patches/@jridgewell__resolve-uri@3.1.1.patch diff --git a/package.json b/package.json index 7b6d67ee69eb96..c76a7d0a2cf372 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ } }, "patchedDependencies": { + "@jridgewell/resolve-uri@3.1.1": "patches/@jridgewell__resolve-uri@3.1.1.patch", "chokidar@3.6.0": "patches/chokidar@3.6.0.patch", "sirv@2.0.4": "patches/sirv@2.0.4.patch" }, diff --git a/packages/vite/package.json b/packages/vite/package.json index bccb4fb885affd..9009b442038c34 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -140,6 +140,7 @@ "postcss-modules": "^6.0.0", "resolve.exports": "^2.0.2", "rollup-plugin-dts": "^6.1.0", + "rollup-plugin-esbuild": "^6.1.1", "rollup-plugin-license": "^3.2.0", "sirv": "^2.0.4", "source-map-support": "^0.5.21", diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 6fa03af49edef0..f306fb56b5c7a2 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -8,6 +8,7 @@ import json from '@rollup/plugin-json' import MagicString from 'magic-string' import type { Plugin, RollupOptions } from 'rollup' import { defineConfig } from 'rollup' +import { minify as esbuildMinifyPlugin } from 'rollup-plugin-esbuild' import licensePlugin from './rollupLicensePlugin' const pkg = JSON.parse( @@ -153,7 +154,6 @@ function createNodeConfig(isProduction: boolean) { index: path.resolve(__dirname, 'src/node/index.ts'), cli: path.resolve(__dirname, 'src/node/cli.ts'), constants: path.resolve(__dirname, 'src/node/constants.ts'), - runtime: path.resolve(__dirname, 'src/node/ssr/runtime/index.ts'), }, output: { ...sharedNodeOptions.output, @@ -176,6 +176,51 @@ function createNodeConfig(isProduction: boolean) { }) } +function createRuntimeConfig(isProduction: boolean) { + return defineConfig({ + ...sharedNodeOptions, + input: { + runtime: path.resolve(__dirname, 'src/node/ssr/runtime/index.ts'), + }, + output: { + ...sharedNodeOptions.output, + sourcemap: !isProduction, + }, + external: [ + 'fsevents', + 'lightningcss', + 'rollup/parseAst', + ...Object.keys(pkg.dependencies), + ], + plugins: [ + ...createNodePlugins( + false, + !isProduction, + // in production we use rollup.dts.config.ts for dts generation + // in development we need to rely on the rollup ts plugin + isProduction ? false : './dist/node', + ), + esbuildMinifyPlugin({ minify: false, minifySyntax: true }), + { + name: 'replace bias', + transform(code, id) { + if (id.includes('@jridgewell+trace-mapping')) { + return { + code: code.replaceAll( + 'bias === LEAST_UPPER_BOUND', + 'true' + + `/*${'bias === LEAST_UPPER_BOUND'.length - '/**/'.length - 'true'.length}*/`, + ), + map: null, + } + } + }, + }, + bundleSizeLimit(43), + ], + }) +} + function createCjsConfig(isProduction: boolean) { return defineConfig({ ...sharedNodeOptions, @@ -209,6 +254,7 @@ export default (commandLineArgs: any): RollupOptions[] => { envConfig, clientConfig, createNodeConfig(isProduction), + createRuntimeConfig(isProduction), createCjsConfig(isProduction), ]) } diff --git a/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts b/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts index a3abcf9e388f98..8bbad42dab202e 100644 --- a/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts +++ b/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts @@ -1,3 +1,8 @@ +import type { + InvalidOriginalMapping, + OriginalMapping, +} from '@jridgewell/trace-mapping' +import { originalPositionFor } from '@jridgewell/trace-mapping' import { posixResolve } from '../utils' interface SourceMapLike { @@ -8,13 +13,6 @@ interface SourceMapLike { sourcesContent?: string[] } -type OriginalMapping = { - source: string | null - line: number - column: number - name: string | null -} - type Needle = { line: number column: number @@ -45,142 +43,6 @@ export class DecodedMap { } } -// This is a copy of all methods that we need for decoding a source map from "@jridgewell/trace-mapping" - -function indexOf(mappings: string, index: number) { - const idx = mappings.indexOf(';', index) - return idx === -1 ? mappings.length : idx -} - -const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -const charToInt = new Uint8Array(128) // z is 122 in ASCII -for (let i = 0; i < chars.length; i++) { - const c = chars.charCodeAt(i) - charToInt[c] = i -} - -function decodeInteger( - mappings: string, - pos: number, - state: Int32Array, - j: number, -) { - let value = 0 - let shift = 0 - let integer = 0 - do { - const c = mappings.charCodeAt(pos++) - integer = charToInt[c] - value |= (integer & 31) << shift - shift += 5 - } while (integer & 32) - const shouldNegate = value & 1 - value >>>= 1 - if (shouldNegate) { - value = -0x80000000 | -value - } - state[j] += value - return pos -} - -const comma = ','.charCodeAt(0) - -function hasMoreVlq(mappings: string, i: number, length: number) { - if (i >= length) return false - return mappings.charCodeAt(i) !== comma -} - -function decode(mappings: string): number[][][] { - const state = new Int32Array(5) - const decoded: number[][][] = [] - let index = 0 - do { - const semi = indexOf(mappings, index) - const line = [] - let sorted = true - let lastCol = 0 - state[0] = 0 - for (let i = index; i < semi; i++) { - let seg - i = decodeInteger(mappings, i, state, 0) // genColumn - const col = state[0] - if (col < lastCol) sorted = false - lastCol = col - if (hasMoreVlq(mappings, i, semi)) { - i = decodeInteger(mappings, i, state, 1) // sourcesIndex - i = decodeInteger(mappings, i, state, 2) // sourceLine - i = decodeInteger(mappings, i, state, 3) // sourceColumn - if (hasMoreVlq(mappings, i, semi)) { - i = decodeInteger(mappings, i, state, 4) // namesIndex - seg = [col, state[1], state[2], state[3], state[4]] - } else { - seg = [col, state[1], state[2], state[3]] - } - } else { - seg = [col] - } - line.push(seg) - } - if (!sorted) line.sort((a, b) => a[0] - b[0]) - decoded.push(line) - index = semi + 1 - } while (index <= mappings.length) - return decoded -} - -const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)' -const COL_GTR_EQ_ZERO = - '`column` must be greater than or equal to 0 (columns start at column 0)' - -const COLUMN = 0 -const SOURCES_INDEX = 1 -const SOURCE_LINE = 2 -const SOURCE_COLUMN = 3 -const NAMES_INDEX = 4 - -function OMapping( - source: string | null, - line: number, - column: number, - name: string | null, -): OriginalMapping { - return { source, line, column, name } -} - -function decodedMappings(map: DecodedMap): number[][][] { - return map._decoded || (map._decoded = decode(map._encoded)) -} - -let found = false -function binarySearch( - haystack: number[][], - needle: number, - low: number, - high: number, -) { - while (low <= high) { - const mid = low + ((high - low) >> 1) - const cmp = haystack[mid][COLUMN] - needle - if (cmp === 0) { - found = true - return mid - } - if (cmp < 0) { - low = mid + 1 - } else { - high = mid - 1 - } - } - found = false - return low - 1 -} - -function lowerBound(haystack: number[][], needle: number, index: number) { - for (let i = index - 1; i >= 0; index = i--) { - if (haystack[i][COLUMN] !== needle) break - } - return index -} interface Stats { lastKey: number lastNeedle: number @@ -193,68 +55,9 @@ function memoizedState(): Stats { lastIndex: -1, } } -function memoizedBinarySearch( - haystack: number[][], - needle: number, - state: Stats, - key: number, -) { - const { lastKey, lastNeedle, lastIndex } = state - let low = 0 - let high = haystack.length - 1 - if (key === lastKey) { - if (needle === lastNeedle) { - found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle - return lastIndex - } - if (needle >= lastNeedle) { - // lastIndex may be -1 if the previous needle was not found. - low = lastIndex === -1 ? 0 : lastIndex - } else { - high = lastIndex - } - } - state.lastKey = key - state.lastNeedle = needle - return (state.lastIndex = binarySearch(haystack, needle, low, high)) -} - -function traceSegmentInternal( - segments: number[][], - memo: Stats, - line: number, - column: number, -) { - let index = memoizedBinarySearch(segments, column, memo, line) - if (found) { - index = lowerBound(segments, column, index) - } - if (index === -1 || index === segments.length) return -1 - return index -} - export function getOriginalPosition( map: DecodedMap, - { line, column }: Needle, -): OriginalMapping | null { - line-- - if (line < 0) throw new Error(LINE_GTR_ZERO) - if (column < 0) throw new Error(COL_GTR_EQ_ZERO) - map._decodedMemo ??= memoizedState() - const decoded = decodedMappings(map) - // It's common for parent source maps to have pointers to lines that have no - // mapping (like a "//# sourceMappingURL=") at the end of the child file. - if (line >= decoded.length) return null - const segments = decoded[line] - const index = traceSegmentInternal(segments, map._decodedMemo, line, column) - if (index === -1) return null - const segment = segments[index] - if (segment.length === 1) return null - const { names, resolvedSources } = map - return OMapping( - resolvedSources[segment[SOURCES_INDEX]], - segment[SOURCE_LINE] + 1, - segment[SOURCE_COLUMN], - segment.length === 5 ? names[segment[NAMES_INDEX]] : null, - ) + needle: Needle, +): OriginalMapping | InvalidOriginalMapping { + return originalPositionFor(map as any, needle) } diff --git a/patches/@jridgewell__resolve-uri@3.1.1.patch b/patches/@jridgewell__resolve-uri@3.1.1.patch new file mode 100644 index 00000000000000..7fe9a54a007502 --- /dev/null +++ b/patches/@jridgewell__resolve-uri@3.1.1.patch @@ -0,0 +1,25 @@ +diff --git a/dist/resolve-uri.mjs b/dist/resolve-uri.mjs +index 94d8dceb931e21c2a55058c1249eb34c2dddfbc9..c09b5f1af2a8118cf7c45959a441cbfe57386e9c 100644 +--- a/dist/resolve-uri.mjs ++++ b/dist/resolve-uri.mjs +@@ -21,8 +21,8 @@ const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^# + * 4. Hash, including "#", optional. + */ + const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i; +-var UrlType; +-(function (UrlType) { ++var UrlType = /* #__PURE__ */ (function () { ++ var UrlType = {}; + UrlType[UrlType["Empty"] = 1] = "Empty"; + UrlType[UrlType["Hash"] = 2] = "Hash"; + UrlType[UrlType["Query"] = 3] = "Query"; +@@ -30,7 +30,8 @@ var UrlType; + UrlType[UrlType["AbsolutePath"] = 5] = "AbsolutePath"; + UrlType[UrlType["SchemeRelative"] = 6] = "SchemeRelative"; + UrlType[UrlType["Absolute"] = 7] = "Absolute"; +-})(UrlType || (UrlType = {})); ++ return UrlType; ++})(); + function isAbsoluteUrl(input) { + return schemeRegex.test(input); + } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0bcfb23744d3f8..69da503d0b7b53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,6 +10,9 @@ overrides: packageExtensionsChecksum: 4e49e84b7a11dfd4895fbffb006a7015 patchedDependencies: + '@jridgewell/resolve-uri@3.1.1': + hash: w3ykihndrrld7utbtpy6utj3bu + path: patches/@jridgewell__resolve-uri@3.1.1.patch chokidar@3.6.0: hash: bckcfsslxcffppz65mxcq6naau path: patches/chokidar@3.6.0.patch @@ -384,6 +387,9 @@ importers: rollup-plugin-dts: specifier: ^6.1.0 version: 6.1.0(rollup@4.2.0)(typescript@5.2.2) + rollup-plugin-esbuild: + specifier: ^6.1.1 + version: 6.1.1(esbuild@0.19.3)(rollup@4.2.0) rollup-plugin-license: specifier: ^3.2.0 version: 3.2.0(rollup@4.2.0) @@ -3627,9 +3633,10 @@ packages: '@jridgewell/sourcemap-codec': 1.4.15 '@jridgewell/trace-mapping': 0.3.22 - /@jridgewell/resolve-uri@3.1.1: + /@jridgewell/resolve-uri@3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu): resolution: {integrity: sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==} engines: {node: '>=6.0.0'} + patched: true /@jridgewell/set-array@1.1.2: resolution: {integrity: sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==} @@ -3648,13 +3655,13 @@ packages: /@jridgewell/trace-mapping@0.3.22: resolution: {integrity: sha512-Wf963MzWtA2sjrNt+g18IAln9lKnlRp+K2eH4jjIoF1wYeq3aMREpG09xhlhdzS0EjwU7qmUJYangWa+151vZw==} dependencies: - '@jridgewell/resolve-uri': 3.1.1 + '@jridgewell/resolve-uri': 3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu) '@jridgewell/sourcemap-codec': 1.4.15 /@jridgewell/trace-mapping@0.3.9: resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} dependencies: - '@jridgewell/resolve-uri': 3.1.1 + '@jridgewell/resolve-uri': 3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu) '@jridgewell/sourcemap-codec': 1.4.15 /@mapbox/node-pre-gyp@1.0.11: @@ -8457,6 +8464,23 @@ packages: '@babel/code-frame': 7.23.5 dev: true + /rollup-plugin-esbuild@6.1.1(esbuild@0.19.3)(rollup@4.2.0): + resolution: {integrity: sha512-CehMY9FAqJD5OUaE/Mi1r5z0kNeYxItmRO2zG4Qnv2qWKF09J2lTy5GUzjJR354ZPrLkCj4fiBN41lo8PzBUhw==} + engines: {node: '>=14.18.0'} + peerDependencies: + esbuild: '>=0.18.0' + rollup: ^1.20.0 || ^2.0.0 || ^3.0.0 || ^4.0.0 + dependencies: + '@rollup/pluginutils': 5.1.0(rollup@4.2.0) + debug: 4.3.4 + es-module-lexer: 1.4.1 + esbuild: 0.19.3 + get-tsconfig: 4.7.2 + rollup: 4.2.0 + transitivePeerDependencies: + - supports-color + dev: true + /rollup-plugin-license@3.2.0(rollup@4.2.0): resolution: {integrity: sha512-gLtSOTE3hZ/mDgxg1HvYz87timTpLlyWXnV7OTyYMhn+Esek+xKxAOjtTsYnfMFGtsBWX+hvqC4b2Ct5ABpE6A==} engines: {node: '>=14.0.0'} From f741299ace8bd03562e22eef8b149e7593762b0a Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 8 Feb 2024 22:12:45 +0900 Subject: [PATCH 02/11] refactor: move runtime directory to top-level --- packages/vite/rollup.config.ts | 3 ++- packages/vite/rollup.dts.config.ts | 4 +++- packages/vite/src/node/__tests__/tsconfig.json | 8 ++++++++ packages/vite/src/node/index.ts | 8 ++++---- packages/vite/src/node/server/index.ts | 2 +- packages/vite/src/node/ssr/fetchModule.ts | 2 +- .../node/ssr/runtime/{node => }/mainThreadRuntime.ts | 9 ++++----- .../ssr/runtime/{node => }/serverHmrConnector.ts | 9 +++------ packages/vite/src/node/ssr/ssrFetchModule.ts | 2 +- packages/vite/src/node/tsconfig.json | 9 ++++++--- .../{node/ssr => }/runtime/__tests__/fixtures/a.ts | 0 .../ssr => }/runtime/__tests__/fixtures/assets.js | 0 .../__tests__/fixtures/assets/placeholder.mov | 0 .../__tests__/fixtures/assets/placeholder.png | 0 .../__tests__/fixtures/assets/placeholder.txt | 0 .../__tests__/fixtures/assets/placeholder.webp | 0 .../{node/ssr => }/runtime/__tests__/fixtures/b.ts | 0 .../ssr => }/runtime/__tests__/fixtures/basic.js | 0 .../{node/ssr => }/runtime/__tests__/fixtures/c.ts | 2 +- .../__tests__/fixtures/circular/circular-a.js | 0 .../__tests__/fixtures/circular/circular-b.js | 0 .../__tests__/fixtures/circular/circular-index.js | 0 .../__tests__/fixtures/cjs-external-existing.js | 0 .../__tests__/fixtures/cjs-external-non-existing.js | 0 .../__tests__/fixtures/cjs-external/index.cjs | 0 .../__tests__/fixtures/cjs-external/package.json | 0 .../{node/ssr => }/runtime/__tests__/fixtures/d.ts | 2 +- .../runtime/__tests__/fixtures/dynamic-import.js | 0 .../__tests__/fixtures/esm-external-existing.js | 0 .../__tests__/fixtures/esm-external-non-existing.js | 0 .../__tests__/fixtures/esm-external/index.mjs | 0 .../__tests__/fixtures/esm-external/package.json | 0 .../ssr => }/runtime/__tests__/fixtures/has-error.js | 0 .../{node/ssr => }/runtime/__tests__/fixtures/hmr.js | 0 .../ssr => }/runtime/__tests__/fixtures/installed.js | 0 .../ssr => }/runtime/__tests__/fixtures/native.js | 0 .../ssr => }/runtime/__tests__/fixtures/simple.js | 0 .../ssr => }/runtime/__tests__/fixtures/test.css | 0 .../runtime/__tests__/fixtures/test.module.css | 0 .../__tests__/fixtures/throws-error-method.ts | 0 .../ssr => }/runtime/__tests__/fixtures/virtual.js | 0 .../{node/ssr => }/runtime/__tests__/package.json | 0 .../ssr => }/runtime/__tests__/server-hmr.spec.ts | 0 .../ssr => }/runtime/__tests__/server-no-hmr.spec.ts | 0 .../runtime/__tests__/server-runtime.spec.ts | 0 .../runtime/__tests__/server-source-maps.spec.ts | 0 .../src/{node/ssr => }/runtime/__tests__/utils.ts | 4 ++-- .../vite/src/{node/ssr => }/runtime/constants.ts | 0 .../vite/src/{node/ssr => }/runtime/esmRunner.ts | 0 .../vite/src/{node/ssr => }/runtime/hmrHandler.ts | 0 .../vite/src/{node/ssr => }/runtime/hmrLogger.ts | 2 +- packages/vite/src/{node/ssr => }/runtime/index.ts | 2 +- .../vite/src/{node/ssr => }/runtime/moduleCache.ts | 0 packages/vite/src/{node/ssr => }/runtime/runtime.ts | 2 +- .../src/{node/ssr => }/runtime/sourcemap/decoder.ts | 0 .../src/{node/ssr => }/runtime/sourcemap/index.ts | 0 .../{node/ssr => }/runtime/sourcemap/interceptor.ts | 0 packages/vite/src/runtime/tsconfig.json | 9 +++++++++ packages/vite/src/{node/ssr => }/runtime/types.ts | 2 +- packages/vite/src/{node/ssr => }/runtime/utils.ts | 0 packages/vite/tsconfig.json | 7 +------ pnpm-lock.yaml | 6 +++--- vitest.config.ts | 12 ++++++++++++ 63 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 packages/vite/src/node/__tests__/tsconfig.json rename packages/vite/src/node/ssr/runtime/{node => }/mainThreadRuntime.ts (88%) rename packages/vite/src/node/ssr/runtime/{node => }/serverHmrConnector.ts (90%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/a.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/assets.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/assets/placeholder.mov (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/assets/placeholder.png (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/assets/placeholder.txt (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/assets/placeholder.webp (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/b.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/basic.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/c.ts (74%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/circular/circular-a.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/circular/circular-b.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/circular/circular-index.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/cjs-external-existing.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/cjs-external-non-existing.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/cjs-external/index.cjs (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/cjs-external/package.json (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/d.ts (74%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/dynamic-import.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/esm-external-existing.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/esm-external-non-existing.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/esm-external/index.mjs (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/esm-external/package.json (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/has-error.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/hmr.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/installed.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/native.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/simple.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/test.css (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/test.module.css (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/throws-error-method.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/fixtures/virtual.js (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/package.json (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/server-hmr.spec.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/server-no-hmr.spec.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/server-runtime.spec.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/server-source-maps.spec.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/__tests__/utils.ts (97%) rename packages/vite/src/{node/ssr => }/runtime/constants.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/esmRunner.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/hmrHandler.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/hmrLogger.ts (66%) rename packages/vite/src/{node/ssr => }/runtime/index.ts (89%) rename packages/vite/src/{node/ssr => }/runtime/moduleCache.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/runtime.ts (99%) rename packages/vite/src/{node/ssr => }/runtime/sourcemap/decoder.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/sourcemap/index.ts (100%) rename packages/vite/src/{node/ssr => }/runtime/sourcemap/interceptor.ts (100%) create mode 100644 packages/vite/src/runtime/tsconfig.json rename packages/vite/src/{node/ssr => }/runtime/types.ts (98%) rename packages/vite/src/{node/ssr => }/runtime/utils.ts (100%) diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index f306fb56b5c7a2..7e9821301ccae5 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -160,6 +160,7 @@ function createNodeConfig(isProduction: boolean) { sourcemap: !isProduction, }, external: [ + /^vite\//, 'fsevents', 'lightningcss', 'rollup/parseAst', @@ -180,7 +181,7 @@ function createRuntimeConfig(isProduction: boolean) { return defineConfig({ ...sharedNodeOptions, input: { - runtime: path.resolve(__dirname, 'src/node/ssr/runtime/index.ts'), + runtime: path.resolve(__dirname, 'src/runtime/index.ts'), }, output: { ...sharedNodeOptions.output, diff --git a/packages/vite/rollup.dts.config.ts b/packages/vite/rollup.dts.config.ts index 6c4b84c6f914e1..41b1da1458a31e 100644 --- a/packages/vite/rollup.dts.config.ts +++ b/packages/vite/rollup.dts.config.ts @@ -15,6 +15,7 @@ const pkg = JSON.parse( const external = [ /^node:*/, + /^vite\//, 'rollup/parseAst', ...Object.keys(pkg.dependencies), // lightningcss types are bundled @@ -24,7 +25,7 @@ const external = [ export default defineConfig({ input: { index: './temp/node/index.d.ts', - runtime: './temp/node/ssr/runtime/index.d.ts', + runtime: './temp/runtime/index.d.ts', }, output: { dir: './dist/node', @@ -132,6 +133,7 @@ function validateChunkImports(this: PluginContext, chunk: RenderedChunk) { !id.startsWith('../') && !id.startsWith('node:') && !id.startsWith('types.d') && + !id.startsWith('vite/') && !deps.includes(id) && !deps.some((name) => id.startsWith(name + '/')) ) { diff --git a/packages/vite/src/node/__tests__/tsconfig.json b/packages/vite/src/node/__tests__/tsconfig.json new file mode 100644 index 00000000000000..ebfc2d30cf7493 --- /dev/null +++ b/packages/vite/src/node/__tests__/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "esModuleInterop": true, + "declaration": false, + "resolveJsonModule": true + } +} diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 61d50e0c6d6853..d640f51d31939e 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -128,10 +128,10 @@ export type { HMRBroadcasterClient, } from './server/hmr' -export type { FetchFunction } from './ssr/runtime/index' -export { createViteRuntime } from './ssr/runtime/node/mainThreadRuntime' -export type { MainThreadRuntimeOptions } from './ssr/runtime/node/mainThreadRuntime' -export { ServerHMRConnector } from './ssr/runtime/node/serverHmrConnector' +export type { FetchFunction } from '../runtime/index' +export { createViteRuntime } from './ssr/runtime/mainThreadRuntime' +export type { MainThreadRuntimeOptions } from './ssr/runtime/mainThreadRuntime' +export { ServerHMRConnector } from './ssr/runtime/serverHmrConnector' export type { BindCLIShortcutsOptions, CLIShortcut } from './shortcuts' diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 156244bbc3fd94..d3029175ce9376 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -48,7 +48,7 @@ import { printServerUrls } from '../logger' import { createNoopWatcher, resolveChokidarOptions } from '../watch' import { initPublicFiles } from '../publicDir' import { getEnvFilesForMode } from '../env' -import type { FetchResult } from '../ssr/runtime/types' +import type { FetchResult } from '../../runtime/types' import { ssrFetchModule } from '../ssr/ssrFetchModule' import type { PluginContainer } from './pluginContainer' import { ERR_CLOSED_SERVER, createPluginContainer } from './pluginContainer' diff --git a/packages/vite/src/node/ssr/fetchModule.ts b/packages/vite/src/node/ssr/fetchModule.ts index e62d5b25d1f2d3..802b457292a5e8 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -4,7 +4,7 @@ import type { PackageCache } from '../packages' import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' import { isBuiltin, isExternalUrl, isFilePathESM, unwrapId } from '../utils' -import type { FetchResult } from './runtime/types' +import type { FetchResult } from '../../runtime/types' interface NodeImportResolveOptions extends InternalResolveOptionsWithOverrideConditions { diff --git a/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts b/packages/vite/src/node/ssr/runtime/mainThreadRuntime.ts similarity index 88% rename from packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts rename to packages/vite/src/node/ssr/runtime/mainThreadRuntime.ts index 9146f42c1f3b21..cbb8e3d8edfbdd 100644 --- a/packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts +++ b/packages/vite/src/node/ssr/runtime/mainThreadRuntime.ts @@ -1,9 +1,8 @@ import { existsSync, readFileSync } from 'node:fs' -import type { ViteDevServer } from '../../../index' -import { ViteRuntime } from '../runtime' -import { ESModulesRunner } from '../esmRunner' -import type { ViteModuleRunner, ViteRuntimeOptions } from '../types' -import type { HMRLogger } from '../../../../shared/hmr' +import { ESModulesRunner, ViteRuntime } from 'vite/runtime' +import type { ViteModuleRunner, ViteRuntimeOptions } from 'vite/runtime' +import type { ViteDevServer } from '../../server' +import type { HMRLogger } from '../../../shared/hmr' import { ServerHMRConnector } from './serverHmrConnector' /** diff --git a/packages/vite/src/node/ssr/runtime/node/serverHmrConnector.ts b/packages/vite/src/node/ssr/runtime/serverHmrConnector.ts similarity index 90% rename from packages/vite/src/node/ssr/runtime/node/serverHmrConnector.ts rename to packages/vite/src/node/ssr/runtime/serverHmrConnector.ts index 21e0315ad381d5..b8bed32a8733c2 100644 --- a/packages/vite/src/node/ssr/runtime/node/serverHmrConnector.ts +++ b/packages/vite/src/node/ssr/runtime/serverHmrConnector.ts @@ -1,10 +1,7 @@ import type { CustomPayload, HMRPayload } from 'types/hmrPayload' -import type { ViteDevServer } from '../../../server' -import type { - HMRBroadcasterClient, - ServerHMRChannel, -} from '../../../server/hmr' -import type { HMRRuntimeConnection } from '../types' +import type { HMRRuntimeConnection } from 'vite/runtime' +import type { ViteDevServer } from '../../server' +import type { HMRBroadcasterClient, ServerHMRChannel } from '../../server/hmr' class ServerHMRBroadcasterClient implements HMRBroadcasterClient { constructor(private readonly hmrChannel: ServerHMRChannel) {} diff --git a/packages/vite/src/node/ssr/ssrFetchModule.ts b/packages/vite/src/node/ssr/ssrFetchModule.ts index 27ffe25085305c..27f9213963918f 100644 --- a/packages/vite/src/node/ssr/ssrFetchModule.ts +++ b/packages/vite/src/node/ssr/ssrFetchModule.ts @@ -1,6 +1,6 @@ import type { ViteDevServer } from '../server' +import type { FetchResult } from '../../runtime/types' import { fetchModule } from './fetchModule' -import type { FetchResult } from './runtime/types' // eslint-disable-next-line @typescript-eslint/no-empty-function const AsyncFunction = async function () {}.constructor as typeof Function diff --git a/packages/vite/src/node/tsconfig.json b/packages/vite/src/node/tsconfig.json index a7f7890f1d0e7b..bb2195ff6a9dd9 100644 --- a/packages/vite/src/node/tsconfig.json +++ b/packages/vite/src/node/tsconfig.json @@ -1,9 +1,12 @@ { "extends": "../../tsconfig.base.json", - "include": ["./", "../dep-types", "../types"], - "exclude": ["**/__tests__"], + "include": ["./", "../runtime", "../dep-types", "../types"], + "exclude": ["../**/__tests__"], "compilerOptions": { "lib": ["ESNext", "DOM"], - "stripInternal": true + "stripInternal": true, + "paths": { + "vite/runtime": ["../runtime"] + } } } diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/a.ts b/packages/vite/src/runtime/__tests__/fixtures/a.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/a.ts rename to packages/vite/src/runtime/__tests__/fixtures/a.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets.js b/packages/vite/src/runtime/__tests__/fixtures/assets.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets.js rename to packages/vite/src/runtime/__tests__/fixtures/assets.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.mov b/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.mov similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.mov rename to packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.mov diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.png b/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.png similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.png rename to packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.png diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.txt b/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.txt similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.txt rename to packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.txt diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.webp b/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.webp similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.webp rename to packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.webp diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/b.ts b/packages/vite/src/runtime/__tests__/fixtures/b.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/b.ts rename to packages/vite/src/runtime/__tests__/fixtures/b.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/basic.js b/packages/vite/src/runtime/__tests__/fixtures/basic.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/basic.js rename to packages/vite/src/runtime/__tests__/fixtures/basic.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts b/packages/vite/src/runtime/__tests__/fixtures/c.ts similarity index 74% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts rename to packages/vite/src/runtime/__tests__/fixtures/c.ts index d21d1b6f71e82a..86380486f9b21d 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts +++ b/packages/vite/src/runtime/__tests__/fixtures/c.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-console */ + export { a as c } from './a' diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-a.js b/packages/vite/src/runtime/__tests__/fixtures/circular/circular-a.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-a.js rename to packages/vite/src/runtime/__tests__/fixtures/circular/circular-a.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-b.js b/packages/vite/src/runtime/__tests__/fixtures/circular/circular-b.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-b.js rename to packages/vite/src/runtime/__tests__/fixtures/circular/circular-b.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-index.js b/packages/vite/src/runtime/__tests__/fixtures/circular/circular-index.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-index.js rename to packages/vite/src/runtime/__tests__/fixtures/circular/circular-index.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-existing.js b/packages/vite/src/runtime/__tests__/fixtures/cjs-external-existing.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-existing.js rename to packages/vite/src/runtime/__tests__/fixtures/cjs-external-existing.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-non-existing.js b/packages/vite/src/runtime/__tests__/fixtures/cjs-external-non-existing.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-non-existing.js rename to packages/vite/src/runtime/__tests__/fixtures/cjs-external-non-existing.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/index.cjs b/packages/vite/src/runtime/__tests__/fixtures/cjs-external/index.cjs similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/index.cjs rename to packages/vite/src/runtime/__tests__/fixtures/cjs-external/index.cjs diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/package.json b/packages/vite/src/runtime/__tests__/fixtures/cjs-external/package.json similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/package.json rename to packages/vite/src/runtime/__tests__/fixtures/cjs-external/package.json diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts b/packages/vite/src/runtime/__tests__/fixtures/d.ts similarity index 74% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts rename to packages/vite/src/runtime/__tests__/fixtures/d.ts index d85309b8e7e7cb..fff83783443af8 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts +++ b/packages/vite/src/runtime/__tests__/fixtures/d.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-console */ + export { c as d } from './c' diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/dynamic-import.js b/packages/vite/src/runtime/__tests__/fixtures/dynamic-import.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/dynamic-import.js rename to packages/vite/src/runtime/__tests__/fixtures/dynamic-import.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-existing.js b/packages/vite/src/runtime/__tests__/fixtures/esm-external-existing.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-existing.js rename to packages/vite/src/runtime/__tests__/fixtures/esm-external-existing.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-non-existing.js b/packages/vite/src/runtime/__tests__/fixtures/esm-external-non-existing.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-non-existing.js rename to packages/vite/src/runtime/__tests__/fixtures/esm-external-non-existing.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/index.mjs b/packages/vite/src/runtime/__tests__/fixtures/esm-external/index.mjs similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/index.mjs rename to packages/vite/src/runtime/__tests__/fixtures/esm-external/index.mjs diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/package.json b/packages/vite/src/runtime/__tests__/fixtures/esm-external/package.json similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/package.json rename to packages/vite/src/runtime/__tests__/fixtures/esm-external/package.json diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error.js b/packages/vite/src/runtime/__tests__/fixtures/has-error.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error.js rename to packages/vite/src/runtime/__tests__/fixtures/has-error.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/hmr.js b/packages/vite/src/runtime/__tests__/fixtures/hmr.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/hmr.js rename to packages/vite/src/runtime/__tests__/fixtures/hmr.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/installed.js b/packages/vite/src/runtime/__tests__/fixtures/installed.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/installed.js rename to packages/vite/src/runtime/__tests__/fixtures/installed.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/native.js b/packages/vite/src/runtime/__tests__/fixtures/native.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/native.js rename to packages/vite/src/runtime/__tests__/fixtures/native.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/simple.js b/packages/vite/src/runtime/__tests__/fixtures/simple.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/simple.js rename to packages/vite/src/runtime/__tests__/fixtures/simple.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.css b/packages/vite/src/runtime/__tests__/fixtures/test.css similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.css rename to packages/vite/src/runtime/__tests__/fixtures/test.css diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.module.css b/packages/vite/src/runtime/__tests__/fixtures/test.module.css similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.module.css rename to packages/vite/src/runtime/__tests__/fixtures/test.module.css diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/throws-error-method.ts b/packages/vite/src/runtime/__tests__/fixtures/throws-error-method.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/throws-error-method.ts rename to packages/vite/src/runtime/__tests__/fixtures/throws-error-method.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/virtual.js b/packages/vite/src/runtime/__tests__/fixtures/virtual.js similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/fixtures/virtual.js rename to packages/vite/src/runtime/__tests__/fixtures/virtual.js diff --git a/packages/vite/src/node/ssr/runtime/__tests__/package.json b/packages/vite/src/runtime/__tests__/package.json similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/package.json rename to packages/vite/src/runtime/__tests__/package.json diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts b/packages/vite/src/runtime/__tests__/server-hmr.spec.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts rename to packages/vite/src/runtime/__tests__/server-hmr.spec.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-no-hmr.spec.ts b/packages/vite/src/runtime/__tests__/server-no-hmr.spec.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/server-no-hmr.spec.ts rename to packages/vite/src/runtime/__tests__/server-no-hmr.spec.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts b/packages/vite/src/runtime/__tests__/server-runtime.spec.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts rename to packages/vite/src/runtime/__tests__/server-runtime.spec.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts b/packages/vite/src/runtime/__tests__/server-source-maps.spec.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts rename to packages/vite/src/runtime/__tests__/server-source-maps.spec.ts diff --git a/packages/vite/src/node/ssr/runtime/__tests__/utils.ts b/packages/vite/src/runtime/__tests__/utils.ts similarity index 97% rename from packages/vite/src/node/ssr/runtime/__tests__/utils.ts rename to packages/vite/src/runtime/__tests__/utils.ts index a6d7ccb57480b3..b0a4875092e359 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/utils.ts +++ b/packages/vite/src/runtime/__tests__/utils.ts @@ -7,8 +7,8 @@ import type { InlineConfig, MainThreadRuntimeOptions, ViteDevServer, -} from '../../../index' -import { createServer } from '../../../index' +} from '../../node' +import { createServer } from '../../node' import type { ViteRuntime } from '../runtime' import { createViteRuntime } from '../node/mainThreadRuntime' diff --git a/packages/vite/src/node/ssr/runtime/constants.ts b/packages/vite/src/runtime/constants.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/constants.ts rename to packages/vite/src/runtime/constants.ts diff --git a/packages/vite/src/node/ssr/runtime/esmRunner.ts b/packages/vite/src/runtime/esmRunner.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/esmRunner.ts rename to packages/vite/src/runtime/esmRunner.ts diff --git a/packages/vite/src/node/ssr/runtime/hmrHandler.ts b/packages/vite/src/runtime/hmrHandler.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/hmrHandler.ts rename to packages/vite/src/runtime/hmrHandler.ts diff --git a/packages/vite/src/node/ssr/runtime/hmrLogger.ts b/packages/vite/src/runtime/hmrLogger.ts similarity index 66% rename from packages/vite/src/node/ssr/runtime/hmrLogger.ts rename to packages/vite/src/runtime/hmrLogger.ts index 4fc83dba7a4a6a..57325298949e09 100644 --- a/packages/vite/src/node/ssr/runtime/hmrLogger.ts +++ b/packages/vite/src/runtime/hmrLogger.ts @@ -1,4 +1,4 @@ -import type { HMRLogger } from '../../../shared/hmr' +import type { HMRLogger } from '../shared/hmr' const noop = (): void => {} diff --git a/packages/vite/src/node/ssr/runtime/index.ts b/packages/vite/src/runtime/index.ts similarity index 89% rename from packages/vite/src/node/ssr/runtime/index.ts rename to packages/vite/src/runtime/index.ts index f2b5b83f0fda5d..ded7222e45d690 100644 --- a/packages/vite/src/node/ssr/runtime/index.ts +++ b/packages/vite/src/runtime/index.ts @@ -4,7 +4,7 @@ export { ModuleCacheMap } from './moduleCache' export { ViteRuntime } from './runtime' export { ESModulesRunner } from './esmRunner' -export type { HMRLogger, HMRConnection } from '../../../shared/hmr' +export type { HMRLogger, HMRConnection } from '../shared/hmr' export type { ViteModuleRunner, ViteRuntimeModuleContext, diff --git a/packages/vite/src/node/ssr/runtime/moduleCache.ts b/packages/vite/src/runtime/moduleCache.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/moduleCache.ts rename to packages/vite/src/runtime/moduleCache.ts diff --git a/packages/vite/src/node/ssr/runtime/runtime.ts b/packages/vite/src/runtime/runtime.ts similarity index 99% rename from packages/vite/src/node/ssr/runtime/runtime.ts rename to packages/vite/src/runtime/runtime.ts index 1c43eb713eea4f..b398b731f45334 100644 --- a/packages/vite/src/node/ssr/runtime/runtime.ts +++ b/packages/vite/src/runtime/runtime.ts @@ -1,5 +1,5 @@ import type { ViteHotContext } from 'types/hot' -import { HMRClient, HMRContext } from '../../../shared/hmr' +import { HMRClient, HMRContext } from '../shared/hmr' import { ModuleCacheMap } from './moduleCache' import type { FetchResult, diff --git a/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts b/packages/vite/src/runtime/sourcemap/decoder.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts rename to packages/vite/src/runtime/sourcemap/decoder.ts diff --git a/packages/vite/src/node/ssr/runtime/sourcemap/index.ts b/packages/vite/src/runtime/sourcemap/index.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/sourcemap/index.ts rename to packages/vite/src/runtime/sourcemap/index.ts diff --git a/packages/vite/src/node/ssr/runtime/sourcemap/interceptor.ts b/packages/vite/src/runtime/sourcemap/interceptor.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/sourcemap/interceptor.ts rename to packages/vite/src/runtime/sourcemap/interceptor.ts diff --git a/packages/vite/src/runtime/tsconfig.json b/packages/vite/src/runtime/tsconfig.json new file mode 100644 index 00000000000000..b664c0ea7a093f --- /dev/null +++ b/packages/vite/src/runtime/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["./", "../node", "../dep-types", "../types"], + "exclude": ["**/__tests__"], + "compilerOptions": { + "lib": ["ESNext", "DOM"], + "stripInternal": true + } +} diff --git a/packages/vite/src/node/ssr/runtime/types.ts b/packages/vite/src/runtime/types.ts similarity index 98% rename from packages/vite/src/node/ssr/runtime/types.ts rename to packages/vite/src/runtime/types.ts index da4ae441282e53..56707005f8db50 100644 --- a/packages/vite/src/node/ssr/runtime/types.ts +++ b/packages/vite/src/runtime/types.ts @@ -1,6 +1,6 @@ import type { ViteHotContext } from 'types/hot' import type { HMRPayload } from 'types/hmrPayload' -import type { HMRConnection, HMRLogger } from '../../../shared/hmr' +import type { HMRConnection, HMRLogger } from '../shared/hmr' import type { ModuleCacheMap } from './moduleCache' import type { ssrDynamicImportKey, diff --git a/packages/vite/src/node/ssr/runtime/utils.ts b/packages/vite/src/runtime/utils.ts similarity index 100% rename from packages/vite/src/node/ssr/runtime/utils.ts rename to packages/vite/src/runtime/utils.ts diff --git a/packages/vite/tsconfig.json b/packages/vite/tsconfig.json index afcebbe0e7b69a..288eefaaa299ce 100644 --- a/packages/vite/tsconfig.json +++ b/packages/vite/tsconfig.json @@ -1,11 +1,6 @@ { "extends": "./tsconfig.base.json", - "include": [ - "./rollup.config.ts", - "./rollup.dts.config.ts", - "src/node/__tests__", - "src/types/shims.d.ts" - ], + "include": ["./rollup.config.ts", "./rollup.dts.config.ts"], "compilerOptions": { "esModuleInterop": true, "declaration": false, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 69da503d0b7b53..91a42baf7593ed 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -447,7 +447,7 @@ importers: packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep: {} - packages/vite/src/node/ssr/runtime/__tests__: + packages/vite/src/runtime/__tests__: dependencies: '@vitejs/cjs-external': specifier: link:./fixtures/cjs-external @@ -459,9 +459,9 @@ importers: specifier: 2.2.0 version: 2.2.0 - packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external: {} + packages/vite/src/runtime/__tests__/fixtures/cjs-external: {} - packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external: {} + packages/vite/src/runtime/__tests__/fixtures/esm-external: {} playground: devDependencies: diff --git a/vitest.config.ts b/vitest.config.ts index b74c08a7681a2c..2802969bc155c2 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,5 +1,9 @@ +import path from 'node:path' +import url from 'node:url' import { defineConfig } from 'vitest/config' +const _dirname = path.dirname(url.fileURLToPath(import.meta.url)) + export default defineConfig({ test: { include: ['**/__tests__/**/*.spec.[tj]s'], @@ -15,4 +19,12 @@ export default defineConfig({ target: 'node18', }, publicDir: false, + resolve: { + alias: { + 'vite/runtime': path.resolve( + _dirname, + './packages/vite/src/runtime/index.ts', + ), + }, + }, }) From c44a17369000891a66e230dd603a3a7618c1d17f Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 8 Feb 2024 22:38:07 +0900 Subject: [PATCH 03/11] refactor: make shared utils/constants --- .../plugins/dynamicImportVar/parse.spec.ts | 3 +- .../vite/src/node/__tests__/utils.spec.ts | 2 +- packages/vite/src/node/build.ts | 10 ++--- packages/vite/src/node/cli.ts | 2 +- packages/vite/src/node/config.ts | 18 ++++----- packages/vite/src/node/constants.ts | 18 --------- packages/vite/src/node/optimizer/index.ts | 2 +- packages/vite/src/node/optimizer/resolve.ts | 3 +- packages/vite/src/node/optimizer/scan.ts | 2 +- packages/vite/src/node/plugins/asset.ts | 2 +- .../src/node/plugins/assetImportMetaUrl.ts | 8 +--- packages/vite/src/node/plugins/css.ts | 3 +- packages/vite/src/node/plugins/esbuild.ts | 2 +- packages/vite/src/node/plugins/html.ts | 2 +- .../vite/src/node/plugins/importAnalysis.ts | 4 +- .../vite/src/node/plugins/importMetaGlob.ts | 8 +--- .../vite/src/node/plugins/loadFallback.ts | 2 +- .../vite/src/node/plugins/optimizedDeps.ts | 3 +- packages/vite/src/node/plugins/preAlias.ts | 2 +- packages/vite/src/node/plugins/resolve.ts | 4 +- packages/vite/src/node/plugins/worker.ts | 3 +- .../src/node/plugins/workerImportMetaUrl.ts | 9 +---- packages/vite/src/node/preview.ts | 2 +- packages/vite/src/node/publicDir.ts | 2 +- packages/vite/src/node/server/hmr.ts | 2 +- .../vite/src/node/server/middlewares/base.ts | 8 +--- .../node/server/middlewares/htmlFallback.ts | 3 +- .../src/node/server/middlewares/indexHtml.ts | 4 +- .../src/node/server/middlewares/static.ts | 4 +- .../src/node/server/middlewares/transform.ts | 10 ++--- packages/vite/src/node/server/moduleGraph.ts | 2 +- .../vite/src/node/server/pluginContainer.ts | 3 +- .../vite/src/node/server/transformRequest.ts | 3 +- packages/vite/src/node/ssr/fetchModule.ts | 3 +- packages/vite/src/node/ssr/ssrModuleLoader.ts | 3 +- packages/vite/src/node/tsconfig.json | 2 +- packages/vite/src/node/utils.ts | 38 +------------------ .../runtime/__tests__/server-runtime.spec.ts | 2 +- packages/vite/src/runtime/hmrHandler.ts | 2 +- packages/vite/src/runtime/moduleCache.ts | 3 +- packages/vite/src/runtime/runtime.ts | 12 +++--- packages/vite/src/runtime/utils.ts | 33 +--------------- packages/vite/src/shared/constants.ts | 17 +++++++++ packages/vite/src/shared/utils.ts | 38 +++++++++++++++++++ 44 files changed, 129 insertions(+), 179 deletions(-) create mode 100644 packages/vite/src/shared/constants.ts create mode 100644 packages/vite/src/shared/utils.ts diff --git a/packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts b/packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts index dff448d54eb8fb..763e1027711e5b 100644 --- a/packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts @@ -2,7 +2,8 @@ import { resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' import { transformDynamicImport } from '../../../plugins/dynamicImportVars' -import { isWindows, normalizePath } from '../../../utils' +import { normalizePath } from '../../../utils' +import { isWindows } from '../../../../shared/utils' const __dirname = resolve(fileURLToPath(import.meta.url), '..') diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 3efdbdd77c2409..c89abcf03c4c91 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -10,11 +10,11 @@ import { getLocalhostAddressIfDiffersFromDNS, injectQuery, isFileReadable, - isWindows, posToNumber, processSrcSetSync, resolveHostname, } from '../utils' +import { isWindows } from '../../shared/utils' describe('bareImportRE', () => { test('should work with normal package name', () => { diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index f7e3ae94a34da6..e7c1a69e157161 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -21,6 +21,11 @@ import commonjsPlugin from '@rollup/plugin-commonjs' import type { RollupCommonJSOptions } from 'dep-types/commonjs' import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars' import type { TransformOptions } from 'esbuild' +import { + DEFAULT_ASSETS_INLINE_LIMIT, + ESBUILD_MODULES_TARGET, + VERSION, +} from './constants' import type { InlineConfig, ResolvedConfig } from './config' import { resolveConfig } from './config' import { buildReporterPlugin } from './plugins/reporter' @@ -45,11 +50,6 @@ import { ssrManifestPlugin } from './ssr/ssrManifestPlugin' import { loadFallbackPlugin } from './plugins/loadFallback' import { findNearestPackageData } from './packages' import type { PackageCache } from './packages' -import { - DEFAULT_ASSETS_INLINE_LIMIT, - ESBUILD_MODULES_TARGET, - VERSION, -} from './constants' import { resolveChokidarOptions } from './watch' import { completeSystemWrapPlugin } from './plugins/completeSystemWrap' import { mergeConfig } from './publicUtils' diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index 08963f29081412..f0fa2092110175 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -3,12 +3,12 @@ import fs from 'node:fs' import { performance } from 'node:perf_hooks' import { cac } from 'cac' import colors from 'picocolors' +import { VERSION } from './constants' import type { BuildOptions } from './build' import type { ServerOptions } from './server' import type { CLIShortcut } from './shortcuts' import type { LogLevel } from './logger' import { createLogger } from './logger' -import { VERSION } from './constants' import { resolveConfig } from './config' const cli = cac('vite') diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index e236c97541a3f5..b7985e984dd1e5 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -10,6 +10,15 @@ import type { Alias, AliasOptions } from 'dep-types/alias' import aliasPlugin from '@rollup/plugin-alias' import { build } from 'esbuild' import type { RollupOptions } from 'rollup' +import { + CLIENT_ENTRY, + DEFAULT_ASSETS_RE, + DEFAULT_CONFIG_FILES, + DEFAULT_EXTENSIONS, + DEFAULT_MAIN_FIELDS, + ENV_ENTRY, + FS_PREFIX, +} from './constants' import type { HookHandler, Plugin, PluginWithRequiredHook } from './plugin' import type { BuildOptions, @@ -49,15 +58,6 @@ import { resolvePlugins, } from './plugins' import type { ESBuildOptions } from './plugins/esbuild' -import { - CLIENT_ENTRY, - DEFAULT_ASSETS_RE, - DEFAULT_CONFIG_FILES, - DEFAULT_EXTENSIONS, - DEFAULT_MAIN_FIELDS, - ENV_ENTRY, - FS_PREFIX, -} from './constants' import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve' import { resolvePlugin, tryNodeResolve } from './plugins/resolve' import type { LogLevel, Logger } from './logger' diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index 384b6694cd9cf5..f729f9d6162298 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -59,24 +59,6 @@ export const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/ */ export const FS_PREFIX = `/@fs/` -/** - * Prefix for resolved Ids that are not valid browser import specifiers - */ -export const VALID_ID_PREFIX = `/@id/` - -/** - * Plugins that use 'virtual modules' (e.g. for helper functions), prefix the - * module ID with `\0`, a convention from the rollup ecosystem. - * This prevents other plugins from trying to process the id (like node resolution), - * and core features like sourcemaps can use this info to differentiate between - * virtual modules and regular files. - * `\0` is not a permitted char in import URLs so we have to replace them during - * import analysis. The id will be decoded back before entering the plugins pipeline. - * These encoded virtual ids are also prefixed by the VALID_ID_PREFIX, so virtual - * modules in the browser end up encoded as `/@id/__x00__{id}` - */ -export const NULL_BYTE_PLACEHOLDER = `__x00__` - export const CLIENT_PUBLIC_PATH = `/@vite/client` export const ENV_PUBLIC_PATH = `/@vite/env` export const VITE_PACKAGE_DIR = resolve( diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 476a638ed0fd1b..b3f6533c86ad09 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -15,7 +15,6 @@ import { flattenId, getHash, isOptimizable, - isWindows, lookupFile, normalizeId, normalizePath, @@ -25,6 +24,7 @@ import { } from '../utils' import { transformWithEsbuild } from '../plugins/esbuild' import { ESBUILD_MODULES_TARGET, METADATA_FILENAME } from '../constants' +import { isWindows } from '../../shared/utils' import { esbuildCjsExternalPlugin, esbuildDepPlugin } from './esbuildDepPlugin' import { scanImports } from './scan' import { createOptimizeDepsIncludeResolver, expandGlobIds } from './resolve' diff --git a/packages/vite/src/node/optimizer/resolve.ts b/packages/vite/src/node/optimizer/resolve.ts index f03d69a5c3697e..aee532896a18d3 100644 --- a/packages/vite/src/node/optimizer/resolve.ts +++ b/packages/vite/src/node/optimizer/resolve.ts @@ -2,8 +2,9 @@ import path from 'node:path' import glob from 'fast-glob' import micromatch from 'micromatch' import type { ResolvedConfig } from '../config' -import { escapeRegex, getNpmPackageName, slash } from '../utils' +import { escapeRegex, getNpmPackageName } from '../utils' import { resolvePackageData } from '../packages' +import { slash } from '../../shared/utils' export function createOptimizeDepsIncludeResolver( config: ResolvedConfig, diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index b28c13167e5fb6..9b034cabf54952 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -20,7 +20,6 @@ import { SPECIAL_QUERY_RE, } from '../constants' import { - cleanUrl, createDebugger, dataUrlRE, externalRE, @@ -37,6 +36,7 @@ import { import type { PluginContainer } from '../server/pluginContainer' import { createPluginContainer } from '../server/pluginContainer' import { transformGlobImport } from '../plugins/importMetaGlob' +import { cleanUrl } from '../../shared/utils' type ResolveIdOptions = Parameters[2] diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 1b3a0cd752136a..8807fd8d1db49c 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -18,7 +18,6 @@ import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' import { checkPublicFile } from '../publicDir' import { - cleanUrl, getHash, injectQuery, joinUrlSegments, @@ -31,6 +30,7 @@ import { } from '../utils' import { DEFAULT_ASSETS_INLINE_LIMIT, FS_PREFIX } from '../constants' import type { ModuleGraph } from '../server/moduleGraph' +import { cleanUrl } from '../../shared/utils' // referenceId is base64url but replaces - with $ export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index c713c6b172e851..e307700adcc13e 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -4,13 +4,9 @@ import { stripLiteral } from 'strip-literal' import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' import type { ResolveFn } from '../' -import { - injectQuery, - isParentDirectory, - slash, - transformStableResult, -} from '../utils' +import { injectQuery, isParentDirectory, transformStableResult } from '../utils' import { CLIENT_ENTRY } from '../constants' +import { slash } from '../../shared/utils' import { fileToUrl } from './asset' import { preloadHelperId } from './importAnalysisBuild' import type { InternalResolveOptions } from './resolve' diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 83b8da61a37c45..f656c8c8aed4fd 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -46,7 +46,6 @@ import { checkPublicFile } from '../publicDir' import { arraify, asyncReplace, - cleanUrl, combineSourcemaps, createSerialPromiseQueue, emptyCssComments, @@ -63,12 +62,12 @@ import { removeDirectQuery, removeUrlQuery, requireResolveFromRootWithFallback, - slash, stripBase, stripBomTag, urlRE, } from '../utils' import type { Logger } from '../logger' +import { cleanUrl, slash } from '../../shared/utils' import { addToHTMLProxyTransformResult } from './html' import { assetUrlRE, diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index ea0c1604beac4a..6a1dafc4ea982d 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -12,7 +12,6 @@ import type { InternalModuleFormat, SourceMap } from 'rollup' import type { TSConfckParseResult } from 'tsconfck' import { TSConfckCache, TSConfckParseError, parse } from 'tsconfck' import { - cleanUrl, combineSourcemaps, createDebugger, createFilter, @@ -22,6 +21,7 @@ import { import type { ViteDevServer } from '../server' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' +import { cleanUrl } from '../../shared/utils' const debug = createDebugger('vite:esbuild') diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index afe39112ce0264..eb7973296af86b 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -13,7 +13,6 @@ import { stripLiteral } from 'strip-literal' import type { Plugin } from '../plugin' import type { ViteDevServer } from '../server' import { - cleanUrl, generateCodeFrame, getHash, isDataUrl, @@ -28,6 +27,7 @@ import { checkPublicFile } from '../publicDir' import { toOutputFilePathInHtml } from '../build' import { resolveEnvPrefix } from '../env' import type { Logger } from '../logger' +import { cleanUrl } from '../../shared/utils' import { assetUrlRE, getPublicAssetFilename, diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 57d1d5664c96bc..8b79d15909b3d6 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -27,7 +27,6 @@ import { normalizeHmrUrl, } from '../server/hmr' import { - cleanUrl, createDebugger, fsPathFromUrl, generateCodeFrame, @@ -48,10 +47,8 @@ import { stripBomTag, timeFrom, transformStableResult, - unwrapId, urlRE, withTrailingSlash, - wrapId, } from '../utils' import { getFsUtils } from '../fsUtils' import { checkPublicFile } from '../publicDir' @@ -60,6 +57,7 @@ import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import { shouldExternalizeForSSR } from '../ssr/ssrExternal' import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer' +import { cleanUrl, unwrapId, wrapId } from '../../shared/utils' import { throwOutdatedRequest } from './optimizedDeps' import { isCSSRequest, isDirectCSSRequest } from './css' import { browserExternalId } from './resolve' diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index f8981b9b253c08..413c60f785a514 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -24,13 +24,9 @@ import type { Plugin } from '../plugin' import type { ViteDevServer } from '../server' import type { ModuleNode } from '../server/moduleGraph' import type { ResolvedConfig } from '../config' -import { - evalValue, - normalizePath, - slash, - transformStableResult, -} from '../utils' +import { evalValue, normalizePath, transformStableResult } from '../utils' import type { Logger } from '../logger' +import { slash } from '../../shared/utils' const { isMatch, scan } = micromatch diff --git a/packages/vite/src/node/plugins/loadFallback.ts b/packages/vite/src/node/plugins/loadFallback.ts index 9274622dff5528..7d56797e48e681 100644 --- a/packages/vite/src/node/plugins/loadFallback.ts +++ b/packages/vite/src/node/plugins/loadFallback.ts @@ -1,6 +1,6 @@ import fsp from 'node:fs/promises' import type { Plugin } from '..' -import { cleanUrl } from '../utils' +import { cleanUrl } from '../../shared/utils' /** * A plugin to provide build load fallback for arbitrary request with queries. diff --git a/packages/vite/src/node/plugins/optimizedDeps.ts b/packages/vite/src/node/plugins/optimizedDeps.ts index ae44dd8ecd4cd8..cdde20891b836b 100644 --- a/packages/vite/src/node/plugins/optimizedDeps.ts +++ b/packages/vite/src/node/plugins/optimizedDeps.ts @@ -3,8 +3,9 @@ import colors from 'picocolors' import type { ResolvedConfig } from '..' import type { Plugin } from '../plugin' import { DEP_VERSION_RE } from '../constants' -import { cleanUrl, createDebugger } from '../utils' +import { createDebugger } from '../utils' import { getDepsOptimizer, optimizedDepInfoFromFile } from '../optimizer' +import { cleanUrl } from '../../shared/utils' export const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR = 'ERR_OPTIMIZE_DEPS_PROCESSING_ERROR' diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index d80a0a3429a5ee..45a86bce049291 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -9,7 +9,6 @@ import type { Plugin } from '../plugin' import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal' import { bareImportRE, - cleanUrl, isInNodeModules, isOptimizable, moduleListContains, @@ -17,6 +16,7 @@ import { } from '../utils' import { getFsUtils } from '../fsUtils' import { getDepsOptimizer } from '../optimizer' +import { cleanUrl } from '../../shared/utils' import { tryOptimizedResolve } from './resolve' /** diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 649a402eb95acf..c4dfde05e8a740 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -17,7 +17,6 @@ import { } from '../constants' import { bareImportRE, - cleanUrl, createDebugger, deepImportRE, fsPathFromId, @@ -32,10 +31,8 @@ import { isObject, isOptimizable, isTsRequest, - isWindows, normalizePath, safeRealpathSync, - slash, tryStatSync, withTrailingSlash, } from '../utils' @@ -51,6 +48,7 @@ import { loadPackageData, resolvePackageData, } from '../packages' +import { cleanUrl, isWindows, slash } from '../../shared/utils' const normalizedClientEntry = normalizePath(CLIENT_ENTRY) const normalizedEnvEntry = normalizePath(ENV_ENTRY) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 522cb7eb16221e..9749f6cd006391 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -5,12 +5,13 @@ import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import type { ViteDevServer } from '../server' import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants' -import { cleanUrl, getHash, injectQuery, urlRE } from '../utils' +import { getHash, injectQuery, urlRE } from '../utils' import { createToImportMetaURLBasedRelativeRuntime, onRollupWarning, toOutputFilePathInJS, } from '../build' +import { cleanUrl } from '../../shared/utils' import { fileToUrl } from './asset' interface WorkerCache { diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index c015dd91068d60..4460c71cf3e836 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -4,14 +4,9 @@ import type { RollupError } from 'rollup' import { stripLiteral } from 'strip-literal' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' -import { - cleanUrl, - evalValue, - injectQuery, - slash, - transformStableResult, -} from '../utils' +import { evalValue, injectQuery, transformStableResult } from '../utils' import type { ResolveFn } from '..' +import { cleanUrl, slash } from '../../shared/utils' import type { WorkerType } from './worker' import { WORKER_FILE_ID, workerFileToUrl } from './worker' import { fileToUrl } from './asset' diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index bad5c6b65e9b73..74a4728529b55a 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -4,6 +4,7 @@ import sirv from 'sirv' import connect from 'connect' import type { Connect } from 'dep-types/connect' import corsMiddleware from 'cors' +import { DEFAULT_PREVIEW_PORT } from './constants' import type { HttpServer, ResolvedServerOptions, @@ -28,7 +29,6 @@ import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils' import { printServerUrls } from './logger' import { bindCLIShortcuts } from './shortcuts' import type { BindCLIShortcutsOptions } from './shortcuts' -import { DEFAULT_PREVIEW_PORT } from './constants' import { resolveConfig } from './config' import type { InlineConfig, ResolvedConfig } from './config' diff --git a/packages/vite/src/node/publicDir.ts b/packages/vite/src/node/publicDir.ts index 7e69016c0e4650..88b9eeb7171c6f 100644 --- a/packages/vite/src/node/publicDir.ts +++ b/packages/vite/src/node/publicDir.ts @@ -1,9 +1,9 @@ import fs from 'node:fs' import path from 'node:path' +import { cleanUrl } from '../shared/utils' import type { ResolvedConfig } from './config' import { ERR_SYMLINK_IN_RECURSIVE_READDIR, - cleanUrl, normalizePath, recursiveReaddir, withTrailingSlash, diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index a87d902ae0a35d..b5bbc781ab25c5 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -11,13 +11,13 @@ import { normalizePath, unique, withTrailingSlash, - wrapId, } from '../utils' import type { InferCustomEventPayload, ViteDevServer } from '..' import { isCSSRequest } from '../plugins/css' import { getAffectedGlobModules } from '../plugins/importMetaGlob' import { isExplicitImportRequired } from '../plugins/importAnalysis' import { getEnvFilesForMode } from '../env' +import { wrapId } from '../../shared/utils' import type { ModuleNode } from './moduleGraph' import { restartServerWithUrls } from '.' diff --git a/packages/vite/src/node/server/middlewares/base.ts b/packages/vite/src/node/server/middlewares/base.ts index a6fab33b08219c..cda40feba7b03e 100644 --- a/packages/vite/src/node/server/middlewares/base.ts +++ b/packages/vite/src/node/server/middlewares/base.ts @@ -1,10 +1,6 @@ import type { Connect } from 'dep-types/connect' -import { - cleanUrl, - joinUrlSegments, - stripBase, - withTrailingSlash, -} from '../../utils' +import { joinUrlSegments, stripBase, withTrailingSlash } from '../../utils' +import { cleanUrl } from '../../../shared/utils' // this middleware is only active when (base !== '/') diff --git a/packages/vite/src/node/server/middlewares/htmlFallback.ts b/packages/vite/src/node/server/middlewares/htmlFallback.ts index d073cbc78f2103..0814b697f95d9a 100644 --- a/packages/vite/src/node/server/middlewares/htmlFallback.ts +++ b/packages/vite/src/node/server/middlewares/htmlFallback.ts @@ -1,8 +1,9 @@ import path from 'node:path' import type { Connect } from 'dep-types/connect' -import { cleanUrl, createDebugger } from '../../utils' +import { createDebugger } from '../../utils' import type { FsUtils } from '../../fsUtils' import { commonFsUtils } from '../../fsUtils' +import { cleanUrl } from '../../../shared/utils' const debug = createDebugger('vite:html-fallback') diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 8d746d60286c9f..d9232b9d629e72 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -26,7 +26,6 @@ import type { PreviewServer, ResolvedConfig, ViteDevServer } from '../..' import { send } from '../send' import { CLIENT_PUBLIC_PATH, FS_PREFIX } from '../../constants' import { - cleanUrl, ensureWatchedFile, fsPathFromId, getHash, @@ -37,13 +36,12 @@ import { normalizePath, processSrcSetSync, stripBase, - unwrapId, - wrapId, } from '../../utils' import { getFsUtils } from '../../fsUtils' import { checkPublicFile } from '../../publicDir' import { isCSSRequest } from '../../plugins/css' import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap' +import { cleanUrl, unwrapId, wrapId } from '../../../shared/utils' interface AssetNode { start: number diff --git a/packages/vite/src/node/server/middlewares/static.ts b/packages/vite/src/node/server/middlewares/static.ts index 8c94e53dd0a7c3..58ccd5e84fd783 100644 --- a/packages/vite/src/node/server/middlewares/static.ts +++ b/packages/vite/src/node/server/middlewares/static.ts @@ -7,7 +7,6 @@ import escapeHtml from 'escape-html' import type { ViteDevServer } from '../..' import { FS_PREFIX } from '../../constants' import { - cleanUrl, fsPathFromId, fsPathFromUrl, isFileReadable, @@ -15,12 +14,11 @@ import { isInternalRequest, isParentDirectory, isSameFileUri, - isWindows, normalizePath, removeLeadingSlash, - slash, withTrailingSlash, } from '../../utils' +import { cleanUrl, isWindows, slash } from '../../../shared/utils' const knownJavascriptExtensionRE = /\.[tj]sx?$/ diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index d335e598a6f358..ca4313c4a86667 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -5,7 +5,6 @@ import colors from 'picocolors' import type { ExistingRawSourceMap } from 'rollup' import type { ViteDevServer } from '..' import { - cleanUrl, createDebugger, fsPathFromId, injectQuery, @@ -15,7 +14,6 @@ import { prettifyUrl, removeImportQuery, removeTimestampQuery, - unwrapId, urlRE, withTrailingSlash, } from '../../utils' @@ -23,11 +21,7 @@ import { send } from '../send' import { ERR_LOAD_URL, transformRequest } from '../transformRequest' import { applySourcemapIgnoreList } from '../sourcemap' import { isHTMLProxy } from '../../plugins/html' -import { - DEP_VERSION_RE, - FS_PREFIX, - NULL_BYTE_PLACEHOLDER, -} from '../../constants' +import { DEP_VERSION_RE, FS_PREFIX } from '../../constants' import { isCSSRequest, isDirectCSSRequest, @@ -39,6 +33,8 @@ import { } from '../../plugins/optimizedDeps' import { ERR_CLOSED_SERVER } from '../pluginContainer' import { getDepsOptimizer } from '../../optimizer' +import { cleanUrl, unwrapId } from '../../../shared/utils' +import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants' const debugCache = createDebugger('vite:cache') diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index 40efa65f32b67e..ac139f06fd112a 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -2,12 +2,12 @@ import { extname } from 'node:path' import type { ModuleInfo, PartialResolvedId } from 'rollup' import { isDirectCSSRequest } from '../plugins/css' import { - cleanUrl, normalizePath, removeImportQuery, removeTimestampQuery, } from '../utils' import { FS_PREFIX } from '../constants' +import { cleanUrl } from '../../shared/utils' import type { TransformResult } from './transformRequest' export class ModuleNode { diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 551b210418363d..4ea3d0e6b51ea0 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -63,7 +63,6 @@ import type { FSWatcher } from 'chokidar' import colors from 'picocolors' import type { Plugin } from '../plugin' import { - cleanUrl, combineSourcemaps, createDebugger, ensureWatchedFile, @@ -75,11 +74,11 @@ import { prettifyUrl, rollupVersion, timeFrom, - unwrapId, } from '../utils' import { FS_PREFIX } from '../constants' import type { ResolvedConfig } from '../config' import { createPluginHookUtils, getHookHandler } from '../plugins' +import { cleanUrl, unwrapId } from '../../shared/utils' import { buildErrorMessage } from './middlewares/error' import type { ModuleGraph, ModuleNode } from './moduleGraph' diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 54028fe3dffa95..2813128af5b1dd 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -10,7 +10,6 @@ import colors from 'picocolors' import type { ModuleNode, ViteDevServer } from '..' import { blankReplacer, - cleanUrl, createDebugger, ensureWatchedFile, injectQuery, @@ -20,11 +19,11 @@ import { removeTimestampQuery, stripBase, timeFrom, - unwrapId, } from '../utils' import { checkPublicFile } from '../publicDir' import { isDepsOptimizerEnabled } from '../config' import { getDepsOptimizer, initDevSsrDepsOptimizer } from '../optimizer' +import { cleanUrl, unwrapId } from '../../shared/utils' import { applySourcemapIgnoreList, injectSourcesContent } from './sourcemap' import { isFileServingAllowed } from './middlewares/static' import { throwClosedServerError } from './pluginContainer' diff --git a/packages/vite/src/node/ssr/fetchModule.ts b/packages/vite/src/node/ssr/fetchModule.ts index 802b457292a5e8..9a01763d219cfe 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -3,8 +3,9 @@ import type { ModuleNode, TransformResult, ViteDevServer } from '..' import type { PackageCache } from '../packages' import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' -import { isBuiltin, isExternalUrl, isFilePathESM, unwrapId } from '../utils' +import { isBuiltin, isExternalUrl, isFilePathESM } from '../utils' import type { FetchResult } from '../../runtime/types' +import { unwrapId } from '../../shared/utils' interface NodeImportResolveOptions extends InternalResolveOptionsWithOverrideConditions { diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index e836d1e5f5f788..42f023adb4a82a 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -2,12 +2,13 @@ import path from 'node:path' import { pathToFileURL } from 'node:url' import colors from 'picocolors' import type { ViteDevServer } from '../server' -import { isBuiltin, isExternalUrl, isFilePathESM, unwrapId } from '../utils' +import { isBuiltin, isExternalUrl, isFilePathESM } from '../utils' import { transformRequest } from '../server/transformRequest' import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' import { genSourceMapUrl } from '../server/sourcemap' import type { PackageCache } from '../packages' +import { unwrapId } from '../../shared/utils' import { ssrDynamicImportKey, ssrExportAllKey, diff --git a/packages/vite/src/node/tsconfig.json b/packages/vite/src/node/tsconfig.json index bb2195ff6a9dd9..db8e56fa8449c5 100644 --- a/packages/vite/src/node/tsconfig.json +++ b/packages/vite/src/node/tsconfig.json @@ -1,6 +1,6 @@ { "extends": "../../tsconfig.base.json", - "include": ["./", "../runtime", "../dep-types", "../types"], + "include": ["./", "../runtime", "../dep-types", "../types", "constants.ts"], "exclude": ["../**/__tests__"], "compilerOptions": { "lib": ["ESNext", "DOM"], diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 6f8004566cb6f9..71a71fbe0f68ec 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -19,14 +19,14 @@ import type MagicString from 'magic-string' import type { TransformResult } from 'rollup' import { createFilter as _createFilter } from '@rollup/pluginutils' +import { cleanUrl, isWindows, slash } from '../shared/utils' +import { VALID_ID_PREFIX } from '../shared/constants' import { CLIENT_ENTRY, CLIENT_PUBLIC_PATH, ENV_PUBLIC_PATH, FS_PREFIX, - NULL_BYTE_PLACEHOLDER, OPTIMIZABLE_ENTRY_RE, - VALID_ID_PREFIX, loopbackHosts, wildcardHosts, } from './constants' @@ -55,31 +55,6 @@ export const createFilter = _createFilter as ( options?: { resolve?: string | false | null }, ) => (id: string | unknown) => boolean -const windowsSlashRE = /\\/g -export function slash(p: string): string { - return p.replace(windowsSlashRE, '/') -} - -/** - * Prepend `/@id/` and replace null byte so the id is URL-safe. - * This is prepended to resolved ids that are not valid browser - * import specifiers by the importAnalysis plugin. - */ -export function wrapId(id: string): string { - return id.startsWith(VALID_ID_PREFIX) - ? id - : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER) -} - -/** - * Undo {@link wrapId}'s `/@id/` and null byte replacements. - */ -export function unwrapId(id: string): string { - return id.startsWith(VALID_ID_PREFIX) - ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0') - : id -} - const replaceSlashOrColonRE = /[/:]/g const replaceDotRE = /\./g const replaceNestedIdRE = /(\s*>\s*)/g @@ -234,8 +209,6 @@ export const urlCanParse = export const isCaseInsensitiveFS = testCaseInsensitiveFS() -export const isWindows = os.platform() === 'win32' - const VOLUME_RE = /^[A-Z]:/i export function normalizePath(id: string): string { @@ -293,13 +266,6 @@ export function isSameFileUri(file1: string, file2: string): boolean { ) } -export const queryRE = /\?.*$/s - -const postfixRE = /[?#].*$/s -export function cleanUrl(url: string): string { - return url.replace(postfixRE, '') -} - export const externalRE = /^(https?:)?\/\// export const isExternalUrl = (url: string): boolean => externalRE.test(url) diff --git a/packages/vite/src/runtime/__tests__/server-runtime.spec.ts b/packages/vite/src/runtime/__tests__/server-runtime.spec.ts index 5dd45fba53fbcc..1a88372b85bdcc 100644 --- a/packages/vite/src/runtime/__tests__/server-runtime.spec.ts +++ b/packages/vite/src/runtime/__tests__/server-runtime.spec.ts @@ -2,7 +2,7 @@ import { existsSync, readdirSync } from 'node:fs' import { posix, win32 } from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect } from 'vitest' -import { isWindows } from '../utils' +import { isWindows } from '../../shared/utils' import { createViteRuntimeTester } from './utils' const _URL = URL diff --git a/packages/vite/src/runtime/hmrHandler.ts b/packages/vite/src/runtime/hmrHandler.ts index 413d355c2f51a0..dfdead2536321d 100644 --- a/packages/vite/src/runtime/hmrHandler.ts +++ b/packages/vite/src/runtime/hmrHandler.ts @@ -1,6 +1,6 @@ import type { HMRPayload } from 'types/hmrPayload' +import { unwrapId } from '../shared/utils' import type { ViteRuntime } from './runtime' -import { unwrapId } from './utils' // updates to HMR should go one after another. It is possible to trigger another update during the invalidation for example. export function createHMRHandler( diff --git a/packages/vite/src/runtime/moduleCache.ts b/packages/vite/src/runtime/moduleCache.ts index e40d1dd7316960..5de690b402a2bc 100644 --- a/packages/vite/src/runtime/moduleCache.ts +++ b/packages/vite/src/runtime/moduleCache.ts @@ -1,6 +1,7 @@ +import { isWindows } from '../shared/utils' +import { decodeBase64 } from './utils' import { DecodedMap } from './sourcemap/decoder' import type { ModuleCache } from './types' -import { decodeBase64, isWindows } from './utils' let SOURCEMAPPING_URL = 'sourceMa' SOURCEMAPPING_URL += 'ppingURL' diff --git a/packages/vite/src/runtime/runtime.ts b/packages/vite/src/runtime/runtime.ts index b398b731f45334..33fd5da3f1ae9e 100644 --- a/packages/vite/src/runtime/runtime.ts +++ b/packages/vite/src/runtime/runtime.ts @@ -1,5 +1,12 @@ import type { ViteHotContext } from 'types/hot' import { HMRClient, HMRContext } from '../shared/hmr' +import { + cleanUrl, + isPrimitive, + isWindows, + unwrapId, + wrapId, +} from '../shared/utils' import { ModuleCacheMap } from './moduleCache' import type { FetchResult, @@ -12,15 +19,10 @@ import type { ViteRuntimeOptions, } from './types' import { - cleanUrl, - isPrimitive, - isWindows, posixDirname, posixPathToFileHref, posixResolve, toWindowsPath, - unwrapId, - wrapId, } from './utils' import { ssrDynamicImportKey, diff --git a/packages/vite/src/runtime/utils.ts b/packages/vite/src/runtime/utils.ts index f2eca533b350fe..458dd5392ea98c 100644 --- a/packages/vite/src/runtime/utils.ts +++ b/packages/vite/src/runtime/utils.ts @@ -1,41 +1,10 @@ -export const isWindows = - typeof process !== 'undefined' && process.platform === 'win32' +import { isWindows, slash } from '../shared/utils' export const decodeBase64 = typeof atob !== 'undefined' ? atob : (str: string) => Buffer.from(str, 'base64').toString('utf-8') -// currently we copy this from '../../constants' - maybe we can inline it somewhow? -const NULL_BYTE_PLACEHOLDER = `__x00__` -const VALID_ID_PREFIX = `/@id/` - -export function wrapId(id: string): string { - return id.startsWith(VALID_ID_PREFIX) - ? id - : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER) -} - -export function unwrapId(id: string): string { - return id.startsWith(VALID_ID_PREFIX) - ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0') - : id -} - -const windowsSlashRE = /\\/g -export function slash(p: string): string { - return p.replace(windowsSlashRE, '/') -} - -const postfixRE = /[?#].*$/s -export function cleanUrl(url: string): string { - return url.replace(postfixRE, '') -} - -export function isPrimitive(value: unknown): boolean { - return !value || (typeof value !== 'object' && typeof value !== 'function') -} - const CHAR_FORWARD_SLASH = 47 const CHAR_BACKWARD_SLASH = 92 diff --git a/packages/vite/src/shared/constants.ts b/packages/vite/src/shared/constants.ts new file mode 100644 index 00000000000000..c51af5b3c42b98 --- /dev/null +++ b/packages/vite/src/shared/constants.ts @@ -0,0 +1,17 @@ +/** + * Prefix for resolved Ids that are not valid browser import specifiers + */ +export const VALID_ID_PREFIX = `/@id/` + +/** + * Plugins that use 'virtual modules' (e.g. for helper functions), prefix the + * module ID with `\0`, a convention from the rollup ecosystem. + * This prevents other plugins from trying to process the id (like node resolution), + * and core features like sourcemaps can use this info to differentiate between + * virtual modules and regular files. + * `\0` is not a permitted char in import URLs so we have to replace them during + * import analysis. The id will be decoded back before entering the plugins pipeline. + * These encoded virtual ids are also prefixed by the VALID_ID_PREFIX, so virtual + * modules in the browser end up encoded as `/@id/__x00__{id}` + */ +export const NULL_BYTE_PLACEHOLDER = `__x00__` diff --git a/packages/vite/src/shared/utils.ts b/packages/vite/src/shared/utils.ts new file mode 100644 index 00000000000000..8203ef0eba870d --- /dev/null +++ b/packages/vite/src/shared/utils.ts @@ -0,0 +1,38 @@ +import { NULL_BYTE_PLACEHOLDER, VALID_ID_PREFIX } from './constants' + +export const isWindows = + typeof process !== 'undefined' && process.platform === 'win32' + +/** + * Prepend `/@id/` and replace null byte so the id is URL-safe. + * This is prepended to resolved ids that are not valid browser + * import specifiers by the importAnalysis plugin. + */ +export function wrapId(id: string): string { + return id.startsWith(VALID_ID_PREFIX) + ? id + : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER) +} + +/** + * Undo {@link wrapId}'s `/@id/` and null byte replacements. + */ +export function unwrapId(id: string): string { + return id.startsWith(VALID_ID_PREFIX) + ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0') + : id +} + +const windowsSlashRE = /\\/g +export function slash(p: string): string { + return p.replace(windowsSlashRE, '/') +} + +const postfixRE = /[?#].*$/s +export function cleanUrl(url: string): string { + return url.replace(postfixRE, '') +} + +export function isPrimitive(value: unknown): boolean { + return !value || (typeof value !== 'object' && typeof value !== 'function') +} From 55a31a41b3c04d95f7f6437c320d69003b918aaf Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 13 Feb 2024 00:32:21 +0900 Subject: [PATCH 04/11] refactor: use shared constants --- packages/vite/src/node/ssr/fetchModule.ts | 14 +++++++------- packages/vite/src/runtime/moduleCache.ts | 5 +---- packages/vite/src/shared/constants.ts | 7 +++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/ssr/fetchModule.ts b/packages/vite/src/node/ssr/fetchModule.ts index 9a01763d219cfe..364a1d9c54f081 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -6,6 +6,11 @@ import { tryNodeResolve } from '../plugins/resolve' import { isBuiltin, isExternalUrl, isFilePathESM } from '../utils' import type { FetchResult } from '../../runtime/types' import { unwrapId } from '../../shared/utils' +import { + SOURCEMAPPING_URL, + VITE_RUNTIME_SOURCEMAPPING_SOURCE, + VITE_RUNTIME_SOURCEMAPPING_URL, +} from '../../shared/constants' interface NodeImportResolveOptions extends InternalResolveOptionsWithOverrideConditions { @@ -78,7 +83,8 @@ export async function fetchModule( throw err } const file = pathToFileURL(resolved.id).toString() - const type = isFilePathESM(file, server.config.packageCache) + // NOTE: fileURLだとキャッシュにヒットしないと思う + const type = isFilePathESM(resolved.id, server.config.packageCache) ? 'module' : 'commonjs' return { externalize: file, type } @@ -118,12 +124,6 @@ export async function fetchModule( return { code: result.code, file: mod.file } } -let SOURCEMAPPING_URL = 'sourceMa' -SOURCEMAPPING_URL += 'ppingURL' - -const VITE_RUNTIME_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime' -const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8` - function inlineSourceMap( mod: ModuleNode, result: TransformResult, diff --git a/packages/vite/src/runtime/moduleCache.ts b/packages/vite/src/runtime/moduleCache.ts index 5de690b402a2bc..cbf547ea1ff4b0 100644 --- a/packages/vite/src/runtime/moduleCache.ts +++ b/packages/vite/src/runtime/moduleCache.ts @@ -1,12 +1,9 @@ import { isWindows } from '../shared/utils' +import { VITE_RUNTIME_SOURCEMAPPING_URL } from '../shared/constants' import { decodeBase64 } from './utils' import { DecodedMap } from './sourcemap/decoder' import type { ModuleCache } from './types' -let SOURCEMAPPING_URL = 'sourceMa' -SOURCEMAPPING_URL += 'ppingURL' - -const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8` const VITE_RUNTIME_SOURCEMAPPING_REGEXP = new RegExp( `//# ${VITE_RUNTIME_SOURCEMAPPING_URL};base64,(.+)`, ) diff --git a/packages/vite/src/shared/constants.ts b/packages/vite/src/shared/constants.ts index c51af5b3c42b98..d60bbc9a1f8287 100644 --- a/packages/vite/src/shared/constants.ts +++ b/packages/vite/src/shared/constants.ts @@ -15,3 +15,10 @@ export const VALID_ID_PREFIX = `/@id/` * modules in the browser end up encoded as `/@id/__x00__{id}` */ export const NULL_BYTE_PLACEHOLDER = `__x00__` + +export let SOURCEMAPPING_URL = 'sourceMa' +SOURCEMAPPING_URL += 'ppingURL' + +export const VITE_RUNTIME_SOURCEMAPPING_SOURCE = + '//# sourceMappingSource=vite-runtime' +export const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8` From f30ccbf2610020f3a4452e11e6717391b7d37d97 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:38:23 +0900 Subject: [PATCH 05/11] refactor: share withTrailingSlash --- packages/vite/src/node/build.ts | 2 +- packages/vite/src/node/config.ts | 2 +- packages/vite/src/node/plugins/asset.ts | 3 +-- packages/vite/src/node/plugins/importAnalysis.ts | 8 ++++++-- packages/vite/src/node/plugins/preAlias.ts | 3 +-- packages/vite/src/node/plugins/reporter.ts | 8 ++------ packages/vite/src/node/plugins/resolve.ts | 8 ++++++-- packages/vite/src/node/publicDir.ts | 3 +-- packages/vite/src/node/server/hmr.ts | 9 ++------- packages/vite/src/node/server/middlewares/base.ts | 4 ++-- packages/vite/src/node/server/middlewares/static.ts | 8 ++++++-- packages/vite/src/node/server/middlewares/transform.ts | 3 +-- packages/vite/src/node/utils.ts | 9 +-------- packages/vite/src/runtime/moduleCache.ts | 9 +-------- packages/vite/src/shared/utils.ts | 7 +++++++ 15 files changed, 39 insertions(+), 47 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index e7c1a69e157161..a6888e97ffe5a7 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -21,6 +21,7 @@ import commonjsPlugin from '@rollup/plugin-commonjs' import type { RollupCommonJSOptions } from 'dep-types/commonjs' import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars' import type { TransformOptions } from 'esbuild' +import { withTrailingSlash } from '../shared/utils' import { DEFAULT_ASSETS_INLINE_LIMIT, ESBUILD_MODULES_TARGET, @@ -40,7 +41,6 @@ import { joinUrlSegments, normalizePath, requireResolveFromRootWithFallback, - withTrailingSlash, } from './utils' import { manifestPlugin } from './plugins/manifest' import type { Logger } from './logger' diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index b7985e984dd1e5..2e18cc8b30ba10 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -10,6 +10,7 @@ import type { Alias, AliasOptions } from 'dep-types/alias' import aliasPlugin from '@rollup/plugin-alias' import { build } from 'esbuild' import type { RollupOptions } from 'rollup' +import { withTrailingSlash } from '../shared/utils' import { CLIENT_ENTRY, DEFAULT_ASSETS_RE, @@ -48,7 +49,6 @@ import { mergeConfig, normalizeAlias, normalizePath, - withTrailingSlash, } from './utils' import { getFsUtils } from './fsUtils' import { diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 8807fd8d1db49c..ac18fb1772a4d6 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -26,11 +26,10 @@ import { removeLeadingSlash, removeUrlQuery, urlRE, - withTrailingSlash, } from '../utils' import { DEFAULT_ASSETS_INLINE_LIMIT, FS_PREFIX } from '../constants' import type { ModuleGraph } from '../server/moduleGraph' -import { cleanUrl } from '../../shared/utils' +import { cleanUrl, withTrailingSlash } from '../../shared/utils' // referenceId is base64url but replaces - with $ export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 8b79d15909b3d6..47a10cc5fe4d14 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -48,7 +48,6 @@ import { timeFrom, transformStableResult, urlRE, - withTrailingSlash, } from '../utils' import { getFsUtils } from '../fsUtils' import { checkPublicFile } from '../publicDir' @@ -57,7 +56,12 @@ import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import { shouldExternalizeForSSR } from '../ssr/ssrExternal' import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer' -import { cleanUrl, unwrapId, wrapId } from '../../shared/utils' +import { + cleanUrl, + unwrapId, + withTrailingSlash, + wrapId, +} from '../../shared/utils' import { throwOutdatedRequest } from './optimizedDeps' import { isCSSRequest, isDirectCSSRequest } from './css' import { browserExternalId } from './resolve' diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index 45a86bce049291..e88173061de122 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -12,11 +12,10 @@ import { isInNodeModules, isOptimizable, moduleListContains, - withTrailingSlash, } from '../utils' import { getFsUtils } from '../fsUtils' import { getDepsOptimizer } from '../optimizer' -import { cleanUrl } from '../../shared/utils' +import { cleanUrl, withTrailingSlash } from '../../shared/utils' import { tryOptimizedResolve } from './resolve' /** diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index d4f39aa5c4b8e4..285d9baa7daba2 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -4,13 +4,9 @@ import { promisify } from 'node:util' import colors from 'picocolors' import type { Plugin } from 'rollup' import type { ResolvedConfig } from '../config' -import { - isDefined, - isInNodeModules, - normalizePath, - withTrailingSlash, -} from '../utils' +import { isDefined, isInNodeModules, normalizePath } from '../utils' import { LogLevels } from '../logger' +import { withTrailingSlash } from '../../shared/utils' const groups = [ { name: 'Assets', color: colors.green }, diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c4dfde05e8a740..eee6ba0f92d742 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -34,7 +34,6 @@ import { normalizePath, safeRealpathSync, tryStatSync, - withTrailingSlash, } from '../utils' import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer' import type { DepsOptimizer } from '../optimizer' @@ -48,7 +47,12 @@ import { loadPackageData, resolvePackageData, } from '../packages' -import { cleanUrl, isWindows, slash } from '../../shared/utils' +import { + cleanUrl, + isWindows, + slash, + withTrailingSlash, +} from '../../shared/utils' const normalizedClientEntry = normalizePath(CLIENT_ENTRY) const normalizedEnvEntry = normalizePath(ENV_ENTRY) diff --git a/packages/vite/src/node/publicDir.ts b/packages/vite/src/node/publicDir.ts index 88b9eeb7171c6f..a72afd41445ab8 100644 --- a/packages/vite/src/node/publicDir.ts +++ b/packages/vite/src/node/publicDir.ts @@ -1,12 +1,11 @@ import fs from 'node:fs' import path from 'node:path' -import { cleanUrl } from '../shared/utils' +import { cleanUrl, withTrailingSlash } from '../shared/utils' import type { ResolvedConfig } from './config' import { ERR_SYMLINK_IN_RECURSIVE_READDIR, normalizePath, recursiveReaddir, - withTrailingSlash, } from './utils' const publicFilesMap = new WeakMap>() diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index b5bbc781ab25c5..77249de3c56d24 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -6,18 +6,13 @@ import colors from 'picocolors' import type { CustomPayload, HMRPayload, Update } from 'types/hmrPayload' import type { RollupError } from 'rollup' import { CLIENT_DIR } from '../constants' -import { - createDebugger, - normalizePath, - unique, - withTrailingSlash, -} from '../utils' +import { createDebugger, normalizePath, unique } from '../utils' import type { InferCustomEventPayload, ViteDevServer } from '..' import { isCSSRequest } from '../plugins/css' import { getAffectedGlobModules } from '../plugins/importMetaGlob' import { isExplicitImportRequired } from '../plugins/importAnalysis' import { getEnvFilesForMode } from '../env' -import { wrapId } from '../../shared/utils' +import { withTrailingSlash, wrapId } from '../../shared/utils' import type { ModuleNode } from './moduleGraph' import { restartServerWithUrls } from '.' diff --git a/packages/vite/src/node/server/middlewares/base.ts b/packages/vite/src/node/server/middlewares/base.ts index cda40feba7b03e..ed44b4c633a80b 100644 --- a/packages/vite/src/node/server/middlewares/base.ts +++ b/packages/vite/src/node/server/middlewares/base.ts @@ -1,6 +1,6 @@ import type { Connect } from 'dep-types/connect' -import { joinUrlSegments, stripBase, withTrailingSlash } from '../../utils' -import { cleanUrl } from '../../../shared/utils' +import { joinUrlSegments, stripBase } from '../../utils' +import { cleanUrl, withTrailingSlash } from '../../../shared/utils' // this middleware is only active when (base !== '/') diff --git a/packages/vite/src/node/server/middlewares/static.ts b/packages/vite/src/node/server/middlewares/static.ts index 58ccd5e84fd783..d706b3fa926fee 100644 --- a/packages/vite/src/node/server/middlewares/static.ts +++ b/packages/vite/src/node/server/middlewares/static.ts @@ -16,9 +16,13 @@ import { isSameFileUri, normalizePath, removeLeadingSlash, - withTrailingSlash, } from '../../utils' -import { cleanUrl, isWindows, slash } from '../../../shared/utils' +import { + cleanUrl, + isWindows, + slash, + withTrailingSlash, +} from '../../../shared/utils' const knownJavascriptExtensionRE = /\.[tj]sx?$/ diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index ca4313c4a86667..12e2e2248d3db7 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -15,7 +15,6 @@ import { removeImportQuery, removeTimestampQuery, urlRE, - withTrailingSlash, } from '../../utils' import { send } from '../send' import { ERR_LOAD_URL, transformRequest } from '../transformRequest' @@ -33,7 +32,7 @@ import { } from '../../plugins/optimizedDeps' import { ERR_CLOSED_SERVER } from '../pluginContainer' import { getDepsOptimizer } from '../../optimizer' -import { cleanUrl, unwrapId } from '../../../shared/utils' +import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils' import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants' const debugCache = createDebugger('vite:cache') diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 71a71fbe0f68ec..c7e982c567ab99 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -19,7 +19,7 @@ import type MagicString from 'magic-string' import type { TransformResult } from 'rollup' import { createFilter as _createFilter } from '@rollup/pluginutils' -import { cleanUrl, isWindows, slash } from '../shared/utils' +import { cleanUrl, isWindows, slash, withTrailingSlash } from '../shared/utils' import { VALID_ID_PREFIX } from '../shared/constants' import { CLIENT_ENTRY, @@ -226,13 +226,6 @@ export function fsPathFromUrl(url: string): string { return fsPathFromId(cleanUrl(url)) } -export function withTrailingSlash(path: string): string { - if (path[path.length - 1] !== '/') { - return `${path}/` - } - return path -} - /** * Check if dir is a parent of file * diff --git a/packages/vite/src/runtime/moduleCache.ts b/packages/vite/src/runtime/moduleCache.ts index cbf547ea1ff4b0..1a376707889fba 100644 --- a/packages/vite/src/runtime/moduleCache.ts +++ b/packages/vite/src/runtime/moduleCache.ts @@ -1,4 +1,4 @@ -import { isWindows } from '../shared/utils' +import { isWindows, withTrailingSlash } from '../shared/utils' import { VITE_RUNTIME_SOURCEMAPPING_URL } from '../shared/constants' import { decodeBase64 } from './utils' import { DecodedMap } from './sourcemap/decoder' @@ -115,13 +115,6 @@ export class ModuleCacheMap extends Map { } } -function withTrailingSlash(path: string): string { - if (path[path.length - 1] !== '/') { - return `${path}/` - } - return path -} - // unique id that is not available as "$bare_import" like "test" const prefixedBuiltins = new Set(['node:test']) diff --git a/packages/vite/src/shared/utils.ts b/packages/vite/src/shared/utils.ts index 8203ef0eba870d..b99ceed6183bae 100644 --- a/packages/vite/src/shared/utils.ts +++ b/packages/vite/src/shared/utils.ts @@ -36,3 +36,10 @@ export function cleanUrl(url: string): string { export function isPrimitive(value: unknown): boolean { return !value || (typeof value !== 'object' && typeof value !== 'function') } + +export function withTrailingSlash(path: string): string { + if (path[path.length - 1] !== '/') { + return `${path}/` + } + return path +} From 626b978e95525576845a9afd3b971943986abb90 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:37:54 +0900 Subject: [PATCH 06/11] test: move runtime test to src/node --- .eslintrc.cjs | 1 + .prettierignore | 1 + .../{ => node/ssr}/runtime/__tests__/fixtures/a.ts | 0 .../ssr}/runtime/__tests__/fixtures/assets.js | 0 .../__tests__/fixtures/assets/placeholder.mov | 0 .../__tests__/fixtures/assets/placeholder.png | 0 .../__tests__/fixtures/assets/placeholder.txt | 0 .../__tests__/fixtures/assets/placeholder.webp | 0 .../{ => node/ssr}/runtime/__tests__/fixtures/b.ts | 0 .../ssr}/runtime/__tests__/fixtures/basic.js | 0 .../{ => node/ssr}/runtime/__tests__/fixtures/c.ts | 0 .../__tests__/fixtures/circular/circular-a.js | 0 .../__tests__/fixtures/circular/circular-b.js | 0 .../__tests__/fixtures/circular/circular-index.js | 0 .../__tests__/fixtures/cjs-external-existing.js | 0 .../fixtures/cjs-external-non-existing.js | 0 .../__tests__/fixtures/cjs-external/index.cjs | 0 .../__tests__/fixtures/cjs-external/package.json | 0 .../{ => node/ssr}/runtime/__tests__/fixtures/d.ts | 0 .../runtime/__tests__/fixtures/dynamic-import.js | 0 .../__tests__/fixtures/esm-external-existing.js | 0 .../fixtures/esm-external-non-existing.js | 0 .../__tests__/fixtures/esm-external/index.mjs | 0 .../__tests__/fixtures/esm-external/package.json | 0 .../ssr}/runtime/__tests__/fixtures/has-error.js | 0 .../ssr}/runtime/__tests__/fixtures/hmr.js | 0 .../ssr}/runtime/__tests__/fixtures/installed.js | 0 .../ssr}/runtime/__tests__/fixtures/native.js | 0 .../ssr}/runtime/__tests__/fixtures/simple.js | 0 .../ssr}/runtime/__tests__/fixtures/test.css | 0 .../runtime/__tests__/fixtures/test.module.css | 0 .../__tests__/fixtures/throws-error-method.ts | 0 .../ssr}/runtime/__tests__/fixtures/virtual.js | 0 .../{ => node/ssr}/runtime/__tests__/package.json | 0 .../ssr}/runtime/__tests__/server-hmr.spec.ts | 0 .../ssr}/runtime/__tests__/server-no-hmr.spec.ts | 0 .../ssr}/runtime/__tests__/server-runtime.spec.ts | 2 +- .../runtime/__tests__/server-source-maps.spec.ts | 2 +- .../src/{ => node/ssr}/runtime/__tests__/utils.ts | 14 ++++++-------- pnpm-lock.yaml | 6 +++--- 40 files changed, 13 insertions(+), 13 deletions(-) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/a.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/assets.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/assets/placeholder.mov (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/assets/placeholder.png (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/assets/placeholder.txt (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/assets/placeholder.webp (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/b.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/basic.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/c.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/circular/circular-a.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/circular/circular-b.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/circular/circular-index.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/cjs-external-existing.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/cjs-external-non-existing.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/cjs-external/index.cjs (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/cjs-external/package.json (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/d.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/dynamic-import.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/esm-external-existing.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/esm-external-non-existing.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/esm-external/index.mjs (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/esm-external/package.json (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/has-error.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/hmr.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/installed.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/native.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/simple.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/test.css (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/test.module.css (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/throws-error-method.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/fixtures/virtual.js (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/package.json (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/server-hmr.spec.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/server-no-hmr.spec.ts (100%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/server-runtime.spec.ts (99%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/server-source-maps.spec.ts (97%) rename packages/vite/src/{ => node/ssr}/runtime/__tests__/utils.ts (90%) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 1c0fa8976dc0ab..b3c31d519d30d5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -145,6 +145,7 @@ module.exports = defineConfig({ }, { files: ['packages/vite/src/node/**'], + excludedFiles: '**/__tests__/**', rules: { 'no-console': ['error'], }, diff --git a/.prettierignore b/.prettierignore index 1fafdb547deccc..0fc71ddd114101 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ packages/*/CHANGELOG.md +packages/vite/src/node/ssr/runtime/__tests__/fixtures playground-temp/ dist/ temp/ diff --git a/packages/vite/src/runtime/__tests__/fixtures/a.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/a.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/a.ts rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/a.ts diff --git a/packages/vite/src/runtime/__tests__/fixtures/assets.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/assets.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.mov b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.mov similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.mov rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.mov diff --git a/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.png b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.png similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.png rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.png diff --git a/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.txt b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.txt similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.txt rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.txt diff --git a/packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.webp b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.webp similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/assets/placeholder.webp rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/assets/placeholder.webp diff --git a/packages/vite/src/runtime/__tests__/fixtures/b.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/b.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/b.ts rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/b.ts diff --git a/packages/vite/src/runtime/__tests__/fixtures/basic.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/basic.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/basic.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/basic.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/c.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/c.ts rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts diff --git a/packages/vite/src/runtime/__tests__/fixtures/circular/circular-a.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-a.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/circular/circular-a.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-a.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/circular/circular-b.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-b.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/circular/circular-b.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-b.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/circular/circular-index.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-index.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/circular/circular-index.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/circular/circular-index.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/cjs-external-existing.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-existing.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/cjs-external-existing.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-existing.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/cjs-external-non-existing.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-non-existing.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/cjs-external-non-existing.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external-non-existing.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/cjs-external/index.cjs b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/index.cjs similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/cjs-external/index.cjs rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/index.cjs diff --git a/packages/vite/src/runtime/__tests__/fixtures/cjs-external/package.json b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/package.json similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/cjs-external/package.json rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external/package.json diff --git a/packages/vite/src/runtime/__tests__/fixtures/d.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/d.ts rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts diff --git a/packages/vite/src/runtime/__tests__/fixtures/dynamic-import.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/dynamic-import.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/dynamic-import.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/dynamic-import.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/esm-external-existing.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-existing.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/esm-external-existing.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-existing.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/esm-external-non-existing.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-non-existing.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/esm-external-non-existing.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external-non-existing.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/esm-external/index.mjs b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/index.mjs similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/esm-external/index.mjs rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/index.mjs diff --git a/packages/vite/src/runtime/__tests__/fixtures/esm-external/package.json b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/package.json similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/esm-external/package.json rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external/package.json diff --git a/packages/vite/src/runtime/__tests__/fixtures/has-error.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/has-error.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/has-error.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/hmr.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/hmr.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/hmr.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/hmr.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/installed.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/installed.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/installed.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/installed.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/native.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/native.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/native.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/native.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/simple.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/simple.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/simple.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/simple.js diff --git a/packages/vite/src/runtime/__tests__/fixtures/test.css b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.css similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/test.css rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.css diff --git a/packages/vite/src/runtime/__tests__/fixtures/test.module.css b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.module.css similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/test.module.css rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/test.module.css diff --git a/packages/vite/src/runtime/__tests__/fixtures/throws-error-method.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/throws-error-method.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/throws-error-method.ts rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/throws-error-method.ts diff --git a/packages/vite/src/runtime/__tests__/fixtures/virtual.js b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/virtual.js similarity index 100% rename from packages/vite/src/runtime/__tests__/fixtures/virtual.js rename to packages/vite/src/node/ssr/runtime/__tests__/fixtures/virtual.js diff --git a/packages/vite/src/runtime/__tests__/package.json b/packages/vite/src/node/ssr/runtime/__tests__/package.json similarity index 100% rename from packages/vite/src/runtime/__tests__/package.json rename to packages/vite/src/node/ssr/runtime/__tests__/package.json diff --git a/packages/vite/src/runtime/__tests__/server-hmr.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/server-hmr.spec.ts rename to packages/vite/src/node/ssr/runtime/__tests__/server-hmr.spec.ts diff --git a/packages/vite/src/runtime/__tests__/server-no-hmr.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-no-hmr.spec.ts similarity index 100% rename from packages/vite/src/runtime/__tests__/server-no-hmr.spec.ts rename to packages/vite/src/node/ssr/runtime/__tests__/server-no-hmr.spec.ts diff --git a/packages/vite/src/runtime/__tests__/server-runtime.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts similarity index 99% rename from packages/vite/src/runtime/__tests__/server-runtime.spec.ts rename to packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts index 1a88372b85bdcc..d788c02c0fd215 100644 --- a/packages/vite/src/runtime/__tests__/server-runtime.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts @@ -2,7 +2,7 @@ import { existsSync, readdirSync } from 'node:fs' import { posix, win32 } from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect } from 'vitest' -import { isWindows } from '../../shared/utils' +import { isWindows } from '../../../../shared/utils' import { createViteRuntimeTester } from './utils' const _URL = URL diff --git a/packages/vite/src/runtime/__tests__/server-source-maps.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts similarity index 97% rename from packages/vite/src/runtime/__tests__/server-source-maps.spec.ts rename to packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts index 15acfaec4990ad..c482a0464827f4 100644 --- a/packages/vite/src/runtime/__tests__/server-source-maps.spec.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest' -import type { ViteRuntime } from '../runtime' +import type { ViteRuntime } from 'vite/runtime' import { createViteRuntimeTester, editFile, resolvePath } from './utils' describe('vite-runtime initialization', async () => { diff --git a/packages/vite/src/runtime/__tests__/utils.ts b/packages/vite/src/node/ssr/runtime/__tests__/utils.ts similarity index 90% rename from packages/vite/src/runtime/__tests__/utils.ts rename to packages/vite/src/node/ssr/runtime/__tests__/utils.ts index b0a4875092e359..7e14bb986e828a 100644 --- a/packages/vite/src/runtime/__tests__/utils.ts +++ b/packages/vite/src/node/ssr/runtime/__tests__/utils.ts @@ -3,14 +3,12 @@ import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import type { TestAPI } from 'vitest' import { afterEach, beforeEach, test } from 'vitest' -import type { - InlineConfig, - MainThreadRuntimeOptions, - ViteDevServer, -} from '../../node' -import { createServer } from '../../node' -import type { ViteRuntime } from '../runtime' -import { createViteRuntime } from '../node/mainThreadRuntime' +import type { ViteRuntime } from 'vite/runtime' +import type { MainThreadRuntimeOptions } from '../mainThreadRuntime' +import type { ViteDevServer } from '../../../server' +import type { InlineConfig } from '../../../config' +import { createServer } from '../../../server' +import { createViteRuntime } from '../mainThreadRuntime' interface TestClient { server: ViteDevServer diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 91a42baf7593ed..69da503d0b7b53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -447,7 +447,7 @@ importers: packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep: {} - packages/vite/src/runtime/__tests__: + packages/vite/src/node/ssr/runtime/__tests__: dependencies: '@vitejs/cjs-external': specifier: link:./fixtures/cjs-external @@ -459,9 +459,9 @@ importers: specifier: 2.2.0 version: 2.2.0 - packages/vite/src/runtime/__tests__/fixtures/cjs-external: {} + packages/vite/src/node/ssr/runtime/__tests__/fixtures/cjs-external: {} - packages/vite/src/runtime/__tests__/fixtures/esm-external: {} + packages/vite/src/node/ssr/runtime/__tests__/fixtures/esm-external: {} playground: devDependencies: From 683c135b59ec8e9fc2117034762af2379ca0862c Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:39:48 +0900 Subject: [PATCH 07/11] chore: revert `isFilePathESM` change --- packages/vite/src/node/ssr/fetchModule.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/vite/src/node/ssr/fetchModule.ts b/packages/vite/src/node/ssr/fetchModule.ts index 364a1d9c54f081..aef39b5d047d85 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -83,8 +83,7 @@ export async function fetchModule( throw err } const file = pathToFileURL(resolved.id).toString() - // NOTE: fileURLだとキャッシュにヒットしないと思う - const type = isFilePathESM(resolved.id, server.config.packageCache) + const type = isFilePathESM(file, server.config.packageCache) ? 'module' : 'commonjs' return { externalize: file, type } From c135910db0b2b1bc4488ce8d026a575a70e0001f Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:49:10 +0900 Subject: [PATCH 08/11] fix: convert InvalidOriginalMapping to null --- packages/vite/src/runtime/sourcemap/decoder.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/runtime/sourcemap/decoder.ts b/packages/vite/src/runtime/sourcemap/decoder.ts index 8bbad42dab202e..6062ae76b09990 100644 --- a/packages/vite/src/runtime/sourcemap/decoder.ts +++ b/packages/vite/src/runtime/sourcemap/decoder.ts @@ -1,7 +1,4 @@ -import type { - InvalidOriginalMapping, - OriginalMapping, -} from '@jridgewell/trace-mapping' +import type { OriginalMapping } from '@jridgewell/trace-mapping' import { originalPositionFor } from '@jridgewell/trace-mapping' import { posixResolve } from '../utils' @@ -58,6 +55,10 @@ function memoizedState(): Stats { export function getOriginalPosition( map: DecodedMap, needle: Needle, -): OriginalMapping | InvalidOriginalMapping { - return originalPositionFor(map as any, needle) +): OriginalMapping | null { + const result = originalPositionFor(map as any, needle) + if (result.column == null) { + return null + } + return result } From 1932630ad93f026b7cf2af17b7e139acfd24a0eb Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:51:16 +0900 Subject: [PATCH 09/11] chore: bump max size --- packages/vite/rollup.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 7e9821301ccae5..59bca72769ed56 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -217,7 +217,7 @@ function createRuntimeConfig(isProduction: boolean) { } }, }, - bundleSizeLimit(43), + bundleSizeLimit(44), ], }) } From 4d322debc5f2d44aea8bdc3e87e40ffdbb46cdaf Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:08:37 +0900 Subject: [PATCH 10/11] refactor: bump @jridgewell/resolve-uri and remove patch --- package.json | 1 - patches/@jridgewell__resolve-uri@3.1.1.patch | 25 -------------------- pnpm-lock.yaml | 12 ++++------ 3 files changed, 4 insertions(+), 34 deletions(-) delete mode 100644 patches/@jridgewell__resolve-uri@3.1.1.patch diff --git a/package.json b/package.json index c76a7d0a2cf372..7b6d67ee69eb96 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,6 @@ } }, "patchedDependencies": { - "@jridgewell/resolve-uri@3.1.1": "patches/@jridgewell__resolve-uri@3.1.1.patch", "chokidar@3.6.0": "patches/chokidar@3.6.0.patch", "sirv@2.0.4": "patches/sirv@2.0.4.patch" }, diff --git a/patches/@jridgewell__resolve-uri@3.1.1.patch b/patches/@jridgewell__resolve-uri@3.1.1.patch deleted file mode 100644 index 7fe9a54a007502..00000000000000 --- a/patches/@jridgewell__resolve-uri@3.1.1.patch +++ /dev/null @@ -1,25 +0,0 @@ -diff --git a/dist/resolve-uri.mjs b/dist/resolve-uri.mjs -index 94d8dceb931e21c2a55058c1249eb34c2dddfbc9..c09b5f1af2a8118cf7c45959a441cbfe57386e9c 100644 ---- a/dist/resolve-uri.mjs -+++ b/dist/resolve-uri.mjs -@@ -21,8 +21,8 @@ const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^# - * 4. Hash, including "#", optional. - */ - const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i; --var UrlType; --(function (UrlType) { -+var UrlType = /* #__PURE__ */ (function () { -+ var UrlType = {}; - UrlType[UrlType["Empty"] = 1] = "Empty"; - UrlType[UrlType["Hash"] = 2] = "Hash"; - UrlType[UrlType["Query"] = 3] = "Query"; -@@ -30,7 +30,8 @@ var UrlType; - UrlType[UrlType["AbsolutePath"] = 5] = "AbsolutePath"; - UrlType[UrlType["SchemeRelative"] = 6] = "SchemeRelative"; - UrlType[UrlType["Absolute"] = 7] = "Absolute"; --})(UrlType || (UrlType = {})); -+ return UrlType; -+})(); - function isAbsoluteUrl(input) { - return schemeRegex.test(input); - } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 69da503d0b7b53..6afc4e86fe5718 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,9 +10,6 @@ overrides: packageExtensionsChecksum: 4e49e84b7a11dfd4895fbffb006a7015 patchedDependencies: - '@jridgewell/resolve-uri@3.1.1': - hash: w3ykihndrrld7utbtpy6utj3bu - path: patches/@jridgewell__resolve-uri@3.1.1.patch chokidar@3.6.0: hash: bckcfsslxcffppz65mxcq6naau path: patches/chokidar@3.6.0.patch @@ -3633,10 +3630,9 @@ packages: '@jridgewell/sourcemap-codec': 1.4.15 '@jridgewell/trace-mapping': 0.3.22 - /@jridgewell/resolve-uri@3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu): - resolution: {integrity: sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==} + /@jridgewell/resolve-uri@3.1.2: + resolution: {integrity: sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==} engines: {node: '>=6.0.0'} - patched: true /@jridgewell/set-array@1.1.2: resolution: {integrity: sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==} @@ -3655,13 +3651,13 @@ packages: /@jridgewell/trace-mapping@0.3.22: resolution: {integrity: sha512-Wf963MzWtA2sjrNt+g18IAln9lKnlRp+K2eH4jjIoF1wYeq3aMREpG09xhlhdzS0EjwU7qmUJYangWa+151vZw==} dependencies: - '@jridgewell/resolve-uri': 3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu) + '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.4.15 /@jridgewell/trace-mapping@0.3.9: resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} dependencies: - '@jridgewell/resolve-uri': 3.1.1(patch_hash=w3ykihndrrld7utbtpy6utj3bu) + '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.4.15 /@mapbox/node-pre-gyp@1.0.11: From 77361bbf0137a69bb0a38f2da97ef2ab186831a1 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:52:40 +0900 Subject: [PATCH 11/11] chore: bump max size --- packages/vite/rollup.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index f56358359e67ea..7a0a3305b6b794 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -217,7 +217,7 @@ function createRuntimeConfig(isProduction: boolean) { } }, }, - bundleSizeLimit(44), + bundleSizeLimit(45), ], }) }