Skip to content

Commit dee6067

Browse files
authored
feat!: rollup v4 (#14508)
1 parent a76be5d commit dee6067

Some content is hidden

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

42 files changed

+294
-261
lines changed

docs/guide/migration.md

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
Vite no longer supports Node.js 14 / 16 / 17 / 19, which reached its EOL. Node.js 18 / 20+ is now required.
66

7+
## Rollup 4
8+
9+
Vite is now using Rollup 4 which also brings along its breaking changes, in particular:
10+
11+
- Import assertions (`assertions` prop) has been renamed to import attributes (`attributes` prop).
12+
- Acorn plugins are no longer supported.
13+
- For Vite plugins, `this.resolve` `skipSelf` option is now `true` by default.
14+
- For Vite plugins, `this.parse` now only supports the `allowReturnOutsideFunction` option for now.
15+
16+
Read the full breaking changes in [Rollup's release notes](https://github.com/rollup/rollup/releases/tag/v4.0.0) for build-related changes in `build.rollupOptions`.
17+
718
## Deprecate CJS Node API
819

920
The CJS Node API of Vite is deprecated. When calling `require('vite')`, a deprecation warning is now logged. You should update your files or frameworks to import the ESM build of Vite instead.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"playwright-chromium": "^1.39.0",
7777
"prettier": "3.0.3",
7878
"rimraf": "^5.0.5",
79-
"rollup": "^3.29.2",
79+
"rollup": "^4.1.4",
8080
"simple-git-hooks": "^2.9.0",
8181
"tslib": "^2.6.2",
8282
"tsx": "^3.13.0",

packages/create-vite/build.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default defineBuildConfig({
2323
'rollup:options'(ctx, options) {
2424
options.plugins = [
2525
options.plugins,
26+
// @ts-expect-error TODO: unbuild uses rollup v3 and Vite uses rollup v4
2627
licensePlugin(
2728
path.resolve(__dirname, './LICENSE'),
2829
'create-vite license',

packages/vite/LICENSE.md

-7
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,6 @@ Repository: https://github.com/acornjs/acorn.git
566566
567567
---------------------------------------
568568

569-
## acorn-import-assertions
570-
License: MIT
571-
By: Sven Sauleau
572-
Repository: https://github.com/xtuc/acorn-import-assertions
573-
574-
---------------------------------------
575-
576569
## acorn-walk
577570
License: MIT
578571
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine

packages/vite/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"dependencies": {
7777
"esbuild": "^0.19.3",
7878
"postcss": "^8.4.31",
79-
"rollup": "^3.29.4"
79+
"rollup": "^4.1.4"
8080
},
8181
"optionalDependencies": {
8282
"fsevents": "~2.3.3"
@@ -94,7 +94,6 @@
9494
"@types/escape-html": "^1.0.2",
9595
"@types/pnpapi": "^0.0.3",
9696
"acorn": "^8.10.0",
97-
"acorn-import-assertions": "^1.9.0",
9897
"acorn-walk": "^8.2.0",
9998
"cac": "^6.7.14",
10099
"chokidar": "^3.5.3",

packages/vite/rollup.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ function createNodeConfig(isProduction: boolean) {
161161
external: [
162162
'fsevents',
163163
'lightningcss',
164+
'rollup/parseAst',
164165
...Object.keys(pkg.dependencies),
165166
...(isProduction ? [] : Object.keys(pkg.devDependencies)),
166167
],

packages/vite/src/node/build.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
RollupLog,
1515
RollupOptions,
1616
RollupOutput,
17-
RollupWarning,
1817
RollupWatcher,
1918
WatcherOptions,
2019
} from 'rollup'
@@ -45,7 +44,6 @@ import { initDepsOptimizer } from './optimizer'
4544
import { loadFallbackPlugin } from './plugins/loadFallback'
4645
import { findNearestPackageData } from './packages'
4746
import type { PackageCache } from './packages'
48-
import { ensureWatchPlugin } from './plugins/ensureWatch'
4947
import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
5048
import { resolveChokidarOptions } from './watch'
5149
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -426,7 +424,6 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
426424
return {
427425
pre: [
428426
completeSystemWrapPlugin(),
429-
...(options.watch ? [ensureWatchPlugin()] : []),
430427
...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []),
431428
dataURIPlugin(),
432429
...((
@@ -858,7 +855,7 @@ const dynamicImportWarningIgnoreList = [
858855
]
859856

860857
export function onRollupWarning(
861-
warning: RollupWarning,
858+
warning: RollupLog,
862859
warn: LoggingFunction,
863860
config: ResolvedConfig,
864861
): void {

packages/vite/src/node/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
150150
source: string,
151151
importer: string | undefined,
152152
options: {
153-
assertions: Record<string, string>
153+
attributes: Record<string, string>
154154
custom?: CustomPluginOptions
155155
ssr?: boolean
156156
/**

packages/vite/src/node/plugins/asset.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
} from '../utils'
2828
import { FS_PREFIX } from '../constants'
2929

30-
export const assetUrlRE = /__VITE_ASSET__([a-z\d]+)__(?:\$_(.*?)__)?/g
30+
// referenceId is base64url but replaces - with $
31+
export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
3132

3233
const rawRE = /(?:\?|&)raw(?:&|$)/
3334
export const urlRE = /(\?|&)url(?:&|$)/
@@ -78,10 +79,10 @@ export function renderAssetUrlInJS(
7879
let s: MagicString | undefined
7980

8081
// Urls added with JS using e.g.
81-
// imgElement.src = "__VITE_ASSET__5aa0ddc0__" are using quotes
82+
// imgElement.src = "__VITE_ASSET__5aA0Ddc0__" are using quotes
8283

8384
// Urls added in CSS that is imported in JS end up like
84-
// var inlined = ".inlined{color:green;background:url(__VITE_ASSET__5aa0ddc0__)}\n";
85+
// var inlined = ".inlined{color:green;background:url(__VITE_ASSET__5aA0Ddc0__)}\n";
8586

8687
// In both cases, the wrapping should already be fine
8788

@@ -107,7 +108,7 @@ export function renderAssetUrlInJS(
107108
s.update(match.index, match.index + full.length, replacementString)
108109
}
109110

110-
// Replace __VITE_PUBLIC_ASSET__5aa0ddc0__ with absolute paths
111+
// Replace __VITE_PUBLIC_ASSET__5aA0Ddc0__ with absolute paths
111112

112113
const publicAssetUrlMap = publicAssetUrlCache.get(config)!
113114
publicAssetUrlRE.lastIndex = 0
@@ -179,6 +180,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
179180
// raw requests, read from disk
180181
if (rawRE.test(id)) {
181182
const file = checkPublicFile(id, config) || cleanUrl(id)
183+
this.addWatchFile(file)
182184
// raw query, read file and return as string
183185
return `export default ${JSON.stringify(
184186
await fsp.readFile(file, 'utf-8'),

packages/vite/src/node/plugins/ensureWatch.ts

-17
This file was deleted.

packages/vite/src/node/plugins/importAnalysis.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
408408
ss: expStart,
409409
se: expEnd,
410410
d: dynamicIndex,
411-
a: assertIndex,
411+
a: attributeIndex,
412412
} = importSpecifier
413413

414414
// #2083 User may use escape path,
@@ -464,8 +464,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
464464

465465
const isDynamicImport = dynamicIndex > -1
466466

467-
// strip import assertions as we can process them ourselves
468-
if (!isDynamicImport && assertIndex > -1) {
467+
// strip import attributes as we can process them ourselves
468+
if (!isDynamicImport && attributeIndex > -1) {
469469
str().remove(end + 1, expEnd)
470470
}
471471

packages/vite/src/node/plugins/importAnalysisBuild.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
261261
}
262262
}
263263

264-
const resolved = await this.resolve(url, importerFile)
264+
const resolved = await this.resolve(url, importerFile, {
265+
skipSelf: false,
266+
})
265267

266268
if (!resolved) {
267269
// in ssr, we should let node handle the missing modules
@@ -305,13 +307,13 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
305307
se: expEnd,
306308
n: specifier,
307309
d: dynamicIndex,
308-
a: assertIndex,
310+
a: attributeIndex,
309311
} = imports[index]
310312

311313
const isDynamicImport = dynamicIndex > -1
312314

313-
// strip import assertions as we can process them ourselves
314-
if (!isDynamicImport && assertIndex > -1) {
315+
// strip import attributes as we can process them ourselves
316+
if (!isDynamicImport && attributeIndex > -1) {
315317
str().remove(end + 1, expEnd)
316318
}
317319

packages/vite/src/node/plugins/importMetaGlob.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ type IdResolver = (
486486
id: string,
487487
importer?: string,
488488
options?: {
489-
assertions?: Record<string, string>
489+
attributes?: Record<string, string>
490490
custom?: CustomPluginOptions
491491
isEntry?: boolean
492492
skipSelf?: boolean

packages/vite/src/node/plugins/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { preAliasPlugin } from './preAlias'
2121
import { definePlugin } from './define'
2222
import { workerImportMetaUrlPlugin } from './workerImportMetaUrl'
2323
import { assetImportMetaUrlPlugin } from './assetImportMetaUrl'
24-
import { ensureWatchPlugin } from './ensureWatch'
2524
import { metadataPlugin } from './metadata'
2625
import { dynamicImportVarsPlugin } from './dynamicImportVars'
2726
import { importGlobPlugin } from './importMetaGlob'
@@ -33,7 +32,6 @@ export async function resolvePlugins(
3332
postPlugins: Plugin[],
3433
): Promise<Plugin[]> {
3534
const isBuild = config.command === 'build'
36-
const isWatch = isBuild && !!config.build.watch
3735
const buildPlugins = isBuild
3836
? await (await import('../build')).resolveBuildPlugins(config)
3937
: { pre: [], post: [] }
@@ -48,7 +46,6 @@ export async function resolvePlugins(
4846
: optimizedDepsPlugin(config),
4947
]
5048
: []),
51-
isWatch ? ensureWatchPlugin() : null,
5249
isBuild ? metadataPlugin() : null,
5350
watchPackageDataPlugin(config.packageCache),
5451
preAliasPlugin(config),

packages/vite/src/node/plugins/loadFallback.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ export function loadFallbackPlugin(): Plugin {
1010
name: 'vite:load-fallback',
1111
async load(id) {
1212
try {
13-
// if we don't add `await` here, we couldn't catch the error in readFile
14-
return await fsp.readFile(cleanUrl(id), 'utf-8')
13+
const cleanedId = cleanUrl(id)
14+
const content = await fsp.readFile(cleanedId, 'utf-8')
15+
this.addWatchFile(cleanedId)
16+
return content
1517
} catch (e) {
16-
return fsp.readFile(id, 'utf-8')
18+
const content = await fsp.readFile(id, 'utf-8')
19+
this.addWatchFile(id)
20+
return content
1721
}
1822
},
1923
}

packages/vite/src/node/plugins/optimizedDeps.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ export function optimizedDepsBuildPlugin(config: ResolvedConfig): Plugin {
102102
// When a optimized dep is aliased, we need to avoid waiting for it before optimizing
103103
return
104104
}
105-
const resolved = await this.resolve(id, importer, {
106-
...options,
107-
skipSelf: true,
108-
})
105+
const resolved = await this.resolve(id, importer, options)
109106
if (resolved && !resolved.external) {
110107
depsOptimizer.delayDepsOptimizerUntil(resolved.id, async () => {
111108
await this.load(resolved)

packages/vite/src/node/plugins/preAlias.ts

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
5656
const resolved = await this.resolve(id, importer, {
5757
...options,
5858
custom: { ...options.custom, 'vite:pre-alias': true },
59-
skipSelf: true,
6059
})
6160
if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
6261
const optimizeDeps = depsOptimizer.options

0 commit comments

Comments
 (0)