From d4aaa5806be10663817b9e08015efdb8c6526e54 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 1 Nov 2023 10:08:29 +0800 Subject: [PATCH] fix: support transform plugin in speedup mode (#6615) * fix: support tranform plugin in speedup mode * chore: lint * chore: remove log --- .changeset/silent-monkeys-check.md | 8 ++ packages/bundles/package.json | 2 +- packages/bundles/scripts/tasks.ts | 16 ++++ packages/ice/package.json | 4 +- .../src/bundler/config/defaultServerConfig.ts | 6 +- packages/ice/src/bundler/config/getUrls.ts | 4 +- packages/rspack-config/src/index.ts | 8 +- .../shared-config/src/getCompilerPlugins.ts | 22 +++--- pnpm-lock.yaml | 74 +++++++++---------- 9 files changed, 88 insertions(+), 56 deletions(-) create mode 100644 .changeset/silent-monkeys-check.md diff --git a/.changeset/silent-monkeys-check.md b/.changeset/silent-monkeys-check.md new file mode 100644 index 0000000000..58831bbc46 --- /dev/null +++ b/.changeset/silent-monkeys-check.md @@ -0,0 +1,8 @@ +--- +'@ice/rspack-config': patch +'@ice/shared-config': patch +'@ice/bundles': patch +'@ice/app': patch +--- + +fix: support custom transform plugins in speedup mode diff --git a/packages/bundles/package.json b/packages/bundles/package.json index c9ff10495e..7588c6d67f 100644 --- a/packages/bundles/package.json +++ b/packages/bundles/package.json @@ -71,7 +71,7 @@ "webpack": "5.88.2", "webpack-bundle-analyzer": "4.5.0", "webpack-dev-server": "4.15.0", - "unplugin": "0.9.5", + "unplugin": "1.5.0", "bonjour-service": "^1.0.13", "colorette": "^2.0.10", "compression": "^1.7.4", diff --git a/packages/bundles/scripts/tasks.ts b/packages/bundles/scripts/tasks.ts index 48dee28bf7..d7ca951587 100644 --- a/packages/bundles/scripts/tasks.ts +++ b/packages/bundles/scripts/tasks.ts @@ -86,6 +86,22 @@ const tasks = [ file: 'node_modules/unplugin/dist/webpack/loaders/load.js', bundleName: 'webpack/loaders/load.js', }, + { + pkgName: 'unplugin', + declaration: false, + emptyDir: false, + externals: taskExternals, + file: 'node_modules/unplugin/dist/rspack/loaders/transform.js', + bundleName: 'rspack/loaders/transform.js', + }, + { + pkgName: 'unplugin', + declaration: false, + emptyDir: false, + externals: taskExternals, + file: 'node_modules/unplugin/dist/rspack/loaders/load.js', + bundleName: 'rspack/loaders/load.js', + }, { // pack main package pkgName: 'fork-ts-checker-webpack-plugin', diff --git a/packages/ice/package.json b/packages/ice/package.json index c2218462e8..982557476d 100644 --- a/packages/ice/package.json +++ b/packages/ice/package.json @@ -98,7 +98,7 @@ "react": "^18.2.0", "react-router": "6.14.2", "sass": "^1.50.0", - "unplugin": "^0.9.0", + "unplugin": "^1.5.0", "webpack": "^5.88.0", "webpack-dev-server": "^4.7.4", "@rspack/core": "0.3.0", @@ -111,4 +111,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/packages/ice/src/bundler/config/defaultServerConfig.ts b/packages/ice/src/bundler/config/defaultServerConfig.ts index 5daa47d873..18f4f27b8d 100644 --- a/packages/ice/src/bundler/config/defaultServerConfig.ts +++ b/packages/ice/src/bundler/config/defaultServerConfig.ts @@ -1,9 +1,13 @@ import detectPort from 'detect-port'; import type { CommandArgs } from 'build-scripts'; import type { Configuration } from 'webpack-dev-server'; +import type { Configuration as DevServerConfiguration } from '@rspack/dev-server'; import { DEFAULT_HOST, DEFAULT_PORT } from '../../constant.js'; -async function getDefaultServerConfig(devServerConfig: Configuration, commandArgs: CommandArgs) { +async function getDefaultServerConfig( + devServerConfig: Configuration | DevServerConfiguration, + commandArgs: CommandArgs, +) { // Get the value of the host and port from the command line, environment variables, and webpack config. // Value priority: process.env.PORT > commandArgs > webpackConfig > DEFAULT. const host = process.env.HOST || diff --git a/packages/ice/src/bundler/config/getUrls.ts b/packages/ice/src/bundler/config/getUrls.ts index 69d4bae422..f4af2fcfb6 100644 --- a/packages/ice/src/bundler/config/getUrls.ts +++ b/packages/ice/src/bundler/config/getUrls.ts @@ -2,13 +2,15 @@ import type { TaskConfig } from 'build-scripts'; import type { Config } from '@ice/shared-config/types'; import type { AppConfig } from '@ice/runtime/types'; import type { Configuration as DevServerConfiguration } from 'webpack-dev-server'; +import type { Configuration as RSPackDevServerConfiguration } from '@rspack/dev-server'; + import prepareURLs from '../../utils/prepareURLs.js'; import getRouterBasename from '../../utils/getRouterBasename.js'; interface Options { taskConfig: TaskConfig; appConfig: AppConfig; - devServerConfig: DevServerConfiguration; + devServerConfig: DevServerConfiguration | RSPackDevServerConfiguration; } const getUrls = ({ diff --git a/packages/rspack-config/src/index.ts b/packages/rspack-config/src/index.ts index 4ca44be9e7..eb1529f466 100644 --- a/packages/rspack-config/src/index.ts +++ b/packages/rspack-config/src/index.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import { createRequire } from 'module'; -import { compilationPlugin, compileExcludes, getDefineVars } from '@ice/shared-config'; +import { compilationPlugin, compileExcludes, getDefineVars, getCompilerPlugins } from '@ice/shared-config'; import type { Config, ModifyWebpackConfig } from '@ice/shared-config/types'; import type { Configuration } from '@rspack/core'; import type { rspack as Rspack } from '@ice/bundles/esm/rspack.js'; @@ -76,6 +76,8 @@ const getConfig: GetConfig = (options) => { getRoutesFile, }); const cssFilename = `css/${hashKey ? `[name]-[${hashKey}].css` : '[name].css'}`; + // get compile plugins + const compilerWebpackPlugins = getCompilerPlugins(rootDir, taskConfig || {}, 'rspack', { isServer: false }); const config: Configuration = { entry: { main: [path.join(rootDir, runtimeTmpDir, 'entry.client.tsx')], @@ -127,11 +129,13 @@ const getConfig: GetConfig = (options) => { }, // @ts-expect-error plugin instance defined by default in not compatible with rspack. plugins: [ + ...plugins, + // Unplugin should be compatible with rspack. + ...compilerWebpackPlugins, new AssetManifest({ fileName: 'assets-manifest.json', outputDir: path.join(rootDir, runtimeTmpDir), }), - ...plugins, ].filter(Boolean), builtins: { define: getDefineVars(define, runtimeDefineVars, getExpandedEnvs), diff --git a/packages/shared-config/src/getCompilerPlugins.ts b/packages/shared-config/src/getCompilerPlugins.ts index 0c4a4ac3b9..ff70ad1179 100644 --- a/packages/shared-config/src/getCompilerPlugins.ts +++ b/packages/shared-config/src/getCompilerPlugins.ts @@ -6,7 +6,7 @@ import compilationPlugin from './unPlugins/compilation.js'; import redirectImportPlugin from './unPlugins/redirectImport.js'; import compileExcludes from './compileExcludes.js'; -type Compiler = 'webpack' | 'esbuild'; +type Compiler = 'webpack' | 'esbuild' | 'rspack'; interface TransformOptions { isServer: boolean; } @@ -31,6 +31,7 @@ function transformInclude(id: string) { return !!id.match(/\.(js|jsx|ts|tsx|mjs|mts|css|less|scss)$/); } +function getCompilerPlugins(rootDir: string, config: Config, compiler: 'rspack', transformOptions: TransformOptions): Config['plugins']; function getCompilerPlugins(rootDir: string, config: Config, compiler: 'webpack', transformOptions: TransformOptions): Config['plugins']; function getCompilerPlugins(rootDir: string, config: Config, compiler: 'esbuild', transformOptions: TransformOptions): BuildOptions['plugins']; function getCompilerPlugins(rootDir: string, config: Config, compiler: Compiler, transformOptions: TransformOptions) { @@ -55,10 +56,10 @@ function getCompilerPlugins(rootDir: string, config: Config, compiler: Compiler, ...(transformPlugins.filter(({ enforce }) => !enforce || enforce === 'pre') || []), ...transforms.map((transform, index) => ({ name: `transform_${index}`, transform, transformInclude })), ); - + const clientBundlers = ['webpack', 'rspack']; // Use webpack loader instead of webpack plugin to do the compilation. // Reason: https://github.com/unjs/unplugin/issues/154 - if (swcOptions && compiler !== 'webpack') { + if (swcOptions && !clientBundlers.includes(compiler)) { compilerPlugins.push(compilationPlugin({ rootDir, cacheDir, @@ -85,13 +86,14 @@ function getCompilerPlugins(rootDir: string, config: Config, compiler: Compiler, exportData: redirectImports, })); } - - return compiler === 'webpack' - // Plugins will be transformed as webpack loader, the execute order of webpack loader is reversed. - ? compilerPlugins - .reverse() - .map((plugin) => createUnplugin(() => getPluginTransform(plugin, transformOptions)).webpack()) as Config['plugins'] - : compilerPlugins.map(plugin => getPluginTransform(plugin, transformOptions)); + if (clientBundlers.includes(compiler)) { + return compilerPlugins + // Plugins will be transformed as webpack loader, the execute order of webpack loader is reversed. + .reverse() + .map((plugin) => createUnplugin(() => getPluginTransform(plugin, transformOptions))[compiler]()) as Config['plugins']; + } else { + return compilerPlugins.map(plugin => getPluginTransform(plugin, transformOptions)); + } } export default getCompilerPlugins; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 72cb6fb4b3..a34d268701 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1466,8 +1466,8 @@ importers: specifier: ^4.6.4 version: 4.9.5 unplugin: - specifier: 0.9.5 - version: 0.9.5(esbuild@0.17.16)(webpack@5.88.2) + specifier: 1.5.0 + version: 1.5.0 webpack: specifier: 5.88.2 version: 5.88.2(@swc/core@1.3.80)(esbuild@0.17.16) @@ -1673,8 +1673,8 @@ importers: specifier: ^1.50.0 version: 1.50.0 unplugin: - specifier: ^0.9.0 - version: 0.9.5(esbuild@0.17.16)(webpack@5.88.2) + specifier: ^1.5.0 + version: 1.5.0 webpack: specifier: ^5.88.0 version: 5.88.2(esbuild@0.17.16) @@ -9394,10 +9394,17 @@ packages: /acorn-globals@7.0.1: resolution: {integrity: sha512-umOSDSDrfHbTNPuNpC2NSnnA3LUrqpevPb4T9jRx4MagXNS0rs+gwiTcAvqCRmsD6utzsrzNt+ebm00SNWiC3Q==} dependencies: - acorn: 8.8.2 + acorn: 8.11.2 acorn-walk: 8.2.0 dev: true + /acorn-import-assertions@1.9.0(acorn@8.11.2): + resolution: {integrity: sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==} + peerDependencies: + acorn: ^8 + dependencies: + acorn: 8.11.2 + /acorn-import-assertions@1.9.0(acorn@8.8.2): resolution: {integrity: sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==} peerDependencies: @@ -9405,12 +9412,12 @@ packages: dependencies: acorn: 8.8.2 - /acorn-jsx@5.3.2(acorn@8.8.2): + /acorn-jsx@5.3.2(acorn@8.11.2): resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} peerDependencies: acorn: ^6.0.0 || ^7.0.0 || ^8.0.0 dependencies: - acorn: 8.8.2 + acorn: 8.11.2 /acorn-node@1.8.2: resolution: {integrity: sha512-8mt+fslDufLYntIoPAaIMUe/lrbrehIiwmR3t2k9LljIzoigEPF27eLk2hy8zSGzmR/ogr7zbRKINMo1u0yh5A==} @@ -9435,6 +9442,11 @@ packages: hasBin: true dev: true + /acorn@8.11.2: + resolution: {integrity: sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==} + engines: {node: '>=0.4.0'} + hasBin: true + /acorn@8.8.2: resolution: {integrity: sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==} engines: {node: '>=0.4.0'} @@ -13165,8 +13177,8 @@ packages: resolution: {integrity: sha512-XwctdmTO6SIvCzd9810yyNzIrOrqNYV9Koizx4C/mRhf9uq0o4yHoCEU/670pOxOL/MSraektvSAji79kX90Vg==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dependencies: - acorn: 8.8.2 - acorn-jsx: 5.3.2(acorn@8.8.2) + acorn: 8.11.2 + acorn-jsx: 5.3.2(acorn@8.11.2) eslint-visitor-keys: 3.3.0 /esprima@4.0.1: @@ -21429,7 +21441,7 @@ packages: /strip-literal@0.4.2: resolution: {integrity: sha512-pv48ybn4iE1O9RLgCAN0iU4Xv7RlBTiit6DKmMiErbs9x1wH6vXBs45tWc0H5wUIF6TLTrKweqkmYF/iraQKNw==} dependencies: - acorn: 8.8.2 + acorn: 8.11.2 dev: true /style-equal@1.0.0: @@ -21958,7 +21970,7 @@ packages: hasBin: true dependencies: '@jridgewell/source-map': 0.3.2 - acorn: 8.8.2 + acorn: 8.11.2 commander: 2.20.3 source-map-support: 0.5.21 @@ -22189,7 +22201,7 @@ packages: '@tsconfig/node14': 1.0.3 '@tsconfig/node16': 1.0.3 '@types/node': 17.0.45 - acorn: 8.8.2 + acorn: 8.11.2 acorn-walk: 8.2.0 arg: 4.1.3 create-require: 1.1.1 @@ -22597,29 +22609,13 @@ packages: resolution: {integrity: sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==} engines: {node: '>= 0.8'} - /unplugin@0.9.5(esbuild@0.17.16)(webpack@5.88.2): - resolution: {integrity: sha512-luraheyfxwtvkvHpsOvMNv7IjLdORTWKZp0gWYNHGLi2ImON3iIZOj464qEyyEwLA/EMt12fC415HW9zRpOfTg==} - peerDependencies: - esbuild: '>=0.13' - rollup: ^2.50.0 - vite: ^2.3.0 || ^3.0.0-0 - webpack: 4 || 5 - peerDependenciesMeta: - esbuild: - optional: true - rollup: - optional: true - vite: - optional: true - webpack: - optional: true + /unplugin@1.5.0: + resolution: {integrity: sha512-9ZdRwbh/4gcm1JTOkp9lAkIDrtOyOxgHmY7cjuwI8L/2RTikMcVG25GsZwNAgRuap3iDw2jeq7eoqtAsz5rW3A==} dependencies: - acorn: 8.8.2 + acorn: 8.11.2 chokidar: 3.5.3 - esbuild: 0.17.16 - webpack: 5.88.2(@swc/core@1.3.80)(esbuild@0.17.16) webpack-sources: 3.2.3 - webpack-virtual-modules: 0.4.6 + webpack-virtual-modules: 0.5.0 dev: true /unquote@1.1.1: @@ -23102,7 +23098,7 @@ packages: hasBin: true dependencies: '@discoveryjs/json-ext': 0.5.7 - acorn: 8.8.2 + acorn: 8.11.2 acorn-walk: 8.2.0 chalk: 4.1.2 commander: 7.2.0 @@ -23491,8 +23487,8 @@ packages: resolution: {integrity: sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==} engines: {node: '>=10.13.0'} - /webpack-virtual-modules@0.4.6: - resolution: {integrity: sha512-5tyDlKLqPfMqjT3Q9TAqf2YqjwmnUleZwzJi1A5qXnlBCdj2AtOJ6wAWdglTIDOPgOiOrXeBeFcsQ8+aGQ6QbA==} + /webpack-virtual-modules@0.5.0: + resolution: {integrity: sha512-kyDivFZ7ZM0BVOUteVbDFhlRt7Ah/CSPwJdi8hBpkK7QLumUqdLtVfm/PX/hkcnrvr0i77fO5+TjZ94Pe+C9iw==} dev: true /webpack@5.76.0(@swc/core@1.3.80)(esbuild@0.17.16): @@ -23510,8 +23506,8 @@ packages: '@webassemblyjs/ast': 1.11.1 '@webassemblyjs/wasm-edit': 1.11.1 '@webassemblyjs/wasm-parser': 1.11.1 - acorn: 8.8.2 - acorn-import-assertions: 1.9.0(acorn@8.8.2) + acorn: 8.11.2 + acorn-import-assertions: 1.9.0(acorn@8.11.2) browserslist: 4.21.5 chrome-trace-event: 1.0.3 enhanced-resolve: 5.15.0 @@ -23549,8 +23545,8 @@ packages: '@webassemblyjs/ast': 1.11.1 '@webassemblyjs/wasm-edit': 1.11.1 '@webassemblyjs/wasm-parser': 1.11.1 - acorn: 8.8.2 - acorn-import-assertions: 1.9.0(acorn@8.8.2) + acorn: 8.11.2 + acorn-import-assertions: 1.9.0(acorn@8.11.2) browserslist: 4.21.5 chrome-trace-event: 1.0.3 enhanced-resolve: 5.15.0