Skip to content

Commit df29bff

Browse files
feat: allow entryFileNames, chunkFileNames functions for legacy (#4122)
1 parent 9c455b2 commit df29bff

File tree

6 files changed

+102
-25
lines changed

6 files changed

+102
-25
lines changed

packages/playground/legacy/__tests__/legacy.spec.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBuild } from '../../testUtils'
1+
import { isBuild, untilUpdated } from '../../testUtils'
22

33
test('should work', async () => {
44
expect(await page.textContent('#app')).toMatch('Hello')
@@ -18,3 +18,29 @@ test('wraps with iife', async () => {
1818
'exposed babel helpers: false'
1919
)
2020
})
21+
22+
test('generates assets', async () => {
23+
await untilUpdated(
24+
() => page.textContent('#assets'),
25+
isBuild
26+
? [
27+
'index: 404',
28+
'index-legacy: 404',
29+
'chunk-async: 404',
30+
'chunk-async-legacy: 404',
31+
'immutable-chunk: 200',
32+
'immutable-chunk-legacy: 200',
33+
'polyfills-legacy: 404'
34+
].join('\n')
35+
: [
36+
'index: 404',
37+
'index-legacy: 404',
38+
'chunk-async: 404',
39+
'chunk-async-legacy: 404',
40+
'immutable-chunk: 404',
41+
'immutable-chunk-legacy: 404',
42+
'polyfills-legacy: 404'
43+
].join('\n'),
44+
true
45+
)
46+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const chunks = [
2+
'index',
3+
'index-legacy',
4+
'chunk-async',
5+
'chunk-async-legacy',
6+
'immutable-chunk',
7+
'immutable-chunk-legacy',
8+
'polyfills-legacy'
9+
]
10+
11+
export function fn() {
12+
return Promise.all(
13+
chunks.map(async (name) => {
14+
const response = await fetch(`/assets/${name}.js`)
15+
return `${name}: ${response.status}`
16+
})
17+
)
18+
}

packages/playground/legacy/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ <h1 id="app"></h1>
22
<div id="env"></div>
33
<div id="iterators"></div>
44
<div id="babel-helpers"></div>
5+
<div id="assets"></div>
56
<script type="module" src="./main.js"></script>

packages/playground/legacy/main.js

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ text(
2727
String.raw`exposed babel helpers: ${window._templateObject != null}`
2828
)
2929

30+
// dynamic chunk names
31+
import('./immutable-chunk.js')
32+
.then(({ fn }) => fn())
33+
.then((assets) => {
34+
text('#assets', assets.join('\n'))
35+
})
36+
3037
function text(el, text) {
3138
document.querySelector(el).textContent = text
3239
}

packages/playground/legacy/vite.config.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ module.exports = {
1111

1212
build: {
1313
// make tests faster
14-
minify: false
14+
minify: false,
15+
16+
rollupOptions: {
17+
output: {
18+
chunkFileNames(chunkInfo) {
19+
if (chunkInfo.name === 'immutable-chunk') {
20+
return `assets/${chunkInfo.name}.js`
21+
}
22+
23+
return `assets/chunk-[name].[hash].js`
24+
}
25+
}
26+
}
1527
},
1628

1729
// special test only hook

packages/plugin-legacy/index.js

+36-23
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function viteLegacyPlugin(options = {}) {
9999
},
100100

101101
async generateBundle(opts, bundle) {
102-
if (!isLegacyOutput(opts)) {
102+
if (!isLegacyBundle(bundle, opts)) {
103103
if (!modernPolyfills.size) {
104104
return
105105
}
@@ -168,8 +168,9 @@ function viteLegacyPlugin(options = {}) {
168168
}
169169

170170
/**
171-
* @param {string|((chunkInfo: import('rollup').PreRenderedChunk)=>string)} fileNames
171+
* @param {string | ((chunkInfo: import('rollup').PreRenderedChunk) => string)} fileNames
172172
* @param {string?} defaultFileName
173+
* @returns {(chunkInfo: import('rollup').PreRenderedChunk) => string)}
173174
*/
174175
const getLegacyOutputFileName = (
175176
fileNames,
@@ -179,21 +180,20 @@ function viteLegacyPlugin(options = {}) {
179180
return path.posix.join(config.build.assetsDir, defaultFileName)
180181
}
181182

182-
// does not support custom functions.
183-
if (typeof fileNames === 'function') {
184-
throw new Error(
185-
`@vitejs/plugin-legacy rollupOptions.output.entryFileNames and rollupOptions.output.chunkFileNames` +
186-
` does not support the function format.`
187-
)
188-
}
183+
return (chunkInfo) => {
184+
let fileName =
185+
typeof fileNames === 'function' ? fileNames(chunkInfo) : fileNames
189186

190-
let fileName = defaultFileName
191-
// Custom string file return format.
192-
if (fileNames && typeof fileNames === 'string') {
193-
fileName = fileNames.replace(/\[name\]/, '[name]-legacy')
194-
}
187+
if (fileName.includes('[name]')) {
188+
// [name]-[hash].[format] -> [name]-legacy-[hash].[format]
189+
fileName = fileName.replace('[name]', '[name]-legacy')
190+
} else {
191+
// entry.js -> entry-legacy.js
192+
fileName = fileName.replace(/(.+)\.(.+)/, '$1-legacy.$2')
193+
}
195194

196-
return fileName
195+
return fileName
196+
}
197197
}
198198

199199
/**
@@ -219,7 +219,7 @@ function viteLegacyPlugin(options = {}) {
219219
},
220220

221221
renderChunk(raw, chunk, opts) {
222-
if (!isLegacyOutput(opts)) {
222+
if (!isLegacyChunk(chunk, opts)) {
223223
if (
224224
options.modernPolyfills &&
225225
!Array.isArray(options.modernPolyfills)
@@ -399,7 +399,7 @@ function viteLegacyPlugin(options = {}) {
399399
},
400400

401401
generateBundle(opts, bundle) {
402-
if (isLegacyOutput(opts)) {
402+
if (isLegacyBundle(bundle, opts)) {
403403
// avoid emitting duplicate assets
404404
for (const name in bundle) {
405405
if (bundle[name].type === 'asset') {
@@ -564,14 +564,27 @@ function polyfillsPlugin(imports) {
564564
}
565565

566566
/**
567+
* @param {import('rollup').RenderedChunk} chunk
567568
* @param {import('rollup').NormalizedOutputOptions} options
568569
*/
569-
function isLegacyOutput(options) {
570-
return (
571-
options.format === 'system' &&
572-
typeof options.entryFileNames === 'string' &&
573-
options.entryFileNames.includes('-legacy')
574-
)
570+
function isLegacyChunk(chunk, options) {
571+
return options.format === 'system' && chunk.fileName.includes('-legacy')
572+
}
573+
574+
/**
575+
* @param {import('rollup').OutputBundle} bundle
576+
* @param {import('rollup').NormalizedOutputOptions} options
577+
*/
578+
function isLegacyBundle(bundle, options) {
579+
if (options.format === 'system') {
580+
const entryChunk = Object.values(bundle).find(
581+
(output) => output.type === 'chunk' && output.isEntry
582+
)
583+
584+
return !!entryChunk && entryChunk.fileName.includes('-legacy')
585+
}
586+
587+
return false
575588
}
576589

577590
/**

0 commit comments

Comments
 (0)