Skip to content

Commit d62dc33

Browse files
authored
refactor(legacy): use Rollup type export from Vite (#20335)
1 parent c1c951d commit d62dc33

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

packages/plugin-legacy/src/index.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable n/no-extraneous-import */
21
import path from 'node:path'
32
import crypto from 'node:crypto'
43
import { createRequire } from 'node:module'
@@ -10,16 +9,8 @@ import type {
109
HtmlTagDescriptor,
1110
Plugin,
1211
ResolvedConfig,
12+
Rollup,
1313
} from 'vite'
14-
import type {
15-
NormalizedOutputOptions,
16-
OutputAsset,
17-
OutputBundle,
18-
OutputChunk,
19-
OutputOptions,
20-
PreRenderedChunk,
21-
RenderedChunk,
22-
} from 'rollup'
2314
import type {
2415
PluginItem as BabelPlugin,
2516
types as BabelTypes,
@@ -174,7 +165,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
174165
// When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the
175166
// modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them.
176167
const outputToChunkFileNameToPolyfills = new WeakMap<
177-
NormalizedOutputOptions,
168+
Rollup.NormalizedOutputOptions,
178169
Map<string, { modern: Set<string>; legacy: Set<string> }> | null
179170
>()
180171

@@ -405,10 +396,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
405396
const getLegacyOutputFileName = (
406397
fileNames:
407398
| string
408-
| ((chunkInfo: PreRenderedChunk) => string)
399+
| ((chunkInfo: Rollup.PreRenderedChunk) => string)
409400
| undefined,
410401
defaultFileName = '[name]-legacy-[hash].js',
411-
): string | ((chunkInfo: PreRenderedChunk) => string) => {
402+
): string | ((chunkInfo: Rollup.PreRenderedChunk) => string) => {
412403
if (!fileNames) {
413404
return path.posix.join(config.build.assetsDir, defaultFileName)
414405
}
@@ -437,8 +428,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
437428
}
438429

439430
const createLegacyOutput = (
440-
options: OutputOptions = {},
441-
): OutputOptions => {
431+
options: Rollup.OutputOptions = {},
432+
): Rollup.OutputOptions => {
442433
return {
443434
...options,
444435
format: 'system',
@@ -798,11 +789,11 @@ function createBabelPresetEnvOptions(
798789
async function buildPolyfillChunk(
799790
mode: string,
800791
imports: Set<string>,
801-
bundle: OutputBundle,
792+
bundle: Rollup.OutputBundle,
802793
facadeToChunkMap: Map<string, string>,
803794
buildOptions: BuildOptions,
804795
format: 'iife' | 'es',
805-
rollupOutputOptions: NormalizedOutputOptions,
796+
rollupOutputOptions: Rollup.NormalizedOutputOptions,
806797
excludeSystemJS?: boolean,
807798
prependModenChunkLegacyGuard?: boolean,
808799
) {
@@ -852,7 +843,7 @@ async function buildPolyfillChunk(
852843
if (!('output' in _polyfillChunk)) return
853844
const polyfillChunk = _polyfillChunk.output.find(
854845
(chunk) => chunk.type === 'chunk' && chunk.isEntry,
855-
) as OutputChunk
846+
) as Rollup.OutputChunk
856847

857848
// associate the polyfill chunk to every entry chunk so that we can retrieve
858849
// the polyfill filename in index html transform
@@ -870,7 +861,7 @@ async function buildPolyfillChunk(
870861
(chunk) =>
871862
chunk.type === 'asset' &&
872863
chunk.fileName === polyfillChunk.sourcemapFileName,
873-
) as OutputAsset | undefined
864+
) as Rollup.OutputAsset | undefined
874865
if (polyfillChunkMapAsset) {
875866
bundle[polyfillChunk.sourcemapFileName] = polyfillChunkMapAsset
876867
}
@@ -923,13 +914,16 @@ function prependModenChunkLegacyGuardPlugin(): Plugin {
923914
}
924915
}
925916

926-
function isLegacyChunk(chunk: RenderedChunk, options: NormalizedOutputOptions) {
917+
function isLegacyChunk(
918+
chunk: Rollup.RenderedChunk,
919+
options: Rollup.NormalizedOutputOptions,
920+
) {
927921
return options.format === 'system' && chunk.fileName.includes('-legacy')
928922
}
929923

930924
function isLegacyBundle(
931-
bundle: OutputBundle,
932-
options: NormalizedOutputOptions,
925+
bundle: Rollup.OutputBundle,
926+
options: Rollup.NormalizedOutputOptions,
933927
) {
934928
if (options.format === 'system') {
935929
const entryChunk = Object.values(bundle).find(

0 commit comments

Comments
 (0)