From b20d54257e6105333c19676a403c574667878e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 24 Feb 2024 19:05:19 +0900 Subject: [PATCH] refactor: share code with vite runtime (#15907) --- .eslintrc.cjs | 1 + .prettierignore | 1 + packages/vite/package.json | 1 + packages/vite/rollup.config.ts | 49 +++- packages/vite/rollup.dts.config.ts | 4 +- .../plugins/dynamicImportVar/parse.spec.ts | 3 +- .../vite/src/node/__tests__/tsconfig.json | 8 + .../vite/src/node/__tests__/utils.spec.ts | 2 +- packages/vite/src/node/build.ts | 12 +- packages/vite/src/node/cli.ts | 2 +- packages/vite/src/node/config.ts | 20 +- packages/vite/src/node/constants.ts | 18 -- packages/vite/src/node/index.ts | 8 +- 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 | 3 +- .../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 | 10 +- .../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 | 3 +- packages/vite/src/node/plugins/reporter.ts | 8 +- packages/vite/src/node/plugins/resolve.ts | 10 +- 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 | 3 +- packages/vite/src/node/server/hmr.ts | 9 +- packages/vite/src/node/server/index.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 | 8 +- .../src/node/server/middlewares/transform.ts | 11 +- 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 | 16 +- .../node/ssr/runtime/__tests__/fixtures/c.ts | 2 +- .../node/ssr/runtime/__tests__/fixtures/d.ts | 2 +- .../runtime/__tests__/server-runtime.spec.ts | 2 +- .../__tests__/server-source-maps.spec.ts | 2 +- .../src/node/ssr/runtime/__tests__/utils.ts | 14 +- .../runtime/{node => }/mainThreadRuntime.ts | 9 +- .../runtime/{node => }/serverHmrConnector.ts | 9 +- .../src/node/ssr/runtime/sourcemap/decoder.ts | 260 ------------------ packages/vite/src/node/ssr/ssrFetchModule.ts | 2 +- packages/vite/src/node/ssr/ssrModuleLoader.ts | 3 +- packages/vite/src/node/tsconfig.json | 9 +- packages/vite/src/node/utils.ts | 45 +-- .../src/{node/ssr => }/runtime/constants.ts | 0 .../src/{node/ssr => }/runtime/esmRunner.ts | 0 .../src/{node/ssr => }/runtime/hmrHandler.ts | 2 +- .../src/{node/ssr => }/runtime/hmrLogger.ts | 2 +- .../vite/src/{node/ssr => }/runtime/index.ts | 2 +- .../src/{node/ssr => }/runtime/moduleCache.ts | 15 +- .../src/{node/ssr => }/runtime/runtime.ts | 14 +- .../vite/src/runtime/sourcemap/decoder.ts | 64 +++++ .../{node/ssr => }/runtime/sourcemap/index.ts | 0 .../ssr => }/runtime/sourcemap/interceptor.ts | 0 packages/vite/src/runtime/tsconfig.json | 9 + .../vite/src/{node/ssr => }/runtime/types.ts | 2 +- .../vite/src/{node/ssr => }/runtime/utils.ts | 33 +-- packages/vite/src/shared/constants.ts | 24 ++ packages/vite/src/shared/utils.ts | 45 +++ packages/vite/tsconfig.json | 7 +- pnpm-lock.yaml | 51 ++-- vitest.config.ts | 12 + 73 files changed, 372 insertions(+), 553 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%) delete mode 100644 packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts 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 (98%) 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 (93%) rename packages/vite/src/{node/ssr => }/runtime/runtime.ts (99%) create mode 100644 packages/vite/src/runtime/sourcemap/decoder.ts 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 (85%) create mode 100644 packages/vite/src/shared/constants.ts create mode 100644 packages/vite/src/shared/utils.ts 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/package.json b/packages/vite/package.json index 2f5809cc63737f..4b1f55ee50da90 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 d70779fa2dcd69..7a0a3305b6b794 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,13 +154,13 @@ 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, sourcemap: !isProduction, }, external: [ + /^vite\//, 'fsevents', 'lightningcss', 'rollup/parseAst', @@ -176,6 +177,51 @@ function createNodeConfig(isProduction: boolean) { }) } +function createRuntimeConfig(isProduction: boolean) { + return defineConfig({ + ...sharedNodeOptions, + input: { + runtime: path.resolve(__dirname, 'src/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(45), + ], + }) +} + function createCjsConfig(isProduction: boolean) { return defineConfig({ ...sharedNodeOptions, @@ -209,6 +255,7 @@ export default (commandLineArgs: any): RollupOptions[] => { envConfig, clientConfig, createNodeConfig(isProduction), + createRuntimeConfig(isProduction), createCjsConfig(isProduction), ]) } 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__/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__/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/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 35fdd3d3f97249..549371a2f395de 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..a6888e97ffe5a7 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -21,6 +21,12 @@ 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, + VERSION, +} from './constants' import type { InlineConfig, ResolvedConfig } from './config' import { resolveConfig } from './config' import { buildReporterPlugin } from './plugins/reporter' @@ -35,7 +41,6 @@ import { joinUrlSegments, normalizePath, requireResolveFromRootWithFallback, - withTrailingSlash, } from './utils' import { manifestPlugin } from './plugins/manifest' import type { Logger } from './logger' @@ -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..2e18cc8b30ba10 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -10,6 +10,16 @@ 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, + DEFAULT_CONFIG_FILES, + DEFAULT_EXTENSIONS, + DEFAULT_MAIN_FIELDS, + ENV_ENTRY, + FS_PREFIX, +} from './constants' import type { HookHandler, Plugin, PluginWithRequiredHook } from './plugin' import type { BuildOptions, @@ -39,7 +49,6 @@ import { mergeConfig, normalizeAlias, normalizePath, - withTrailingSlash, } from './utils' import { getFsUtils } from './fsUtils' import { @@ -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/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/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 68fc1f4db941cf..48b816bd23c81d 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -21,7 +21,6 @@ import { } from '../constants' import { arraify, - cleanUrl, createDebugger, dataUrlRE, externalRE, @@ -38,6 +37,7 @@ import { import type { PluginContainer } from '../server/pluginContainer' import { createPluginContainer } from '../server/pluginContainer' import { transformGlobImport } from '../plugins/importMetaGlob' +import { cleanUrl } from '../../shared/utils' import { loadTsconfigJsonForFile } from '../plugins/esbuild' type ResolveIdOptions = Parameters[2] diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 1b3a0cd752136a..ac18fb1772a4d6 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, @@ -27,10 +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, 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/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 015e7dc137c3b9..7076d0e6291c1b 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 0955f0f501bc19..cb3743395be4cd 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,7 @@ import { stripBomTag, timeFrom, transformStableResult, - unwrapId, urlRE, - withTrailingSlash, - wrapId, } from '../utils' import { getFsUtils } from '../fsUtils' import { checkPublicFile } from '../publicDir' @@ -60,6 +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, + 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/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..e88173061de122 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -9,14 +9,13 @@ import type { Plugin } from '../plugin' import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal' import { bareImportRE, - cleanUrl, isInNodeModules, isOptimizable, moduleListContains, - withTrailingSlash, } from '../utils' import { getFsUtils } from '../fsUtils' import { getDepsOptimizer } from '../optimizer' +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 649a402eb95acf..eee6ba0f92d742 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,12 +31,9 @@ import { isObject, isOptimizable, isTsRequest, - isWindows, normalizePath, safeRealpathSync, - slash, tryStatSync, - withTrailingSlash, } from '../utils' import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer' import type { DepsOptimizer } from '../optimizer' @@ -51,6 +47,12 @@ import { loadPackageData, resolvePackageData, } from '../packages' +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/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..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, withTrailingSlash } from '../shared/utils' import type { ResolvedConfig } from './config' import { ERR_SYMLINK_IN_RECURSIVE_READDIR, - cleanUrl, 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 ad25d8edc213f6..86bf9961bf9a72 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, - wrapId, -} 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 { withTrailingSlash, wrapId } from '../../shared/utils' import type { ModuleNode } from './moduleGraph' import { restartServerWithUrls } from '.' 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/server/middlewares/base.ts b/packages/vite/src/node/server/middlewares/base.ts index a6fab33b08219c..ed44b4c633a80b 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 } from '../../utils' +import { cleanUrl, withTrailingSlash } 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..d706b3fa926fee 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,15 @@ import { isInternalRequest, isParentDirectory, isSameFileUri, - isWindows, normalizePath, removeLeadingSlash, +} from '../../utils' +import { + cleanUrl, + isWindows, slash, withTrailingSlash, -} from '../../utils' +} 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 9cdbaf6ce1b44b..a0239aab7fcb4e 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,19 +14,13 @@ import { prettifyUrl, removeImportQuery, removeTimestampQuery, - unwrapId, urlRE, - withTrailingSlash, } from '../../utils' 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 +32,8 @@ import { } from '../../plugins/optimizedDeps' import { ERR_CLOSED_SERVER } from '../pluginContainer' import { getDepsOptimizer } from '../../optimizer' +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/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 c8dc57fcb38df1..ae66d3af596072 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -3,8 +3,14 @@ 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 type { FetchResult } from './runtime/types' +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 { @@ -117,12 +123,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` - const OTHER_SOURCE_MAP_REGEXP = new RegExp( `//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`, 'gm', diff --git a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts index d21d1b6f71e82a..86380486f9b21d 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/c.ts +++ b/packages/vite/src/node/ssr/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/d.ts b/packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts index d85309b8e7e7cb..fff83783443af8 100644 --- a/packages/vite/src/node/ssr/runtime/__tests__/fixtures/d.ts +++ b/packages/vite/src/node/ssr/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__/server-runtime.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts index a27ee8034523e6..bcf06bb91d4005 100644 --- a/packages/vite/src/node/ssr/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 '../utils' +import { isWindows } from '../../../../shared/utils' import { createViteRuntimeTester } from './utils' const _URL = URL diff --git a/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts b/packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts index 15acfaec4990ad..c482a0464827f4 100644 --- a/packages/vite/src/node/ssr/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/node/ssr/runtime/__tests__/utils.ts b/packages/vite/src/node/ssr/runtime/__tests__/utils.ts index a6d7ccb57480b3..7e14bb986e828a 100644 --- a/packages/vite/src/node/ssr/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 '../../../index' -import { createServer } from '../../../index' -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/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/runtime/sourcemap/decoder.ts b/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts deleted file mode 100644 index a3abcf9e388f98..00000000000000 --- a/packages/vite/src/node/ssr/runtime/sourcemap/decoder.ts +++ /dev/null @@ -1,260 +0,0 @@ -import { posixResolve } from '../utils' - -interface SourceMapLike { - version: number - mappings?: string - names?: string[] - sources?: string[] - sourcesContent?: string[] -} - -type OriginalMapping = { - source: string | null - line: number - column: number - name: string | null -} - -type Needle = { - line: number - column: number -} - -export class DecodedMap { - _encoded: string - _decoded: undefined | number[][][] - _decodedMemo: Stats - url: string - version: number - names: string[] = [] - resolvedSources: string[] - - constructor( - public map: SourceMapLike, - from: string, - ) { - const { mappings, names, sources } = map - this.version = map.version - this.names = names || [] - this._encoded = mappings || '' - this._decodedMemo = memoizedState() - this.url = from - this.resolvedSources = (sources || []).map((s) => - posixResolve(s || '', from), - ) - } -} - -// 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 - lastIndex: number -} -function memoizedState(): Stats { - return { - lastKey: -1, - lastNeedle: -1, - 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, - ) -} 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/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 a7f7890f1d0e7b..db8e56fa8449c5 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", "constants.ts"], + "exclude": ["../**/__tests__"], "compilerOptions": { "lib": ["ESNext", "DOM"], - "stripInternal": true + "stripInternal": true, + "paths": { + "vite/runtime": ["../runtime"] + } } } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 7fabe97b4253aa..c6ef88a4cb72fc 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, withTrailingSlash } 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 { @@ -253,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 * @@ -293,13 +259,6 @@ export function isSameFileUri(file1: string, file2: string): boolean { ) } -export const queryRE = /\?.*$/s - -const postfixRE = /[?#].*$/ -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/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 98% rename from packages/vite/src/node/ssr/runtime/hmrHandler.ts rename to packages/vite/src/runtime/hmrHandler.ts index ee59bb81dc8824..0b8363eac52dea 100644 --- a/packages/vite/src/node/ssr/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/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 93% rename from packages/vite/src/node/ssr/runtime/moduleCache.ts rename to packages/vite/src/runtime/moduleCache.ts index f7610401d066fb..750e933e0e303c 100644 --- a/packages/vite/src/node/ssr/runtime/moduleCache.ts +++ b/packages/vite/src/runtime/moduleCache.ts @@ -1,11 +1,9 @@ +import { isWindows, withTrailingSlash } 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' -import { decodeBase64, isWindows } from './utils' -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,(.+)`, ) @@ -168,13 +166,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/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 8abdd243770896..008dd214a37ce5 100644 --- a/packages/vite/src/node/ssr/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 { 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/sourcemap/decoder.ts b/packages/vite/src/runtime/sourcemap/decoder.ts new file mode 100644 index 00000000000000..6062ae76b09990 --- /dev/null +++ b/packages/vite/src/runtime/sourcemap/decoder.ts @@ -0,0 +1,64 @@ +import type { OriginalMapping } from '@jridgewell/trace-mapping' +import { originalPositionFor } from '@jridgewell/trace-mapping' +import { posixResolve } from '../utils' + +interface SourceMapLike { + version: number + mappings?: string + names?: string[] + sources?: string[] + sourcesContent?: string[] +} + +type Needle = { + line: number + column: number +} + +export class DecodedMap { + _encoded: string + _decoded: undefined | number[][][] + _decodedMemo: Stats + url: string + version: number + names: string[] = [] + resolvedSources: string[] + + constructor( + public map: SourceMapLike, + from: string, + ) { + const { mappings, names, sources } = map + this.version = map.version + this.names = names || [] + this._encoded = mappings || '' + this._decodedMemo = memoizedState() + this.url = from + this.resolvedSources = (sources || []).map((s) => + posixResolve(s || '', from), + ) + } +} + +interface Stats { + lastKey: number + lastNeedle: number + lastIndex: number +} +function memoizedState(): Stats { + return { + lastKey: -1, + lastNeedle: -1, + lastIndex: -1, + } +} +export function getOriginalPosition( + map: DecodedMap, + needle: Needle, +): OriginalMapping | null { + const result = originalPositionFor(map as any, needle) + if (result.column == null) { + return null + } + return result +} 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 85% rename from packages/vite/src/node/ssr/runtime/utils.ts rename to packages/vite/src/runtime/utils.ts index dc88390906d675..458dd5392ea98c 100644 --- a/packages/vite/src/node/ssr/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 = /[?#].*$/ -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..d60bbc9a1f8287 --- /dev/null +++ b/packages/vite/src/shared/constants.ts @@ -0,0 +1,24 @@ +/** + * 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 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` diff --git a/packages/vite/src/shared/utils.ts b/packages/vite/src/shared/utils.ts new file mode 100644 index 00000000000000..e3b9800edbc6bf --- /dev/null +++ b/packages/vite/src/shared/utils.ts @@ -0,0 +1,45 @@ +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 = /[?#].*$/ +export function cleanUrl(url: string): string { + return url.replace(postfixRE, '') +} + +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 +} 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 1dea46182b28cc..6ac8cfba836262 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -387,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) @@ -2976,7 +2979,6 @@ packages: cpu: [arm64] os: [android] requiresBuild: true - dev: false optional: true /@esbuild/android-arm@0.18.20: @@ -3003,7 +3005,6 @@ packages: cpu: [arm] os: [android] requiresBuild: true - dev: false optional: true /@esbuild/android-x64@0.18.20: @@ -3030,7 +3031,6 @@ packages: cpu: [x64] os: [android] requiresBuild: true - dev: false optional: true /@esbuild/darwin-arm64@0.18.20: @@ -3057,7 +3057,6 @@ packages: cpu: [arm64] os: [darwin] requiresBuild: true - dev: false optional: true /@esbuild/darwin-x64@0.18.20: @@ -3084,7 +3083,6 @@ packages: cpu: [x64] os: [darwin] requiresBuild: true - dev: false optional: true /@esbuild/freebsd-arm64@0.18.20: @@ -3111,7 +3109,6 @@ packages: cpu: [arm64] os: [freebsd] requiresBuild: true - dev: false optional: true /@esbuild/freebsd-x64@0.18.20: @@ -3138,7 +3135,6 @@ packages: cpu: [x64] os: [freebsd] requiresBuild: true - dev: false optional: true /@esbuild/linux-arm64@0.18.20: @@ -3165,7 +3161,6 @@ packages: cpu: [arm64] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-arm@0.18.20: @@ -3192,7 +3187,6 @@ packages: cpu: [arm] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-ia32@0.18.20: @@ -3219,7 +3213,6 @@ packages: cpu: [ia32] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-loong64@0.18.20: @@ -3246,7 +3239,6 @@ packages: cpu: [loong64] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-mips64el@0.18.20: @@ -3273,7 +3265,6 @@ packages: cpu: [mips64el] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-ppc64@0.18.20: @@ -3300,7 +3291,6 @@ packages: cpu: [ppc64] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-riscv64@0.18.20: @@ -3327,7 +3317,6 @@ packages: cpu: [riscv64] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-s390x@0.18.20: @@ -3354,7 +3343,6 @@ packages: cpu: [s390x] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/linux-x64@0.18.20: @@ -3381,7 +3369,6 @@ packages: cpu: [x64] os: [linux] requiresBuild: true - dev: false optional: true /@esbuild/netbsd-x64@0.18.20: @@ -3408,7 +3395,6 @@ packages: cpu: [x64] os: [netbsd] requiresBuild: true - dev: false optional: true /@esbuild/openbsd-x64@0.18.20: @@ -3435,7 +3421,6 @@ packages: cpu: [x64] os: [openbsd] requiresBuild: true - dev: false optional: true /@esbuild/sunos-x64@0.18.20: @@ -3462,7 +3447,6 @@ packages: cpu: [x64] os: [sunos] requiresBuild: true - dev: false optional: true /@esbuild/win32-arm64@0.18.20: @@ -3489,7 +3473,6 @@ packages: cpu: [arm64] os: [win32] requiresBuild: true - dev: false optional: true /@esbuild/win32-ia32@0.18.20: @@ -3516,7 +3499,6 @@ packages: cpu: [ia32] os: [win32] requiresBuild: true - dev: false optional: true /@esbuild/win32-x64@0.18.20: @@ -3543,7 +3525,6 @@ packages: cpu: [x64] os: [win32] requiresBuild: true - dev: false optional: true /@eslint-community/eslint-utils@4.4.0(eslint@8.56.0): @@ -3652,8 +3633,8 @@ packages: '@jridgewell/sourcemap-codec': 1.4.15 '@jridgewell/trace-mapping': 0.3.22 - /@jridgewell/resolve-uri@3.1.1: - 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'} /@jridgewell/set-array@1.1.2: @@ -3673,13 +3654,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.2 '@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.2 '@jridgewell/sourcemap-codec': 1.4.15 /@mapbox/node-pre-gyp@1.0.11: @@ -5804,7 +5785,6 @@ packages: '@esbuild/win32-arm64': 0.19.3 '@esbuild/win32-ia32': 0.19.3 '@esbuild/win32-x64': 0.19.3 - dev: false /escalade@3.1.1: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} @@ -8482,6 +8462,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'} 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', + ), + }, + }, })