Skip to content

Commit

Permalink
chore(bundler-webpack): use webpack internal types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed May 21, 2024
1 parent 90a11d9 commit adc6e81
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 416 deletions.
44 changes: 24 additions & 20 deletions packages/bundler-webpack/src/build/ssr/createClientPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { fs } from '@vuepress/utils'
import type { WebpackPluginInstance } from 'webpack'
import type { FnModules, StatsToJsonOutput } from '../../types.webpack.js'
import type { StatsModule, WebpackPluginInstance } from 'webpack'
import { isCSS, isJS } from './utils.js'

export interface ClientManifest {
Expand Down Expand Up @@ -29,18 +28,20 @@ export const createClientPlugin = (
modules = [],
entrypoints = {},
chunks = [],
}: StatsToJsonOutput = compilation
.getStats()
.toJson() as unknown as StatsToJsonOutput
} = compilation.getStats().toJson()

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

// get initial entry files
const initialFiles = Object.keys(entrypoints)
.map((name) => entrypoints[name].assets.map((item) => item.name))
.reduce((assets, all) => all.concat(assets), [])
.filter((file) => isJS(file) || isCSS(file))
const initialFiles =
Object.keys(entrypoints)
.map(
(name) =>
entrypoints[name].assets?.map((item) => item.name) ?? [],
)
.reduce((assets, all) => all.concat(assets), [])
.filter((file) => isJS(file) || isCSS(file)) ?? []

// get files that should be loaded asynchronously
// i.e. script and style files that are not included in the initial entry files
Expand All @@ -51,22 +52,25 @@ export const createClientPlugin = (

// get asset modules
const assetModules = modules.filter(
(m): m is FnModules & Required<Pick<FnModules, 'assets'>> =>
!!(m.assets && m.assets.length),
(
module,
): module is StatsModule & Required<Pick<StatsModule, 'assets'>> =>
Boolean(module.assets?.length),
)

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

const fileToIndex = (file: string): number => allFiles.indexOf(file)
const fileToIndex = (file: number | string): number =>
allFiles.indexOf(file.toString())

modules.forEach((m) => {
modules.forEach((module) => {
// ignore modules duplicated in multiple chunks
if (m.chunks.length !== 1) {
if (module.chunks?.length !== 1) {
return
}

const cid = m.chunks[0]
const cid = module.chunks[0]
const chunk = chunks.find((c) => c.id === cid)

if (!chunk || !chunk.files) {
Expand All @@ -75,21 +79,21 @@ export const createClientPlugin = (

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

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

// find all asset modules associated with the same chunk
assetModules.forEach((m) => {
if (m.chunks.some((id) => id === cid)) {
assetModules.forEach((module) => {
if (module.chunks?.some((id) => id === cid)) {
// get asset files
files.push(...m.assets.map(fileToIndex))
files.push(...module.assets.map(fileToIndex))
}
})

// map the module request to files index
manifestModules[request] = files
if (request) manifestModules[request] = files
})

// generate client manifest json file
Expand Down
13 changes: 9 additions & 4 deletions packages/bundler-webpack/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { VueLoaderOptions } from 'vue-loader'
import type { Configuration as WebpackConfiguration } from 'webpack'
import type {
LoaderContext,
Configuration as WebpackConfiguration,
} from 'webpack'
import type WebpackChainConfig from 'webpack-chain'
import type WebpackDevServer from 'webpack-dev-server'
import type { LoaderContext } from './types.webpack.js'

export type {
VueLoaderOptions,
Expand Down Expand Up @@ -82,15 +84,18 @@ export interface LoaderOptions {
webpackImporter?: boolean
additionalData?:
| string
| ((content: string, loaderContext: LoaderContext) => string)
| ((
content: string,
loaderContext: LoaderContext<Record<string, any>>,
) => string)
}

/**
* Common type for style pre-processor options
*/
export type StylePreprocessorOptions<
T extends Record<string, any> = Record<string, any>,
> = T | ((loaderContext: LoaderContext) => TextDecodeOptions)
> = T | ((loaderContext: LoaderContext<T>) => TextDecodeOptions)

/**
* Options for postcss-loader
Expand Down

0 comments on commit adc6e81

Please sign in to comment.