Skip to content

Commit

Permalink
wip: support plugin-legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 6, 2025
1 parent 5a3ed8b commit 9650262
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"funding": "https://github.com/vitejs/vite?sponsor=1",
"dependencies": {
"@babel/core": "^7.26.0",
"@babel/plugin-transform-dynamic-import": "^7.25.9",
"@babel/plugin-transform-modules-systemjs": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"browserslist": "^4.24.3",
"browserslist-to-esbuild": "^2.1.1",
Expand Down
67 changes: 48 additions & 19 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
OutputBundle,
OutputChunk,
OutputOptions,
PluginContext,
PreRenderedChunk,
RenderedChunk,
} from 'rollup'
Expand Down Expand Up @@ -174,6 +175,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
const legacyPolyfills = new Set<string>()
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
// modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
// TODO: options object is not identical, so Map won't work
const outputToChunkFileNameToPolyfills = new WeakMap<
NormalizedOutputOptions,
Map<string, { modern: Set<string>; legacy: Set<string> }> | null
Expand Down Expand Up @@ -282,13 +284,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
return
}

const chunkFileNameToPolyfills =
outputToChunkFileNameToPolyfills.get(opts)
if (chunkFileNameToPolyfills == null) {
throw new Error(
'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',
)
}
// const chunkFileNameToPolyfills =
// outputToChunkFileNameToPolyfills.get(opts)
// if (chunkFileNameToPolyfills == null) {
// throw new Error(
// 'Internal @vitejs/plugin-legacy error: discovered polyfills should exist',
// )
// }
const chunkFileNameToPolyfills = new Map()

if (!isLegacyBundle(bundle, opts)) {
// Merge discovered modern polyfills to `modernPolyfills`
Expand All @@ -305,6 +308,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
)
}
await buildPolyfillChunk(
this,
config.mode,
modernPolyfills,
bundle,
Expand Down Expand Up @@ -347,6 +351,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}

await buildPolyfillChunk(
this,
config.mode,
legacyPolyfills,
bundle,
Expand Down Expand Up @@ -434,7 +439,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
): OutputOptions => {
return {
...options,
format: 'system',
// TODO
format: 'esm',
// format: 'system',
entryFileNames: getLegacyOutputFileName(options.entryFileNames),
chunkFileNames: getLegacyOutputFileName(options.chunkFileNames),
}
Expand All @@ -455,7 +462,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
},

async renderChunk(raw, chunk, opts, { chunks }) {
// TODO: meta.chunks not supported
async renderChunk(raw, chunk, opts, { chunks } = { chunks: {} }) {
if (config.build.ssr) {
return null
}
Expand All @@ -472,11 +480,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
outputToChunkFileNameToPolyfills.set(opts, chunkFileNameToPolyfills)
}
const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
if (polyfillsDiscovered == null) {
throw new Error(
`Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
)
// const polyfillsDiscovered = chunkFileNameToPolyfills.get(chunk.fileName)
// if (polyfillsDiscovered == null) {
// throw new Error(
// `Internal @vitejs/plugin-legacy error: discovered polyfills for ${chunk.fileName} should exist`,
// )
// }
const polyfillsDiscovered = {
modern: new Set(),
legacy: new Set(),
}

if (!isLegacyChunk(chunk, opts)) {
Expand Down Expand Up @@ -545,6 +557,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// transform the legacy chunk with @babel/preset-env
const sourceMaps = !!config.build.sourcemap
const babel = await loadBabel()
const systemJsPlugins = [
// @ts-ignore

Check failure on line 561 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
(await import('@babel/plugin-transform-dynamic-import')).default,
// @ts-ignore

Check failure on line 563 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
(await import('@babel/plugin-transform-modules-systemjs')).default,
]
const result = babel.transform(raw, {
babelrc: false,
configFile: false,
Expand All @@ -557,6 +575,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
[
() => ({
plugins: [
...systemJsPlugins,
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
replaceLegacyEnvBabelPlugin(),
wrapIIFEBabelPlugin(),
Expand Down Expand Up @@ -718,7 +737,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// avoid emitting duplicate assets
for (const name in bundle) {
if (bundle[name].type === 'asset' && !/.+\.map$/.test(name)) {
delete bundle[name]
// TODO: don't delete polyfil chunk emitted as asset
// delete bundle[name]
}
}
}
Expand Down Expand Up @@ -781,6 +801,7 @@ function createBabelPresetEnvOptions(
}

async function buildPolyfillChunk(
ctx: PluginContext,
mode: string,
imports: Set<string>,
bundle: OutputBundle,
Expand Down Expand Up @@ -846,16 +867,23 @@ async function buildPolyfillChunk(
}
}

// TODO: adding a whole new chunk to `bundle` is not supported by rolldown.
// can we use `emitFile(asset)` instead?
ctx.emitFile({
type: 'asset',
fileName: polyfillChunk.fileName,
source: polyfillChunk.code,
})
// add the chunk to the bundle
bundle[polyfillChunk.fileName] = polyfillChunk
// bundle[polyfillChunk.fileName] = polyfillChunk
if (polyfillChunk.sourcemapFileName) {
const polyfillChunkMapAsset = _polyfillChunk.output.find(
(chunk) =>
chunk.type === 'asset' &&
chunk.fileName === polyfillChunk.sourcemapFileName,
) as OutputAsset | undefined
if (polyfillChunkMapAsset) {
bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset
// bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset
}
}
}
Expand Down Expand Up @@ -907,14 +935,15 @@ function prependModenChunkLegacyGuardPlugin(): Plugin {
}

function isLegacyChunk(chunk: RenderedChunk, options: NormalizedOutputOptions) {

Check failure on line 937 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

'options' is defined but never used. Allowed unused args must match /^_/u
return options.format === 'system' && chunk.fileName.includes('-legacy')
return chunk.fileName.includes('-legacy')
// return options.format === 'system' && chunk.fileName.includes('-legacy')
}

function isLegacyBundle(
bundle: OutputBundle,
options: NormalizedOutputOptions,
) {
if (options.format === 'system') {
if (true || options.format === 'system') {

Check failure on line 946 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Unexpected constant condition

Check failure on line 946 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Unexpected constant truthiness on the left-hand side of a `||` expression
const entryChunk = Object.values(bundle).find(
(output) => output.type === 'chunk' && output.isEntry,
)
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
],

build: {
minify: false,
cssCodeSplit: false,
manifest: true,
sourcemap: true,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9650262

Please sign in to comment.