From 47edee19888d9750633e369e74291fd6d166a1c9 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Tue, 10 Sep 2024 03:56:43 +0800 Subject: [PATCH] refactor(bundler-webpack): improve build dir structure and e2e scripts of webpack --- e2e/package.json | 8 ++++---- e2e/playwright.config.ts | 2 +- package.json | 1 + packages/bundler-webpack/src/build/build.ts | 2 +- .../bundler-webpack/src/build/createClientConfig.ts | 2 +- .../src/build/{ssr => }/createClientPlugin.ts | 11 ++++------- .../src/build/resolveClientManifestMeta.ts | 3 +-- packages/bundler-webpack/src/build/ssr/index.ts | 2 -- packages/bundler-webpack/src/build/ssr/utils.ts | 3 --- packages/bundler-webpack/src/build/types.ts | 10 ++++++++++ .../src/{build/ssr => loaders}/vuepressSsrLoader.cts | 0 packages/bundler-webpack/tsup.config.ts | 2 +- 12 files changed, 24 insertions(+), 22 deletions(-) rename packages/bundler-webpack/src/build/{ssr => }/createClientPlugin.ts (94%) delete mode 100644 packages/bundler-webpack/src/build/ssr/index.ts delete mode 100644 packages/bundler-webpack/src/build/ssr/utils.ts rename packages/bundler-webpack/src/{build/ssr => loaders}/vuepressSsrLoader.cts (100%) diff --git a/e2e/package.json b/e2e/package.json index 69ad711431..556330efff 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -5,15 +5,15 @@ "type": "module", "scripts": { "docs:build": "vuepress build docs --clean-cache --clean-temp", - "docs:build-webpack": "cross-env E2E_BUNDLER=webpack pnpm docs:build", + "docs:build:webpack": "cross-env E2E_BUNDLER=webpack pnpm docs:build", "docs:clean": "rimraf docs/.vuepress/.temp docs/.vuepress/.cache docs/.vuepress/dist", "docs:dev": "vuepress dev docs --clean-cache --clean-temp", - "docs:dev-webpack": "cross-env E2E_BUNDLER=webpack pnpm docs:dev", + "docs:dev:webpack": "cross-env E2E_BUNDLER=webpack pnpm docs:dev", "docs:serve": "anywhere -s -h localhost -p 9080 -d docs/.vuepress/dist", "e2e:build": "cross-env E2E_COMMAND=build playwright test", - "e2e:build-webpack": "cross-env E2E_COMMAND=build E2E_BUNDLER=webpack playwright test", + "e2e:build:webpack": "cross-env E2E_COMMAND=build E2E_BUNDLER=webpack playwright test", "e2e:dev": "cross-env E2E_COMMAND=dev playwright test", - "e2e:dev-webpack": "cross-env E2E_COMMAND=dev E2E_BUNDLER=webpack playwright test" + "e2e:dev:webpack": "cross-env E2E_COMMAND=dev E2E_BUNDLER=webpack playwright test" }, "dependencies": { "@vuepress-e2e/conditional-exports": "file:./modules/conditional-exports", diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts index 8bfcec75b8..fba829000e 100644 --- a/e2e/playwright.config.ts +++ b/e2e/playwright.config.ts @@ -2,7 +2,7 @@ import { defineConfig, devices } from '@playwright/test' import { BASE, BUNDLER, IS_CI, IS_DEV } from './utils/env' const COMMAND_PART1 = IS_DEV ? 'docs:dev' : 'docs:build' -const COMMAND_PART2 = BUNDLER === 'vite' ? '' : `-${BUNDLER}` +const COMMAND_PART2 = BUNDLER === 'vite' ? '' : `:${BUNDLER}` const COMMAND_PART3 = IS_DEV ? '' : ' && pnpm docs:serve' export default defineConfig({ diff --git a/package.json b/package.json index 1b5258220e..f08c8b3ab7 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "release:version": "bumpp -r --execute=\"pnpm release:changelog\" --commit \"build: publish v%s\" --all", "test": "pnpm test:unit && pnpm test:e2e", "test:e2e": "pnpm --filter e2e e2e:dev && pnpm --filter e2e e2e:build", + "test:e2e:webpack": "pnpm --filter e2e e2e:dev:webpack && pnpm --filter e2e e2e:build:webpack", "test:unit": "vitest run", "test:unit:cov": "vitest run --coverage" }, diff --git a/packages/bundler-webpack/src/build/build.ts b/packages/bundler-webpack/src/build/build.ts index 72ddb08331..82774429d3 100644 --- a/packages/bundler-webpack/src/build/build.ts +++ b/packages/bundler-webpack/src/build/build.ts @@ -18,7 +18,7 @@ import { import { createServerConfig } from './createServerConfig.js' import { renderPage } from './renderPage.js' import { resolveClientManifestMeta } from './resolveClientManifestMeta.js' -import type { ClientManifest } from './ssr/index.js' +import type { ClientManifest } from './types.js' const log = debug('vuepress:bundler-webpack/build') diff --git a/packages/bundler-webpack/src/build/createClientConfig.ts b/packages/bundler-webpack/src/build/createClientConfig.ts index 7530be9e07..6266b9bda1 100644 --- a/packages/bundler-webpack/src/build/createClientConfig.ts +++ b/packages/bundler-webpack/src/build/createClientConfig.ts @@ -8,7 +8,7 @@ import MiniCssExtractPlugin from 'mini-css-extract-plugin' import type Config from 'webpack-5-chain' import { createClientBaseConfig } from '../config/index.js' import type { WebpackBundlerOptions } from '../types.js' -import { createClientPlugin } from './ssr/index.js' +import { createClientPlugin } from './createClientPlugin.js' const require = createRequire(import.meta.url) diff --git a/packages/bundler-webpack/src/build/ssr/createClientPlugin.ts b/packages/bundler-webpack/src/build/createClientPlugin.ts similarity index 94% rename from packages/bundler-webpack/src/build/ssr/createClientPlugin.ts rename to packages/bundler-webpack/src/build/createClientPlugin.ts index 757500b464..b375d4134c 100644 --- a/packages/bundler-webpack/src/build/ssr/createClientPlugin.ts +++ b/packages/bundler-webpack/src/build/createClientPlugin.ts @@ -1,13 +1,10 @@ import { fs } from '@vuepress/utils' import type { StatsModule, WebpackPluginInstance } from 'webpack' -import { isCSS, isJS } from './utils.js' +import type { ClientManifest } from './types.js' -export interface ClientManifest { - all: string[] - initial: string[] - async: string[] - modules: Record -} +const isJS = (file: string): boolean => /\.js(\?[^.]+)?$/.test(file) + +const isCSS = (file: string): boolean => /\.css(\?[^.]+)?$/.test(file) /** * Vuepress client plugin diff --git a/packages/bundler-webpack/src/build/resolveClientManifestMeta.ts b/packages/bundler-webpack/src/build/resolveClientManifestMeta.ts index 837c4b1efb..8cef9fdb27 100644 --- a/packages/bundler-webpack/src/build/resolveClientManifestMeta.ts +++ b/packages/bundler-webpack/src/build/resolveClientManifestMeta.ts @@ -1,6 +1,5 @@ import { resolveFileMeta } from './resolveFileMeta.js' -import type { ClientManifest } from './ssr/index.js' -import type { FileMeta, ModuleFilesMetaMap } from './types.js' +import type { ClientManifest, FileMeta, ModuleFilesMetaMap } from './types.js' /** * Resolve files meta from clientManifest diff --git a/packages/bundler-webpack/src/build/ssr/index.ts b/packages/bundler-webpack/src/build/ssr/index.ts deleted file mode 100644 index 22766860f3..0000000000 --- a/packages/bundler-webpack/src/build/ssr/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './createClientPlugin.js' -export * from './utils.js' diff --git a/packages/bundler-webpack/src/build/ssr/utils.ts b/packages/bundler-webpack/src/build/ssr/utils.ts deleted file mode 100644 index 33be37d50f..0000000000 --- a/packages/bundler-webpack/src/build/ssr/utils.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const isJS = (file: string): boolean => /\.js(\?[^.]+)?$/.test(file) - -export const isCSS = (file: string): boolean => /\.css(\?[^.]+)?$/.test(file) diff --git a/packages/bundler-webpack/src/build/types.ts b/packages/bundler-webpack/src/build/types.ts index 112392f4cf..5eaf515096 100644 --- a/packages/bundler-webpack/src/build/types.ts +++ b/packages/bundler-webpack/src/build/types.ts @@ -27,3 +27,13 @@ export type FileMetaType = '' | 'font' | 'image' | 'script' | 'style' * A "module request" to "client files meta" key-value map */ export type ModuleFilesMetaMap = Record + +/** + * Client manifest that collected from webpack stats + */ +export interface ClientManifest { + all: string[] + initial: string[] + async: string[] + modules: Record +} diff --git a/packages/bundler-webpack/src/build/ssr/vuepressSsrLoader.cts b/packages/bundler-webpack/src/loaders/vuepressSsrLoader.cts similarity index 100% rename from packages/bundler-webpack/src/build/ssr/vuepressSsrLoader.cts rename to packages/bundler-webpack/src/loaders/vuepressSsrLoader.cts diff --git a/packages/bundler-webpack/tsup.config.ts b/packages/bundler-webpack/tsup.config.ts index fbd20132f9..4899bc89aa 100644 --- a/packages/bundler-webpack/tsup.config.ts +++ b/packages/bundler-webpack/tsup.config.ts @@ -18,7 +18,7 @@ export default defineConfig([ { ...shared, entry: { - 'vuepress-ssr-loader': './src/build/ssr/vuepressSsrLoader.cts', + 'vuepress-ssr-loader': './src/loaders/vuepressSsrLoader.cts', }, format: ['cjs'], },