Skip to content

Commit b20d542

Browse files
authored
refactor: share code with vite runtime (#15907)
1 parent 42fd11c commit b20d542

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+372
-553
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ module.exports = defineConfig({
145145
},
146146
{
147147
files: ['packages/vite/src/node/**'],
148+
excludedFiles: '**/__tests__/**',
148149
rules: {
149150
'no-console': ['error'],
150151
},

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
packages/*/CHANGELOG.md
2+
packages/vite/src/node/ssr/runtime/__tests__/fixtures
23
playground-temp/
34
dist/
45
temp/

packages/vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"postcss-modules": "^6.0.0",
141141
"resolve.exports": "^2.0.2",
142142
"rollup-plugin-dts": "^6.1.0",
143+
"rollup-plugin-esbuild": "^6.1.1",
143144
"rollup-plugin-license": "^3.2.0",
144145
"sirv": "^2.0.4",
145146
"source-map-support": "^0.5.21",

packages/vite/rollup.config.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import json from '@rollup/plugin-json'
88
import MagicString from 'magic-string'
99
import type { Plugin, RollupOptions } from 'rollup'
1010
import { defineConfig } from 'rollup'
11+
import { minify as esbuildMinifyPlugin } from 'rollup-plugin-esbuild'
1112
import licensePlugin from './rollupLicensePlugin'
1213

1314
const pkg = JSON.parse(
@@ -153,13 +154,13 @@ function createNodeConfig(isProduction: boolean) {
153154
index: path.resolve(__dirname, 'src/node/index.ts'),
154155
cli: path.resolve(__dirname, 'src/node/cli.ts'),
155156
constants: path.resolve(__dirname, 'src/node/constants.ts'),
156-
runtime: path.resolve(__dirname, 'src/node/ssr/runtime/index.ts'),
157157
},
158158
output: {
159159
...sharedNodeOptions.output,
160160
sourcemap: !isProduction,
161161
},
162162
external: [
163+
/^vite\//,
163164
'fsevents',
164165
'lightningcss',
165166
'rollup/parseAst',
@@ -176,6 +177,51 @@ function createNodeConfig(isProduction: boolean) {
176177
})
177178
}
178179

180+
function createRuntimeConfig(isProduction: boolean) {
181+
return defineConfig({
182+
...sharedNodeOptions,
183+
input: {
184+
runtime: path.resolve(__dirname, 'src/runtime/index.ts'),
185+
},
186+
output: {
187+
...sharedNodeOptions.output,
188+
sourcemap: !isProduction,
189+
},
190+
external: [
191+
'fsevents',
192+
'lightningcss',
193+
'rollup/parseAst',
194+
...Object.keys(pkg.dependencies),
195+
],
196+
plugins: [
197+
...createNodePlugins(
198+
false,
199+
!isProduction,
200+
// in production we use rollup.dts.config.ts for dts generation
201+
// in development we need to rely on the rollup ts plugin
202+
isProduction ? false : './dist/node',
203+
),
204+
esbuildMinifyPlugin({ minify: false, minifySyntax: true }),
205+
{
206+
name: 'replace bias',
207+
transform(code, id) {
208+
if (id.includes('@jridgewell+trace-mapping')) {
209+
return {
210+
code: code.replaceAll(
211+
'bias === LEAST_UPPER_BOUND',
212+
'true' +
213+
`/*${'bias === LEAST_UPPER_BOUND'.length - '/**/'.length - 'true'.length}*/`,
214+
),
215+
map: null,
216+
}
217+
}
218+
},
219+
},
220+
bundleSizeLimit(45),
221+
],
222+
})
223+
}
224+
179225
function createCjsConfig(isProduction: boolean) {
180226
return defineConfig({
181227
...sharedNodeOptions,
@@ -209,6 +255,7 @@ export default (commandLineArgs: any): RollupOptions[] => {
209255
envConfig,
210256
clientConfig,
211257
createNodeConfig(isProduction),
258+
createRuntimeConfig(isProduction),
212259
createCjsConfig(isProduction),
213260
])
214261
}

packages/vite/rollup.dts.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const pkg = JSON.parse(
1515

1616
const external = [
1717
/^node:*/,
18+
/^vite\//,
1819
'rollup/parseAst',
1920
...Object.keys(pkg.dependencies),
2021
// lightningcss types are bundled
@@ -24,7 +25,7 @@ const external = [
2425
export default defineConfig({
2526
input: {
2627
index: './temp/node/index.d.ts',
27-
runtime: './temp/node/ssr/runtime/index.d.ts',
28+
runtime: './temp/runtime/index.d.ts',
2829
},
2930
output: {
3031
dir: './dist/node',
@@ -132,6 +133,7 @@ function validateChunkImports(this: PluginContext, chunk: RenderedChunk) {
132133
!id.startsWith('../') &&
133134
!id.startsWith('node:') &&
134135
!id.startsWith('types.d') &&
136+
!id.startsWith('vite/') &&
135137
!deps.includes(id) &&
136138
!deps.some((name) => id.startsWith(name + '/'))
137139
) {

packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33
import { describe, expect, it } from 'vitest'
44
import { transformDynamicImport } from '../../../plugins/dynamicImportVars'
5-
import { isWindows, normalizePath } from '../../../utils'
5+
import { normalizePath } from '../../../utils'
6+
import { isWindows } from '../../../../shared/utils'
67

78
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
89

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"esModuleInterop": true,
5+
"declaration": false,
6+
"resolveJsonModule": true
7+
}
8+
}

packages/vite/src/node/__tests__/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {
1010
getLocalhostAddressIfDiffersFromDNS,
1111
injectQuery,
1212
isFileReadable,
13-
isWindows,
1413
posToNumber,
1514
processSrcSetSync,
1615
resolveHostname,
1716
} from '../utils'
17+
import { isWindows } from '../../shared/utils'
1818

1919
describe('bareImportRE', () => {
2020
test('should work with normal package name', () => {

packages/vite/src/node/build.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import commonjsPlugin from '@rollup/plugin-commonjs'
2121
import type { RollupCommonJSOptions } from 'dep-types/commonjs'
2222
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
2323
import type { TransformOptions } from 'esbuild'
24+
import { withTrailingSlash } from '../shared/utils'
25+
import {
26+
DEFAULT_ASSETS_INLINE_LIMIT,
27+
ESBUILD_MODULES_TARGET,
28+
VERSION,
29+
} from './constants'
2430
import type { InlineConfig, ResolvedConfig } from './config'
2531
import { resolveConfig } from './config'
2632
import { buildReporterPlugin } from './plugins/reporter'
@@ -35,7 +41,6 @@ import {
3541
joinUrlSegments,
3642
normalizePath,
3743
requireResolveFromRootWithFallback,
38-
withTrailingSlash,
3944
} from './utils'
4045
import { manifestPlugin } from './plugins/manifest'
4146
import type { Logger } from './logger'
@@ -45,11 +50,6 @@ import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
4550
import { loadFallbackPlugin } from './plugins/loadFallback'
4651
import { findNearestPackageData } from './packages'
4752
import type { PackageCache } from './packages'
48-
import {
49-
DEFAULT_ASSETS_INLINE_LIMIT,
50-
ESBUILD_MODULES_TARGET,
51-
VERSION,
52-
} from './constants'
5353
import { resolveChokidarOptions } from './watch'
5454
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
5555
import { mergeConfig } from './publicUtils'

packages/vite/src/node/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import fs from 'node:fs'
33
import { performance } from 'node:perf_hooks'
44
import { cac } from 'cac'
55
import colors from 'picocolors'
6+
import { VERSION } from './constants'
67
import type { BuildOptions } from './build'
78
import type { ServerOptions } from './server'
89
import type { CLIShortcut } from './shortcuts'
910
import type { LogLevel } from './logger'
1011
import { createLogger } from './logger'
11-
import { VERSION } from './constants'
1212
import { resolveConfig } from './config'
1313

1414
const cli = cac('vite')

0 commit comments

Comments
 (0)