From cbe7b82cecad377ecf6a27fecf513f39e69fe5e7 Mon Sep 17 00:00:00 2001 From: renzp94 <1074720760@qq.com> Date: Wed, 29 May 2024 10:11:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?chore:=20=E5=B0=86webpack=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=95=B4=E7=90=86=E5=88=B0webpack=E7=9B=AE=E5=BD=95=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rzpack/src/cli.ts | 23 ++++------- .../rzpack/src/{configs/index.ts => ctx.ts} | 41 ++++--------------- packages/rzpack/src/index.ts | 12 +++--- .../rzpack/src/{ => webpack}/assets/css.ts | 10 ++--- .../rzpack/src/{ => webpack}/assets/font.ts | 0 .../rzpack/src/{ => webpack}/assets/image.ts | 0 .../rzpack/src/{ => webpack}/assets/index.ts | 10 ++--- .../src/{ => webpack}/assets/jsx/esbuild.ts | 0 .../src/{ => webpack}/assets/jsx/index.ts | 8 ++-- .../src/{ => webpack}/assets/jsx/swc.ts | 6 +-- packages/rzpack/src/{ => webpack}/build.ts | 8 ++-- .../rzpack/src/{ => webpack}/configs/alias.ts | 4 +- .../rzpack/src/{ => webpack}/configs/entry.ts | 16 ++++---- .../src/{ => webpack}/configs/extensions.ts | 4 +- packages/rzpack/src/webpack/configs/index.ts | 24 +++++++++++ .../{ => webpack}/configs/lazyCompilation.ts | 4 +- .../src/{ => webpack}/configs/minimizer.ts | 6 ++- .../src/{ => webpack}/configs/output.ts | 7 +--- packages/rzpack/src/webpack/index.ts | 20 +++++++++ .../plugins/compression-webpack-plugin.ts | 0 .../plugins/copy-webpack-plugin.ts | 0 .../{ => webpack}/plugins/define-plugin.ts | 0 .../src/{ => webpack}/plugins/dll-plugin.ts | 6 +-- .../plugins/eslint-webpack-plugin.ts | 2 +- .../plugins/fork-ts-checker-webpack-plugin.ts | 2 +- .../plugins/friendly-errors-webpack-plugin.ts | 0 .../plugins/html-webpack-plugin.ts | 2 +- .../rzpack/src/{ => webpack}/plugins/index.ts | 8 ++-- .../plugins/million-webpack-plugin.ts | 0 .../plugins/mini-css-extract-plugin.ts | 0 .../plugins/module-federation-plugin.ts | 4 +- .../plugins/react-refresh-webpack-plugin.ts | 0 .../plugins/speed-measure-webpack-plugin.ts | 0 .../plugins/unplugin-build-info.ts | 0 .../plugins/webpack-bundle-analyzer.ts | 0 .../src/{ => webpack}/plugins/webpackbar.ts | 2 +- packages/rzpack/src/{ => webpack}/preview.ts | 4 +- packages/rzpack/src/{ => webpack}/server.ts | 8 ++-- 38 files changed, 118 insertions(+), 123 deletions(-) rename packages/rzpack/src/{configs/index.ts => ctx.ts} (74%) rename packages/rzpack/src/{ => webpack}/assets/css.ts (96%) rename packages/rzpack/src/{ => webpack}/assets/font.ts (100%) rename packages/rzpack/src/{ => webpack}/assets/image.ts (100%) rename packages/rzpack/src/{ => webpack}/assets/index.ts (65%) rename packages/rzpack/src/{ => webpack}/assets/jsx/esbuild.ts (100%) rename packages/rzpack/src/{ => webpack}/assets/jsx/index.ts (75%) rename packages/rzpack/src/{ => webpack}/assets/jsx/swc.ts (81%) rename packages/rzpack/src/{ => webpack}/build.ts (95%) rename packages/rzpack/src/{ => webpack}/configs/alias.ts (66%) rename packages/rzpack/src/{ => webpack}/configs/entry.ts (81%) rename packages/rzpack/src/{ => webpack}/configs/extensions.ts (57%) create mode 100644 packages/rzpack/src/webpack/configs/index.ts rename packages/rzpack/src/{ => webpack}/configs/lazyCompilation.ts (84%) rename packages/rzpack/src/{ => webpack}/configs/minimizer.ts (94%) rename packages/rzpack/src/{ => webpack}/configs/output.ts (84%) create mode 100644 packages/rzpack/src/webpack/index.ts rename packages/rzpack/src/{ => webpack}/plugins/compression-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/copy-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/define-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/dll-plugin.ts (93%) rename packages/rzpack/src/{ => webpack}/plugins/eslint-webpack-plugin.ts (96%) rename packages/rzpack/src/{ => webpack}/plugins/fork-ts-checker-webpack-plugin.ts (96%) rename packages/rzpack/src/{ => webpack}/plugins/friendly-errors-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/html-webpack-plugin.ts (93%) rename packages/rzpack/src/{ => webpack}/plugins/index.ts (93%) rename packages/rzpack/src/{ => webpack}/plugins/million-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/mini-css-extract-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/module-federation-plugin.ts (97%) rename packages/rzpack/src/{ => webpack}/plugins/react-refresh-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/speed-measure-webpack-plugin.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/unplugin-build-info.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/webpack-bundle-analyzer.ts (100%) rename packages/rzpack/src/{ => webpack}/plugins/webpackbar.ts (85%) rename packages/rzpack/src/{ => webpack}/preview.ts (88%) rename packages/rzpack/src/{ => webpack}/server.ts (93%) diff --git a/packages/rzpack/src/cli.ts b/packages/rzpack/src/cli.ts index a8c4d2c..d00f57d 100644 --- a/packages/rzpack/src/cli.ts +++ b/packages/rzpack/src/cli.ts @@ -2,11 +2,9 @@ import { cac } from 'cac' import { fileExists, logError, pathResolve } from 'rzpack-utils' import type { BuildOptions, RzpackConfigs, ServerOptions } from '.' -import runBuild from './build' -import { RzpackContext } from './configs' import { NAME, VERSION } from './constant' -import runPreview from './preview' -import runServer from './server' +import { RzpackContext } from './ctx' +import { runWebpackBuild, runWebpackPreview, runWebpackServer } from './webpack' export const rzpack = new RzpackContext() @@ -28,13 +26,12 @@ cli .option('--ui', '[boolean] startup Rzpack UI') .action(async (_: string, options: ServerOptions) => { const { c, m, mode, ui = true, config, host, port, open } = options ?? {} - rzpack.mode = m ?? mode ?? 'development' - process.env.NODE_ENV = rzpack.mode + process.env.NODE_ENV = m ?? mode ?? 'development' rzpack.webpackChain.devServer.host(host).port(port).open(open) try { const configs: RzpackConfigs = rzpack.loadConfigFile(c ?? config) await rzpack.configs(configs) - runServer(ui, configs?.proxyFile) + runWebpackServer(ui, configs?.proxyFile) } catch (error) { logError(error) } @@ -56,8 +53,7 @@ cli bundleSize, bundleTime, } = options ?? {} - rzpack.mode = m ?? mode ?? 'production' - process.env.NODE_ENV = rzpack.mode + process.env.NODE_ENV = m ?? mode ?? 'production' rzpack.bundleSize = bundleSize ?? false rzpack.bundleTime = bundleTime ?? false try { @@ -66,7 +62,7 @@ cli configs.output = outDir } await rzpack.configs(configs) - runBuild(!rzpack.bundleTime) + runWebpackBuild(!rzpack.bundleTime) } catch (error) { logError(error) } @@ -78,8 +74,7 @@ cli .option('--outDir ', '[string] output directory (default: dist)') .action(async (options: BuildOptions) => { const { c, m, mode, config, outDir = 'dist' } = options ?? {} - rzpack.mode = m ?? mode ?? 'production' - process.env.NODE_ENV = rzpack.mode + process.env.NODE_ENV = m ?? mode ?? 'production' const configs = rzpack.loadConfigFile(c ?? config) if (!configs?.output) { @@ -93,10 +88,10 @@ cli let isPreview: boolean = fileExists(fullPath) if (!isPreview) { await rzpack.configs(configs) - isPreview = await runBuild(false) + isPreview = await runWebpackBuild(false) } - runPreview(dir) + runWebpackPreview(dir) }) cli.help() diff --git a/packages/rzpack/src/configs/index.ts b/packages/rzpack/src/ctx.ts similarity index 74% rename from packages/rzpack/src/configs/index.ts rename to packages/rzpack/src/ctx.ts index 67f8041..830dcb3 100644 --- a/packages/rzpack/src/configs/index.ts +++ b/packages/rzpack/src/ctx.ts @@ -9,16 +9,9 @@ import { import { bundleTsFile } from 'rzpack-utils' import type { Configuration } from 'webpack' import WebpackChain from 'webpack-chain' -import type { RzpackConfigs, RzpackWebpackChain, Yagt } from '..' -import resolveAssets from '../assets' -import { DEFAULT_CONFIG } from '../constant' -import resolvePlugins from '../plugins' -import resolveAlias from './alias' -import resolveEntry from './entry' -import resolveExtensions from './extensions' -import resolveLazyCompilation from './lazyCompilation' -import resolveMinimizer from './minimizer' -import resolveOutput from './output' +import type { RzpackConfigs, RzpackWebpackChain, Yagt } from '.' +import { DEFAULT_CONFIG } from './constant' +import { loadWebpackConfigs } from './webpack' export interface RzpackContextConfigs extends Configuration { network?: string @@ -53,7 +46,6 @@ export const getBuildTmpFilePath = (filename: string) => { export class RzpackContext { public readonly webpackChain: WebpackChain - public mode: 'development' | 'production' public cache: boolean public bundleSize: boolean public bundleTime: boolean @@ -70,9 +62,9 @@ export class RzpackContext { return this.webpackChain.get(key) } loadConfigFile(configFilePath?: string): RzpackConfigs { - let configFile - let configFileName - loadEnv(this.mode) + let configFile: string + let configFileName: string + loadEnv(process.env.NODE_ENV) loadEnv() if (configFilePath) { @@ -105,33 +97,18 @@ export class RzpackContext { async configs(configs: RzpackConfigs) { if (typeof configs === 'object') { const { - entry, - output, - assets, cache = true, - publicPath = DEFAULT_CONFIG.STATIC_DIR, webpackChain: resolveWebpackChain, server, - lazyCompilation, yagt, } = configs this.cache = cache this.yagt = yagt - resolveEntry(this.webpackChain, entry) - resolveOutput(this.webpackChain, output) - resolveAlias(this.webpackChain) - resolveExtensions(this.webpackChain) - resolveAssets(this.webpackChain, { ...configs, publicPath }) - resolveLazyCompilation(this.webpackChain, lazyCompilation) - await resolvePlugins(this.webpackChain, { ...configs, publicPath }) - if (this.mode === 'production') { - resolveMinimizer(this.webpackChain, assets?.jsxTools, assets?.imageMini) - } - + await loadWebpackConfigs(this.webpackChain, configs) const { network, local, port } = await getNetwork( server?.port as unknown as number, ) - if (this.mode === 'development') { + if (process.env.NODE_ENV === 'development') { this.set('network', network) this.set('local', local) this.set('port', port) @@ -148,7 +125,7 @@ export class RzpackContext { } else { console.log(logError('配置文件配置有误,请导出一个函数或对象')) } - this.webpackChain.mode(this.mode) + this.webpackChain.mode(process.env.NODE_ENV as 'development' | 'production') } toConfig() { return this.webpackChain.toConfig() as unknown as RzpackContextConfigs diff --git a/packages/rzpack/src/index.ts b/packages/rzpack/src/index.ts index cb26c78..a6c2619 100644 --- a/packages/rzpack/src/index.ts +++ b/packages/rzpack/src/index.ts @@ -1,12 +1,12 @@ import type HtmlWebpackPlugin from 'html-webpack-plugin' import type WebpackChain from 'webpack-chain' import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server' -import type { RzpackAssets } from './assets' -import type { LazyCompilationOptions } from './configs/lazyCompilation' -import type { Output } from './configs/output' -import type { MillionOptions } from './plugins/million-webpack-plugin' -import type { ModuleFederationPluginOptions } from './plugins/module-federation-plugin' -import type { BuildInfoWebpackPluginOptions } from './plugins/unplugin-build-info' +import type { RzpackAssets } from './webpack/assets' +import type { LazyCompilationOptions } from './webpack/configs/lazyCompilation' +import type { Output } from './webpack/configs/output' +import type { MillionOptions } from './webpack/plugins/million-webpack-plugin' +import type { ModuleFederationPluginOptions } from './webpack/plugins/module-federation-plugin' +import type { BuildInfoWebpackPluginOptions } from './webpack/plugins/unplugin-build-info' interface CLIOptions { '--'?: string[] diff --git a/packages/rzpack/src/assets/css.ts b/packages/rzpack/src/webpack/assets/css.ts similarity index 96% rename from packages/rzpack/src/assets/css.ts rename to packages/rzpack/src/webpack/assets/css.ts index 7d7139e..98d6e52 100644 --- a/packages/rzpack/src/assets/css.ts +++ b/packages/rzpack/src/webpack/assets/css.ts @@ -8,9 +8,9 @@ import { requireResolve, } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import { rzpack } from '../cli' -import { getBuildTmpFilePath } from '../configs' -import type { LessVars, RzpackConfigs } from './../index' +import { rzpack } from '../../cli' +import { getBuildTmpFilePath } from '../../ctx' +import type { LessVars, RzpackConfigs } from '../../index' /** * 应用公共css loader @@ -21,7 +21,7 @@ const applyCommonLoader = ( rule: WebpackChain.Rule>, cssScoped?: boolean, ) => { - if (rzpack.mode === 'development') { + if (process.env.NODE_ENV === 'development') { rule.use('style-loader').loader(requireResolve('style-loader')) } else { rule.use('mini-css-extract-loader').loader(MiniCssExtractPlugin.loader) @@ -112,7 +112,7 @@ const useAntdTheme = (antdTheme: LessVars) => { } let antdVersion = getPackageVersion('antd') if (antdVersion < 5 && antdTheme.vars) { - antdVersion = parseInt(antdVersion) + antdVersion = Number.parseInt(antdVersion) } // 直接定义的变量优先级高于变量文件 diff --git a/packages/rzpack/src/assets/font.ts b/packages/rzpack/src/webpack/assets/font.ts similarity index 100% rename from packages/rzpack/src/assets/font.ts rename to packages/rzpack/src/webpack/assets/font.ts diff --git a/packages/rzpack/src/assets/image.ts b/packages/rzpack/src/webpack/assets/image.ts similarity index 100% rename from packages/rzpack/src/assets/image.ts rename to packages/rzpack/src/webpack/assets/image.ts diff --git a/packages/rzpack/src/assets/index.ts b/packages/rzpack/src/webpack/assets/index.ts similarity index 65% rename from packages/rzpack/src/assets/index.ts rename to packages/rzpack/src/webpack/assets/index.ts index 91bfde5..da1107b 100644 --- a/packages/rzpack/src/assets/index.ts +++ b/packages/rzpack/src/webpack/assets/index.ts @@ -1,7 +1,7 @@ -import { FilterFn } from 'image-minimizer-webpack-plugin' +import type { FilterFn } from 'image-minimizer-webpack-plugin' import type WebpackChain from 'webpack-chain' -import type { JSX_TOOLS } from '../' -import type { RzpackConfigs } from './../index' +import type { JSX_TOOLS } from '../../' +import type { RzpackConfigs } from '../../index' import css from './css' import font from './font' import image from './image' @@ -16,11 +16,9 @@ export interface RzpackAssets { imageMini?: boolean | FilterFn } -const resolveAssets = (webpackChain: WebpackChain, options: RzpackConfigs) => { +export default (webpackChain: WebpackChain, options: RzpackConfigs) => { jsx(webpackChain, options?.assets) font(webpackChain) image(webpackChain) css(webpackChain, options) } - -export default resolveAssets diff --git a/packages/rzpack/src/assets/jsx/esbuild.ts b/packages/rzpack/src/webpack/assets/jsx/esbuild.ts similarity index 100% rename from packages/rzpack/src/assets/jsx/esbuild.ts rename to packages/rzpack/src/webpack/assets/jsx/esbuild.ts diff --git a/packages/rzpack/src/assets/jsx/index.ts b/packages/rzpack/src/webpack/assets/jsx/index.ts similarity index 75% rename from packages/rzpack/src/assets/jsx/index.ts rename to packages/rzpack/src/webpack/assets/jsx/index.ts index 6997daf..ba31557 100644 --- a/packages/rzpack/src/assets/jsx/index.ts +++ b/packages/rzpack/src/webpack/assets/jsx/index.ts @@ -1,11 +1,11 @@ import { requireResolve } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import { JSX_TOOLS } from '../..' -import { RzpackAssets } from './../index' +import { JSX_TOOLS } from '../../..' +import type { RzpackAssets } from '../index' import esbuild from './esbuild' import swc from './swc' -const jsx = (webpackChain: WebpackChain, assets: RzpackAssets) => { +export default (webpackChain: WebpackChain, assets: RzpackAssets) => { const { jsxTools = JSX_TOOLS.ESBUILD, cssScoped } = assets ?? {} const transformTools = { [JSX_TOOLS.ESBUILD]: esbuild, @@ -20,5 +20,3 @@ const jsx = (webpackChain: WebpackChain, assets: RzpackAssets) => { .end() } } - -export default jsx diff --git a/packages/rzpack/src/assets/jsx/swc.ts b/packages/rzpack/src/webpack/assets/jsx/swc.ts similarity index 81% rename from packages/rzpack/src/assets/jsx/swc.ts rename to packages/rzpack/src/webpack/assets/jsx/swc.ts index b6d09a9..46de4e8 100644 --- a/packages/rzpack/src/assets/jsx/swc.ts +++ b/packages/rzpack/src/webpack/assets/jsx/swc.ts @@ -1,6 +1,6 @@ import { requireResolve } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import { rzpack } from '../../cli' +import { rzpack } from '../../../cli' export default (webpackChain: WebpackChain) => { return webpackChain.module @@ -23,12 +23,12 @@ export default (webpackChain: WebpackChain) => { transform: { react: { runtime: 'automatic', - development: rzpack.mode === 'development', + development: process.env.NODE_ENV === 'development', useBuiltins: true, }, }, }, - minify: rzpack.mode === 'production', + minify: process.env.NODE_ENV === 'production', }) .end() } diff --git a/packages/rzpack/src/build.ts b/packages/rzpack/src/webpack/build.ts similarity index 95% rename from packages/rzpack/src/build.ts rename to packages/rzpack/src/webpack/build.ts index 1b91a5a..e859221 100644 --- a/packages/rzpack/src/build.ts +++ b/packages/rzpack/src/webpack/build.ts @@ -9,9 +9,9 @@ import { lightYellow, } from 'rzpack-utils' import Webpack from 'webpack' -import { rzpack } from './cli' +import { rzpack } from '../cli' -const runBuild = (isLog = true) => { +export default (isLog = true) => { // 抽离公共部分 rzpack.webpackChain // 抛出错误之后停止打包 @@ -38,7 +38,7 @@ const runBuild = (isLog = true) => { if (rzpack.cache) { rzpack.webpackChain.cache({ type: 'filesystem', - name: `${rzpack.mode}-cache`, + name: `${process.env.NODE_ENV}-cache`, version: createEnvHash(raw), cacheDirectory: getFileFullPath('./node_modules/.cache'), store: 'pack', @@ -123,5 +123,3 @@ const logBuildAssets = (stats) => { console.log(`\n✨ Done in ${(time / 1000).toPrecision(2)}s.`) } - -export default runBuild diff --git a/packages/rzpack/src/configs/alias.ts b/packages/rzpack/src/webpack/configs/alias.ts similarity index 66% rename from packages/rzpack/src/configs/alias.ts rename to packages/rzpack/src/webpack/configs/alias.ts index 6ac8156..c017e4b 100644 --- a/packages/rzpack/src/configs/alias.ts +++ b/packages/rzpack/src/webpack/configs/alias.ts @@ -1,8 +1,6 @@ import { pathResolve } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -const resolveAlias = (webpackChain: WebpackChain) => { +export default (webpackChain: WebpackChain) => { webpackChain.resolve.alias.set('@', pathResolve('src', process.cwd())) } - -export default resolveAlias diff --git a/packages/rzpack/src/configs/entry.ts b/packages/rzpack/src/webpack/configs/entry.ts similarity index 81% rename from packages/rzpack/src/configs/entry.ts rename to packages/rzpack/src/webpack/configs/entry.ts index fc2b2da..4a26166 100644 --- a/packages/rzpack/src/configs/entry.ts +++ b/packages/rzpack/src/webpack/configs/entry.ts @@ -1,6 +1,6 @@ import { logError } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import { DEFAULT_CONFIG } from '../constant' +import { DEFAULT_CONFIG } from '../../constant' const getEntryKey = (entry: string) => entry @@ -11,7 +11,7 @@ const getEntryKey = (entry: string) => .replace('.ts', '') .replace('.js', '') -const resolveEntry = ( +export default ( webpackChain: WebpackChain, entry: string | string[] | Record, ) => { @@ -23,7 +23,7 @@ const resolveEntry = ( webpackChain.entry(key).add(entry as string) break } - case entry instanceof Array: { + case Array.isArray(entry): { // eslint-disable-next-line no-case-declarations const entryOption = (entry as Array).map((item: string) => { if (typeof item !== 'string') { @@ -45,16 +45,14 @@ const resolveEntry = ( }) break } - case typeof entry === 'object': - entryOption.forEach((item: Record) => { - const key = Object.keys(item)[0] - webpackChain.entry(key).add(item[key]) + case typeof entry === 'object': { + Object.keys(entry).forEach((key: string) => { + webpackChain.entry(key).add(entry[key]) }) break + } } } else { webpackChain.entry('main').add(DEFAULT_CONFIG.ENTRY) } } - -export default resolveEntry diff --git a/packages/rzpack/src/configs/extensions.ts b/packages/rzpack/src/webpack/configs/extensions.ts similarity index 57% rename from packages/rzpack/src/configs/extensions.ts rename to packages/rzpack/src/webpack/configs/extensions.ts index 4a487d7..2eaa41f 100644 --- a/packages/rzpack/src/configs/extensions.ts +++ b/packages/rzpack/src/webpack/configs/extensions.ts @@ -1,7 +1,5 @@ import type WebpackChain from 'webpack-chain' -const resolveExtensions = (webpackChain: WebpackChain) => { +export default (webpackChain: WebpackChain) => { webpackChain.resolve.extensions.add('.jsx').add('.js').add('.ts').add('.tsx') } - -export default resolveExtensions diff --git a/packages/rzpack/src/webpack/configs/index.ts b/packages/rzpack/src/webpack/configs/index.ts new file mode 100644 index 0000000..ffd49e5 --- /dev/null +++ b/packages/rzpack/src/webpack/configs/index.ts @@ -0,0 +1,24 @@ +import type WebpackChain from 'webpack-chain' +import type { RzpackConfigs } from '../..' +import loadAliasConfigs from './alias' +import loadEntryConfigs from './entry' +import loadExtensionsConfigs from './extensions' +import loadLazyCompilationConfigs from './lazyCompilation' +import loadMinimizerConfigs from './minimizer' +import loadOutputConfigs from './output' +const loadBaseConfigs = ( + webpackChain: WebpackChain, + configs: RzpackConfigs, +) => { + const { entry, output, assets, lazyCompilation } = configs + loadAliasConfigs(webpackChain) + loadEntryConfigs(webpackChain, entry) + loadExtensionsConfigs(webpackChain) + loadOutputConfigs(webpackChain, output) + loadLazyCompilationConfigs(webpackChain, lazyCompilation) + if (process.env.NODE_ENV === 'production') { + loadMinimizerConfigs(webpackChain, assets?.jsxTools, assets?.imageMini) + } +} + +export default loadBaseConfigs diff --git a/packages/rzpack/src/configs/lazyCompilation.ts b/packages/rzpack/src/webpack/configs/lazyCompilation.ts similarity index 84% rename from packages/rzpack/src/configs/lazyCompilation.ts rename to packages/rzpack/src/webpack/configs/lazyCompilation.ts index 0fd13d2..ac0208d 100644 --- a/packages/rzpack/src/configs/lazyCompilation.ts +++ b/packages/rzpack/src/webpack/configs/lazyCompilation.ts @@ -9,11 +9,9 @@ export interface LazyCompilationOptions { } // 懒编译 -const resolveLazyCompilation = ( +export default ( webpackChain: WebpackChain, lazyCompilation: boolean | LazyCompilationOptions, ) => { webpackChain.set('experiments', { lazyCompilation }) } - -export default resolveLazyCompilation diff --git a/packages/rzpack/src/configs/minimizer.ts b/packages/rzpack/src/webpack/configs/minimizer.ts similarity index 94% rename from packages/rzpack/src/configs/minimizer.ts rename to packages/rzpack/src/webpack/configs/minimizer.ts index 90f533c..28eea13 100644 --- a/packages/rzpack/src/configs/minimizer.ts +++ b/packages/rzpack/src/webpack/configs/minimizer.ts @@ -1,8 +1,10 @@ import { ESBuildMinifyPlugin } from 'esbuild-loader' -import ImageMinimizerPlugin, { FilterFn } from 'image-minimizer-webpack-plugin' +import ImageMinimizerPlugin, { + type FilterFn, +} from 'image-minimizer-webpack-plugin' import type WebpackChain from 'webpack-chain' import type { Optimization } from 'webpack-chain' -import { JSX_TOOLS } from '..' +import type { JSX_TOOLS } from '../..' /** * esbuild压缩 diff --git a/packages/rzpack/src/configs/output.ts b/packages/rzpack/src/webpack/configs/output.ts similarity index 84% rename from packages/rzpack/src/configs/output.ts rename to packages/rzpack/src/webpack/configs/output.ts index 0f798a1..8c2bf92 100644 --- a/packages/rzpack/src/configs/output.ts +++ b/packages/rzpack/src/webpack/configs/output.ts @@ -1,14 +1,13 @@ import { pathResolve } from 'rzpack-utils' import type { Configuration } from 'webpack' import type WebpackChain from 'webpack-chain' -import { rzpack } from '../cli' export type Output = Configuration['output'] | string const defaultFileName = 'assets/js/[name].[contenthash].js' const defaultChunkFilename = 'assets/js/[name].[contenthash].js' -const resolveOutput = (webpackChain: WebpackChain, output: Output = 'dist') => { +export default (webpackChain: WebpackChain, output: Output = 'dist') => { let configs: Output = { filename: defaultFileName, chunkFilename: defaultChunkFilename, @@ -32,12 +31,10 @@ const resolveOutput = (webpackChain: WebpackChain, output: Output = 'dist') => { // 配置打包输出文件 webpackOutput // 构建前先清空目录 - .set('clean', rzpack.mode === 'production') + .set('clean', process.env.NODE_ENV === 'production') .end() // 将runtime代码抽离成一个 .optimization.runtimeChunk(true) // vendors包的hash值保持不变,用于优化公共库的长期缓存 .set('chunkIds', 'deterministic') } - -export default resolveOutput diff --git a/packages/rzpack/src/webpack/index.ts b/packages/rzpack/src/webpack/index.ts new file mode 100644 index 0000000..c9031f8 --- /dev/null +++ b/packages/rzpack/src/webpack/index.ts @@ -0,0 +1,20 @@ +import type WebpackChain from 'webpack-chain' +import type { RzpackConfigs } from '..' +import { DEFAULT_CONFIG } from '../constant' +import loadAssetsConfigs from './assets' +import loadBaseConfigs from './configs' +import loadPluginsConfigs from './plugins' + +export const loadWebpackConfigs = async ( + webpackChain: WebpackChain, + configs: RzpackConfigs, +) => { + const { publicPath = DEFAULT_CONFIG.STATIC_DIR } = configs + loadBaseConfigs(webpackChain, configs) + loadAssetsConfigs(webpackChain, { ...configs, publicPath }) + await loadPluginsConfigs(webpackChain, { ...configs, publicPath }) +} + +export { default as runWebpackBuild } from './build' +export { default as runWebpackPreview } from './preview' +export { default as runWebpackServer } from './server' diff --git a/packages/rzpack/src/plugins/compression-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/compression-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/compression-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/compression-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/copy-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/copy-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/copy-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/copy-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/define-plugin.ts b/packages/rzpack/src/webpack/plugins/define-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/define-plugin.ts rename to packages/rzpack/src/webpack/plugins/define-plugin.ts diff --git a/packages/rzpack/src/plugins/dll-plugin.ts b/packages/rzpack/src/webpack/plugins/dll-plugin.ts similarity index 93% rename from packages/rzpack/src/plugins/dll-plugin.ts rename to packages/rzpack/src/webpack/plugins/dll-plugin.ts index 0dcf295..4c09d99 100644 --- a/packages/rzpack/src/plugins/dll-plugin.ts +++ b/packages/rzpack/src/webpack/plugins/dll-plugin.ts @@ -1,4 +1,4 @@ -import fs from 'fs' +import fs from 'node:fs' import { checkDllVersion, dllManifestPath, @@ -78,7 +78,7 @@ export const buildDll = (dll: Array) => { * 加载DllReferencePlugin * @param webpackChain WebpackChain */ -const resolveDllReferencePlugin = (webpackChain: WebpackChain) => { +export default (webpackChain: WebpackChain) => { webpackChain.plugin('dll-reference-plugin').use(webpack.DllReferencePlugin, [ { context: __dirname, @@ -86,5 +86,3 @@ const resolveDllReferencePlugin = (webpackChain: WebpackChain) => { }, ]) } - -export default resolveDllReferencePlugin diff --git a/packages/rzpack/src/plugins/eslint-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/eslint-webpack-plugin.ts similarity index 96% rename from packages/rzpack/src/plugins/eslint-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/eslint-webpack-plugin.ts index ad0223e..31f935b 100644 --- a/packages/rzpack/src/plugins/eslint-webpack-plugin.ts +++ b/packages/rzpack/src/webpack/plugins/eslint-webpack-plugin.ts @@ -1,4 +1,4 @@ -import path from 'path' +import path from 'node:path' import EslintWebpackPlugin from 'eslint-webpack-plugin' import { getFileFullPath, requireResolve } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' diff --git a/packages/rzpack/src/plugins/fork-ts-checker-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/fork-ts-checker-webpack-plugin.ts similarity index 96% rename from packages/rzpack/src/plugins/fork-ts-checker-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/fork-ts-checker-webpack-plugin.ts index eaf69b7..61b4f94 100644 --- a/packages/rzpack/src/plugins/fork-ts-checker-webpack-plugin.ts +++ b/packages/rzpack/src/webpack/plugins/fork-ts-checker-webpack-plugin.ts @@ -1,4 +1,4 @@ -import path from 'path' +import path from 'node:path' import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin' import { requireResolve } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' diff --git a/packages/rzpack/src/plugins/friendly-errors-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/friendly-errors-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/friendly-errors-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/friendly-errors-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/html-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/html-webpack-plugin.ts similarity index 93% rename from packages/rzpack/src/plugins/html-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/html-webpack-plugin.ts index 34a4124..187d2b9 100644 --- a/packages/rzpack/src/plugins/html-webpack-plugin.ts +++ b/packages/rzpack/src/webpack/plugins/html-webpack-plugin.ts @@ -1,7 +1,7 @@ import HtmlWebpackPlugin from 'html-webpack-plugin' import { fileExists, getFileFullPath } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import { DEFAULT_CONFIG } from '../constant' +import { DEFAULT_CONFIG } from '../../constant' export default ( webpackChain: WebpackChain, diff --git a/packages/rzpack/src/plugins/index.ts b/packages/rzpack/src/webpack/plugins/index.ts similarity index 93% rename from packages/rzpack/src/plugins/index.ts rename to packages/rzpack/src/webpack/plugins/index.ts index c8ef25d..9651568 100644 --- a/packages/rzpack/src/plugins/index.ts +++ b/packages/rzpack/src/webpack/plugins/index.ts @@ -1,7 +1,7 @@ import { fileExists, getFileFullPath } from 'rzpack-utils' import type WebpackChain from 'webpack-chain' -import type { RzpackConfigs } from '..' -import { rzpack } from './../cli' +import type { RzpackConfigs } from '../..' +import { rzpack } from '../../cli' import compressionWebpackPlugin from './compression-webpack-plugin' import copyWebpackPlugin from './copy-webpack-plugin' import definePlugin from './define-plugin' @@ -61,7 +61,7 @@ export default async (webpackChain: WebpackChain, options: RzpackConfigs) => { ) } - if (rzpack.mode === 'production') { + if (process.env.NODE_ENV === 'production') { copyWebpackPlugin(webpackChain, options.publicPath) miniCssExtractPlugin(webpackChain) if (rzpack.bundleSize) { @@ -73,7 +73,7 @@ export default async (webpackChain: WebpackChain, options: RzpackConfigs) => { } const refresh = options?.reactRefresh ?? true - if (rzpack.mode === 'development' && refresh) { + if (process.env.NODE_ENV === 'development' && refresh) { reactRefreshWebpackPlugin(webpackChain) } } diff --git a/packages/rzpack/src/plugins/million-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/million-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/million-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/million-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/mini-css-extract-plugin.ts b/packages/rzpack/src/webpack/plugins/mini-css-extract-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/mini-css-extract-plugin.ts rename to packages/rzpack/src/webpack/plugins/mini-css-extract-plugin.ts diff --git a/packages/rzpack/src/plugins/module-federation-plugin.ts b/packages/rzpack/src/webpack/plugins/module-federation-plugin.ts similarity index 97% rename from packages/rzpack/src/plugins/module-federation-plugin.ts rename to packages/rzpack/src/webpack/plugins/module-federation-plugin.ts index dfae70f..dd8fb85 100644 --- a/packages/rzpack/src/plugins/module-federation-plugin.ts +++ b/packages/rzpack/src/webpack/plugins/module-federation-plugin.ts @@ -78,9 +78,9 @@ export default ( ...restOptions } = options ?? {} const providerDefaultOption = exposes ? { runtime: false } : {} - let sharedConfigs + let sharedConfigs: Record if (shared) { - if (shared instanceof Array) { + if (Array.isArray(shared)) { sharedConfigs = shared.reduce((prev, item) => { return { ...prev, diff --git a/packages/rzpack/src/plugins/react-refresh-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/react-refresh-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/react-refresh-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/react-refresh-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/speed-measure-webpack-plugin.ts b/packages/rzpack/src/webpack/plugins/speed-measure-webpack-plugin.ts similarity index 100% rename from packages/rzpack/src/plugins/speed-measure-webpack-plugin.ts rename to packages/rzpack/src/webpack/plugins/speed-measure-webpack-plugin.ts diff --git a/packages/rzpack/src/plugins/unplugin-build-info.ts b/packages/rzpack/src/webpack/plugins/unplugin-build-info.ts similarity index 100% rename from packages/rzpack/src/plugins/unplugin-build-info.ts rename to packages/rzpack/src/webpack/plugins/unplugin-build-info.ts diff --git a/packages/rzpack/src/plugins/webpack-bundle-analyzer.ts b/packages/rzpack/src/webpack/plugins/webpack-bundle-analyzer.ts similarity index 100% rename from packages/rzpack/src/plugins/webpack-bundle-analyzer.ts rename to packages/rzpack/src/webpack/plugins/webpack-bundle-analyzer.ts diff --git a/packages/rzpack/src/plugins/webpackbar.ts b/packages/rzpack/src/webpack/plugins/webpackbar.ts similarity index 85% rename from packages/rzpack/src/plugins/webpackbar.ts rename to packages/rzpack/src/webpack/plugins/webpackbar.ts index bc83605..f8f5afb 100644 --- a/packages/rzpack/src/plugins/webpackbar.ts +++ b/packages/rzpack/src/webpack/plugins/webpackbar.ts @@ -1,6 +1,6 @@ import type WebpackChain from 'webpack-chain' import Webpackbar from 'webpackbar' -import { NAME, VERSION } from '../constant' +import { NAME, VERSION } from '../../constant' export default (webpackChain: WebpackChain) => { webpackChain diff --git a/packages/rzpack/src/preview.ts b/packages/rzpack/src/webpack/preview.ts similarity index 88% rename from packages/rzpack/src/preview.ts rename to packages/rzpack/src/webpack/preview.ts index 4afc6a9..d9fa0d8 100644 --- a/packages/rzpack/src/preview.ts +++ b/packages/rzpack/src/webpack/preview.ts @@ -4,7 +4,7 @@ import { getNetwork } from 'rzpack-utils' import { cyan } from 'rzpack-utils' import sirv from 'sirv' -const runPreview = async (outDir: string) => { +export default async (outDir: string) => { const assets = sirv(outDir, { gzip: true, }) @@ -23,5 +23,3 @@ const runPreview = async (outDir: string) => { ) }) } - -export default runPreview diff --git a/packages/rzpack/src/server.ts b/packages/rzpack/src/webpack/server.ts similarity index 93% rename from packages/rzpack/src/server.ts rename to packages/rzpack/src/webpack/server.ts index bca301c..8002b79 100644 --- a/packages/rzpack/src/server.ts +++ b/packages/rzpack/src/webpack/server.ts @@ -6,10 +6,10 @@ import runUI, { } from 'rzpack-ui' import { cyan, logError, logWarning } from 'rzpack-utils' import Webpack from 'webpack' -import WebpackDevServer, { Configuration } from 'webpack-dev-server' -import { rzpack } from './cli' +import WebpackDevServer, { type Configuration } from 'webpack-dev-server' +import { rzpack } from '../cli' -const runServer = async (startUI: boolean, proxyFile: string) => { +export default async (startUI: boolean, proxyFile: string) => { rzpack.webpackChain.devtool('cheap-module-source-map') const { network, local, port, ...webpackConfigs } = rzpack.toConfig() @@ -102,5 +102,3 @@ const runServer = async (startUI: boolean, proxyFile: string) => { ) server.start() } - -export default runServer From 6dc1e63820346f8ed6a1664b1c15bbdef22430d9 Mon Sep 17 00:00:00 2001 From: renzp94 <1074720760@qq.com> Date: Wed, 29 May 2024 14:53:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(ui):=20=E4=BF=AE=E5=A4=8Dtitle=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E4=B8=BAundefined=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=97=B6?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/server/src/routes/system.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/server/src/routes/system.ts b/packages/ui/server/src/routes/system.ts index 02f403b..11e3277 100644 --- a/packages/ui/server/src/routes/system.ts +++ b/packages/ui/server/src/routes/system.ts @@ -10,7 +10,7 @@ router.get( catchError((_, res) => { let title = process.env.APP_TITLE const yagt = process.env.YAGT_URL - if (!title) { + if (title === 'undefined' || !title) { const pkgs = requireFile(getFileFullPath('./package.json')) title = pkgs?.name } From 17be846b9f000d1e9d35f174df7b3db4c300dd5a Mon Sep 17 00:00:00 2001 From: renzp94 <1074720760@qq.com> Date: Wed, 29 May 2024 14:53:27 +0800 Subject: [PATCH 3/3] chore(ui): publish V0.0.13 --- packages/ui/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/package.json b/packages/ui/package.json index 49900c0..eac2943 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -1,6 +1,6 @@ { "name": "rzpack-ui", - "version": "0.0.12", + "version": "0.0.13", "description": "", "main": "./dist/index.js", "types": "./dist/index.d.ts",