Skip to content

Commit beb7166

Browse files
patak-devbluwy
andauthored
feat: rollup 3 (#9870)
Co-authored-by: bluwy <[email protected]>
1 parent d9f6dc0 commit beb7166

18 files changed

+115
-132
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"prompts": "^2.4.2",
8080
"resolve": "^1.22.1",
8181
"rimraf": "^3.0.2",
82-
"rollup": "^2.79.1",
82+
"rollup": "~3.2.3",
8383
"rollup-plugin-license": "^2.9.0",
8484
"semver": "^7.3.8",
8585
"simple-git-hooks": "^2.8.1",

packages/plugin-legacy/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
415415
configFile: false,
416416
compact: !!config.build.minify,
417417
sourceMaps,
418-
inputSourceMap: sourceMaps ? chunk.map : undefined,
418+
inputSourceMap: undefined, // sourceMaps ? chunk.map : undefined, `.map` TODO: moved to OutputChunk?
419419
presets: [
420420
// forcing our plugin to run before preset-env by wrapping it in a
421421
// preset so we can catch the injected import statements...

packages/plugin-vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@jridgewell/gen-mapping": "^0.3.2",
4343
"@jridgewell/trace-mapping": "^0.3.17",
4444
"debug": "^4.3.4",
45-
"rollup": "^2.79.1",
45+
"rollup": "~3.2.3",
4646
"slash": "^5.0.0",
4747
"source-map": "^0.6.1",
4848
"vite": "workspace:*",

packages/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"esbuild": "^0.15.9",
6262
"postcss": "^8.4.18",
6363
"resolve": "^1.22.1",
64-
"rollup": "^2.79.1"
64+
"rollup": "~3.2.3"
6565
},
6666
"optionalDependencies": {
6767
"fsevents": "~2.3.2"

packages/vite/rollup.config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* eslint-disable no-restricted-globals */
1+
import { readFileSync } from 'node:fs'
22
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
34
import nodeResolve from '@rollup/plugin-node-resolve'
45
import typescript from '@rollup/plugin-typescript'
56
import commonjs from '@rollup/plugin-commonjs'
@@ -8,7 +9,12 @@ import MagicString from 'magic-string'
89
import type { Plugin, RollupOptions } from 'rollup'
910
import { defineConfig } from 'rollup'
1011
import licensePlugin from '../../scripts/rollupLicensePlugin.mjs'
11-
import pkg from './package.json'
12+
13+
const pkg = JSON.parse(
14+
readFileSync(new URL('./package.json', import.meta.url)).toString()
15+
)
16+
17+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
1218

1319
const envConfig = defineConfig({
1420
input: path.resolve(__dirname, 'src/client/env.ts'),

packages/vite/src/node/build.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { buildReporterPlugin } from './plugins/reporter'
2828
import { buildEsbuildPlugin } from './plugins/esbuild'
2929
import { terserPlugin } from './plugins/terser'
3030
import {
31+
asyncFlatten,
3132
copyDir,
3233
emptyDir,
3334
joinUrlSegments,
@@ -383,25 +384,30 @@ export function resolveBuildOptions(
383384
return resolved
384385
}
385386

386-
export function resolveBuildPlugins(config: ResolvedConfig): {
387+
export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
387388
pre: Plugin[]
388389
post: Plugin[]
389-
} {
390+
}> {
390391
const options = config.build
391392
const { commonjsOptions } = options
392393
const usePluginCommonjs =
393394
!Array.isArray(commonjsOptions?.include) ||
394395
commonjsOptions?.include.length !== 0
396+
const rollupOptionsPlugins = options.rollupOptions.plugins
395397
return {
396398
pre: [
397399
completeSystemWrapPlugin(),
398400
...(options.watch ? [ensureWatchPlugin()] : []),
399401
watchPackageDataPlugin(config),
400402
...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []),
401403
dataURIPlugin(),
402-
...(options.rollupOptions.plugins
403-
? (options.rollupOptions.plugins.filter(Boolean) as Plugin[])
404-
: [])
404+
...((
405+
await asyncFlatten(
406+
Array.isArray(rollupOptionsPlugins)
407+
? rollupOptionsPlugins
408+
: [rollupOptionsPlugins]
409+
)
410+
).filter(Boolean) as Plugin[])
405411
],
406412
post: [
407413
buildImportAnalysisPlugin(config),
@@ -827,12 +833,12 @@ export function onRollupWarning(
827833
config: ResolvedConfig
828834
): void {
829835
if (warning.code === 'UNRESOLVED_IMPORT') {
830-
const id = warning.source
831-
const importer = warning.importer
836+
const id = warning.id
837+
const exporter = warning.exporter
832838
// throw unless it's commonjs external...
833-
if (!importer || !/\?commonjs-external$/.test(importer)) {
839+
if (!id || !/\?commonjs-external$/.test(id)) {
834840
throw new Error(
835-
`[vite]: Rollup failed to resolve import "${id}" from "${importer}".\n` +
841+
`[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
836842
`This is most likely unintended because it can break your application at runtime.\n` +
837843
`If you do want to externalize this module explicitly add it to\n` +
838844
`\`build.rollupOptions.external\``

packages/vite/src/node/config.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,10 @@ export async function resolveConfig(
569569
}))
570570
}
571571
return (
572-
await container.resolveId(id, importer, { ssr, scan: options?.scan })
572+
await container.resolveId(id, importer, {
573+
ssr,
574+
scan: options?.scan
575+
})
573576
)?.id
574577
}
575578
}

packages/vite/src/node/constants.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import path, { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3-
// @ts-expect-error
4-
import { version } from '../../package.json'
3+
import { readFileSync } from 'node:fs'
4+
5+
const { version } = JSON.parse(
6+
readFileSync(new URL('../../package.json', import.meta.url)).toString()
7+
)
58

69
export const VERSION = version as string
710

packages/vite/src/node/plugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export interface Plugin extends RollupPlugin {
146146
source: string,
147147
importer: string | undefined,
148148
options: {
149+
assertions: Record<string, string>
149150
custom?: CustomPluginOptions
150151
ssr?: boolean
151152
/**

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,7 @@ async function fileToBuiltUrl(
494494
name,
495495
fileName: map.get(contentHash)!,
496496
type: 'asset',
497-
source: content,
498-
isAsset: true
497+
source: content
499498
})
500499
}
501500

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

+24-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CLIENT_PUBLIC_PATH, SPECIAL_QUERY_RE } from '../constants'
3030
import type { ResolvedConfig } from '../config'
3131
import type { Plugin } from '../plugin'
3232
import {
33+
arrayEqual,
3334
asyncReplace,
3435
cleanUrl,
3536
combineSourcemaps,
@@ -305,7 +306,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
305306
export function cssPostPlugin(config: ResolvedConfig): Plugin {
306307
// styles initialization in buildStart causes a styling loss in watch
307308
const styles: Map<string, string> = new Map<string, string>()
308-
let pureCssChunks: Set<string>
309+
let pureCssChunks: Set<RenderedChunk>
309310

310311
// when there are multiple rollup outputs and extracting CSS, only emit once,
311312
// since output formats have no effect on the generated CSS.
@@ -339,7 +340,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
339340

340341
buildStart() {
341342
// Ensure new caches for every build (i.e. rebuilding in watch mode)
342-
pureCssChunks = new Set<string>()
343+
pureCssChunks = new Set<RenderedChunk>()
343344
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
344345
hasEmitted = false
345346
},
@@ -537,7 +538,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
537538
if (config.build.cssCodeSplit) {
538539
if (isPureCssChunk) {
539540
// this is a shared CSS-only chunk that is empty.
540-
pureCssChunks.add(chunk.fileName)
541+
pureCssChunks.add(chunk)
541542
}
542543
if (opts.format === 'es' || opts.format === 'cjs') {
543544
const cssAssetName = chunk.facadeModuleId
@@ -624,7 +625,24 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
624625

625626
// remove empty css chunks and their imports
626627
if (pureCssChunks.size) {
627-
const emptyChunkFiles = [...pureCssChunks]
628+
// map each pure css chunk (rendered chunk) to it's corresponding bundle
629+
// chunk. we check that by comparing the `moduleIds` as they have different
630+
// filenames (rendered chunk has the !~{XXX}~ placeholder)
631+
const pureCssChunkNames: string[] = []
632+
for (const pureCssChunk of pureCssChunks) {
633+
for (const key in bundle) {
634+
const bundleChunk = bundle[key]
635+
if (
636+
bundleChunk.type === 'chunk' &&
637+
arrayEqual(bundleChunk.moduleIds, pureCssChunk.moduleIds)
638+
) {
639+
pureCssChunkNames.push(key)
640+
break
641+
}
642+
}
643+
}
644+
645+
const emptyChunkFiles = pureCssChunkNames
628646
.map((file) => path.basename(file))
629647
.join('|')
630648
.replace(/\./g, '\\.')
@@ -641,7 +659,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
641659
// and also register the emitted CSS files under the importer
642660
// chunks instead.
643661
chunk.imports = chunk.imports.filter((file) => {
644-
if (pureCssChunks.has(file)) {
662+
if (pureCssChunkNames.includes(file)) {
645663
const {
646664
viteMetadata: { importedCss }
647665
} = bundle[file] as OutputChunk
@@ -660,7 +678,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
660678
}
661679
}
662680
const removedPureCssFiles = removedPureCssFilesCache.get(config)!
663-
pureCssChunks.forEach((fileName) => {
681+
pureCssChunkNames.forEach((fileName) => {
664682
removedPureCssFiles.set(fileName, bundle[fileName] as RenderedChunk)
665683
delete bundle[fileName]
666684
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function resolvePlugins(
3535
const isBuild = config.command === 'build'
3636
const isWatch = isBuild && !!config.build.watch
3737
const buildPlugins = isBuild
38-
? (await import('../build')).resolveBuildPlugins(config)
38+
? await (await import('../build')).resolveBuildPlugins(config)
3939
: { pre: [], post: [] }
4040
const { modulePreload } = config.build
4141

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {
22
GetManualChunk,
3-
GetManualChunkApi,
43
GetModuleInfo,
4+
ManualChunkMeta,
55
OutputOptions
66
} from 'rollup'
77
import type { UserConfig } from '../../node'
@@ -106,7 +106,7 @@ export function splitVendorChunkPlugin(): Plugin {
106106
if (output.manualChunks) {
107107
if (typeof output.manualChunks === 'function') {
108108
const userManualChunks = output.manualChunks
109-
output.manualChunks = (id: string, api: GetManualChunkApi) => {
109+
output.manualChunks = (id: string, api: ManualChunkMeta) => {
110110
return userManualChunks(id, api) ?? viteManualChunks(id, api)
111111
}
112112
}

packages/vite/src/node/server/pluginContainer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface PluginContainer {
9696
id: string,
9797
importer?: string,
9898
options?: {
99+
assertions?: Record<string, string>
99100
custom?: CustomPluginOptions
100101
skip?: Set<Plugin>
101102
ssr?: boolean
@@ -295,6 +296,7 @@ export async function createPluginContainer(
295296
id: string,
296297
importer?: string,
297298
options?: {
299+
assertions?: Record<string, string>
298300
custom?: CustomPluginOptions
299301
isEntry?: boolean
300302
skipSelf?: boolean
@@ -306,6 +308,7 @@ export async function createPluginContainer(
306308
skip.add(this._activePlugin)
307309
}
308310
let out = await container.resolveId(id, importer, {
311+
assertions: options?.assertions,
309312
custom: options?.custom,
310313
isEntry: !!options?.isEntry,
311314
skip,
@@ -587,6 +590,7 @@ export async function createPluginContainer(
587590
? plugin.resolveId.handler
588591
: plugin.resolveId
589592
const result = await handler.call(ctx as any, rawId, importer, {
593+
assertions: options?.assertions ?? {},
590594
custom: options?.custom,
591595
isEntry: !!options?.isEntry,
592596
ssr,

packages/vite/src/node/utils.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,12 @@ export function joinUrlSegments(a: string, b: string): string {
12051205
}
12061206
return a + b
12071207
}
1208+
1209+
export function arrayEqual(a: any[], b: any[]): boolean {
1210+
if (a === b) return true
1211+
if (a.length !== b.length) return false
1212+
for (let i = 0; i < a.length; i++) {
1213+
if (a[i] !== b[i]) return false
1214+
}
1215+
return true
1216+
}

playground/vitestSetup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export async function notifyRebuildComplete(
306306
await new Promise<void>((resolve) => {
307307
resolveFn = resolve
308308
})
309-
return watcher.removeListener('event', callback)
309+
return watcher.off('event', callback)
310310
}
311311

312312
function createInMemoryLogger(logs: string[]): Logger {

0 commit comments

Comments
 (0)