Skip to content

Commit abe7bf9

Browse files
authored
refactor(bundler-webpack): use webpack internal types (#1565)
1 parent 90a11d9 commit abe7bf9

File tree

3 files changed

+27
-412
lines changed

3 files changed

+27
-412
lines changed

packages/bundler-webpack/src/build/ssr/createClientPlugin.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { fs } from '@vuepress/utils'
2-
import type { WebpackPluginInstance } from 'webpack'
3-
import type { FnModules, StatsToJsonOutput } from '../../types.webpack.js'
2+
import type { StatsModule, WebpackPluginInstance } from 'webpack'
43
import { isCSS, isJS } from './utils.js'
54

65
export interface ClientManifest {
@@ -29,18 +28,20 @@ export const createClientPlugin = (
2928
modules = [],
3029
entrypoints = {},
3130
chunks = [],
32-
}: StatsToJsonOutput = compilation
33-
.getStats()
34-
.toJson() as unknown as StatsToJsonOutput
31+
} = compilation.getStats().toJson()
3532

3633
// get all files
3734
const allFiles = assets.map((a) => a.name)
3835

3936
// get initial entry files
40-
const initialFiles = Object.keys(entrypoints)
41-
.map((name) => entrypoints[name].assets.map((item) => item.name))
42-
.reduce((assets, all) => all.concat(assets), [])
43-
.filter((file) => isJS(file) || isCSS(file))
37+
const initialFiles =
38+
Object.keys(entrypoints)
39+
.map(
40+
(name) =>
41+
entrypoints[name].assets?.map((item) => item.name) ?? [],
42+
)
43+
.reduce((assets, all) => all.concat(assets), [])
44+
.filter((file) => isJS(file) || isCSS(file)) ?? []
4445

4546
// get files that should be loaded asynchronously
4647
// i.e. script and style files that are not included in the initial entry files
@@ -51,18 +52,19 @@ export const createClientPlugin = (
5152

5253
// get asset modules
5354
const assetModules = modules.filter(
54-
(m): m is FnModules & Required<Pick<FnModules, 'assets'>> =>
55-
!!(m.assets && m.assets.length),
55+
(m): m is StatsModule & Required<Pick<StatsModule, 'assets'>> =>
56+
Boolean(m.assets?.length),
5657
)
5758

5859
// get modules for client manifest
5960
const manifestModules: ClientManifest['modules'] = {}
6061

61-
const fileToIndex = (file: string): number => allFiles.indexOf(file)
62+
const fileToIndex = (file: number | string): number =>
63+
allFiles.indexOf(file.toString())
6264

6365
modules.forEach((m) => {
6466
// ignore modules duplicated in multiple chunks
65-
if (m.chunks.length !== 1) {
67+
if (m.chunks?.length !== 1) {
6668
return
6769
}
6870

@@ -75,21 +77,21 @@ export const createClientPlugin = (
7577

7678
// remove appended hash of module identifier
7779
// which is the request string of the module
78-
const request = m.identifier.replace(/\|\w+$/, '')
80+
const request = m.identifier?.replace(/\|\w+$/, '')
7981

8082
// get chunk files index
8183
const files = [...chunk.files.map(fileToIndex)]
8284

8385
// find all asset modules associated with the same chunk
8486
assetModules.forEach((m) => {
85-
if (m.chunks.some((id) => id === cid)) {
87+
if (m.chunks?.some((id) => id === cid)) {
8688
// get asset files
8789
files.push(...m.assets.map(fileToIndex))
8890
}
8991
})
9092

9193
// map the module request to files index
92-
manifestModules[request] = files
94+
if (request) manifestModules[request] = files
9395
})
9496

9597
// generate client manifest json file

packages/bundler-webpack/src/types.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { VueLoaderOptions } from 'vue-loader'
2-
import type { Configuration as WebpackConfiguration } from 'webpack'
2+
import type {
3+
LoaderContext,
4+
Configuration as WebpackConfiguration,
5+
} from 'webpack'
36
import type WebpackChainConfig from 'webpack-chain'
47
import type WebpackDevServer from 'webpack-dev-server'
5-
import type { LoaderContext } from './types.webpack.js'
68

79
export type {
810
VueLoaderOptions,
@@ -82,15 +84,18 @@ export interface LoaderOptions {
8284
webpackImporter?: boolean
8385
additionalData?:
8486
| string
85-
| ((content: string, loaderContext: LoaderContext) => string)
87+
| ((
88+
content: string,
89+
loaderContext: LoaderContext<Record<string, any>>,
90+
) => string)
8691
}
8792

8893
/**
8994
* Common type for style pre-processor options
9095
*/
9196
export type StylePreprocessorOptions<
9297
T extends Record<string, any> = Record<string, any>,
93-
> = T | ((loaderContext: LoaderContext) => TextDecodeOptions)
98+
> = T | ((loaderContext: LoaderContext<T>) => TextDecodeOptions)
9499

95100
/**
96101
* Options for postcss-loader

0 commit comments

Comments
 (0)