From 7705f2fd8984cabcfab0163b1137c71894cb9fcd Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 15 Feb 2023 19:09:41 +0800 Subject: [PATCH] fix: exclude rule of compilation and load compile target (#5906) * fix: compilation options * fix: regexp * chore: changelog * fix: optimize code --- packages/ice/src/service/serverCompiler.ts | 2 +- packages/webpack-config/CHANGELOG.md | 1 + packages/webpack-config/package.json | 1 + .../webpack-config/src/compileExcludes.ts | 4 +-- .../webpack-config/src/getCompilerPlugins.ts | 7 +++-- packages/webpack-config/src/index.ts | 3 +- .../src/unPlugins/compilation.ts | 31 ++++++++++++++++++- pnpm-lock.yaml | 2 ++ 8 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/ice/src/service/serverCompiler.ts b/packages/ice/src/service/serverCompiler.ts index 15e869dba5..226e8f33ae 100644 --- a/packages/ice/src/service/serverCompiler.ts +++ b/packages/ice/src/service/serverCompiler.ts @@ -97,7 +97,7 @@ export function createServerCompiler(options: Options) { }; } const enableSyntaxFeatures = syntaxFeatures && Object.keys(syntaxFeatures).some(key => syntaxFeatures[key]); - const transformPlugins = getCompilerPlugins({ + const transformPlugins = getCompilerPlugins(rootDir, { ...task.config, fastRefresh: false, enableEnv, diff --git a/packages/webpack-config/CHANGELOG.md b/packages/webpack-config/CHANGELOG.md index e2ec0b6a19..8e25b0d1bf 100644 --- a/packages/webpack-config/CHANGELOG.md +++ b/packages/webpack-config/CHANGELOG.md @@ -4,6 +4,7 @@ - [fix] support transform options for API `getCompilerPlugins` - [fix] optimize webpack config (enable unsafeCache) +- [fix] exclude rule of compilation and load compile target ## 1.0.6 diff --git a/packages/webpack-config/package.json b/packages/webpack-config/package.json index 378fec23ce..ed861551f9 100644 --- a/packages/webpack-config/package.json +++ b/packages/webpack-config/package.json @@ -17,6 +17,7 @@ "dependencies": { "@ice/bundles": "0.1.4", "@rollup/pluginutils": "^4.2.0", + "browserslist": "^4.19.3", "consola": "^2.15.3", "fast-glob": "^3.2.11", "process": "^0.11.10" diff --git a/packages/webpack-config/src/compileExcludes.ts b/packages/webpack-config/src/compileExcludes.ts index 2e20d3e0f7..f770b8a085 100644 --- a/packages/webpack-config/src/compileExcludes.ts +++ b/packages/webpack-config/src/compileExcludes.ts @@ -10,8 +10,8 @@ const SKIP_COMPILE = [ ]; const compileExcludes = [ - new RegExp(SKIP_COMPILE.map((dep) => `node_modules/?.+${dep}/`).join('|')), - /bundles\/compiled/, + new RegExp(SKIP_COMPILE.map((dep) => `node_modules/[\\w-@\\.]*${dep}/`).join('|')), + /@ice\/bundles\/compiled/, ]; export default compileExcludes; diff --git a/packages/webpack-config/src/getCompilerPlugins.ts b/packages/webpack-config/src/getCompilerPlugins.ts index 7f82e690d4..6be6d85562 100644 --- a/packages/webpack-config/src/getCompilerPlugins.ts +++ b/packages/webpack-config/src/getCompilerPlugins.ts @@ -32,9 +32,9 @@ function transformInclude(id: string) { return !!id.match(/\.(js|jsx|ts|tsx|mjs|mts|css|less|scss)$/); } -function getCompilerPlugins(config: Config, compiler: 'webpack', transformOptions: TransformOptions): Configuration['plugins']; -function getCompilerPlugins(config: Config, compiler: 'esbuild', transformOptions: TransformOptions): BuildOptions['plugins']; -function getCompilerPlugins(config: Config, compiler: Compiler, transformOptions: TransformOptions) { +function getCompilerPlugins(rootDir: string, config: Config, compiler: 'webpack', transformOptions: TransformOptions): Configuration['plugins']; +function getCompilerPlugins(rootDir: string, config: Config, compiler: 'esbuild', transformOptions: TransformOptions): BuildOptions['plugins']; +function getCompilerPlugins(rootDir: string, config: Config, compiler: Compiler, transformOptions: TransformOptions) { const { sourceMap, transformPlugins = [], @@ -60,6 +60,7 @@ function getCompilerPlugins(config: Config, compiler: Compiler, transformOptions // Reason: https://github.com/unjs/unplugin/issues/154 if (swcOptions && compiler !== 'webpack') { compilerPlugins.push(compilationPlugin({ + rootDir, cacheDir, sourceMap, fastRefresh, diff --git a/packages/webpack-config/src/index.ts b/packages/webpack-config/src/index.ts index 03eaca3a38..39c245601f 100644 --- a/packages/webpack-config/src/index.ts +++ b/packages/webpack-config/src/index.ts @@ -182,7 +182,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio }, } : {}; // get compile plugins - const compilerWebpackPlugins = getCompilerPlugins(config, 'webpack', { isServer: false }); + const compilerWebpackPlugins = getCompilerPlugins(rootDir, config, 'webpack', { isServer: false }); const terserOptions: any = merge({ compress: { @@ -206,6 +206,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio module: true, }, minimizerOptions); const compilation = compilationPlugin({ + rootDir, cacheDir, sourceMap, fastRefresh, diff --git a/packages/webpack-config/src/unPlugins/compilation.ts b/packages/webpack-config/src/unPlugins/compilation.ts index 6af965461e..dcfc0cdbb9 100644 --- a/packages/webpack-config/src/unPlugins/compilation.ts +++ b/packages/webpack-config/src/unPlugins/compilation.ts @@ -1,5 +1,7 @@ import path from 'path'; import { swc, swcPluginRemoveExport, swcPluginKeepExport, coreJsPath } from '@ice/bundles'; +import browserslist from 'browserslist'; +import consola from 'consola'; import type { SwcConfig, ReactConfig } from '@ice/bundles'; import type { UnpluginOptions } from '@ice/bundles/compiled/unplugin/index.js'; import lodash from '@ice/bundles/compiled/lodash/index.js'; @@ -11,6 +13,7 @@ const { merge } = lodash; type JSXSuffix = 'jsx' | 'tsx'; interface Options { + rootDir: string; mode: 'development' | 'production' | 'none'; fastRefresh: boolean; compileIncludes?: (string | RegExp)[]; @@ -26,6 +29,7 @@ const formatId = (id: string) => id.split(path.sep).join('/'); const compilationPlugin = (options: Options): UnpluginOptions => { const { + rootDir, sourceMap, mode, fastRefresh, @@ -78,7 +82,7 @@ const compilationPlugin = (options: Options): UnpluginOptions => { sourceMaps: !!sourceMap, }; - const commonOptions = getJsxTransformOptions({ suffix, fastRefresh, polyfill, enableEnv }); + const commonOptions = getJsxTransformOptions({ rootDir, mode, suffix, fastRefresh, polyfill, enableEnv }); // auto detect development mode if ( @@ -170,6 +174,8 @@ const compilationPlugin = (options: Options): UnpluginOptions => { }; interface GetJsxTransformOptions { + rootDir: string; + mode: Options['mode']; suffix: JSXSuffix; fastRefresh: boolean; polyfill: Config['polyfill']; @@ -181,6 +187,8 @@ function getJsxTransformOptions({ fastRefresh, polyfill, enableEnv, + mode, + rootDir, }: GetJsxTransformOptions) { const reactTransformConfig: ReactConfig = { refresh: fastRefresh, @@ -211,6 +219,11 @@ function getJsxTransformOptions({ coreJs: '3.26', } : {}), }; + const supportBrowsers = getSupportedBrowsers(rootDir, mode === 'development'); + if (supportBrowsers) { + // Fix issue of https://github.com/swc-project/swc/issues/3365 + commonOptions.env.targets = supportBrowsers; + } } else { // Set target `es2022` for default transform when env is false. commonOptions.jsc.target = 'es2022'; @@ -249,4 +262,20 @@ function getJsxTransformOptions({ return commonOptions; } +function getSupportedBrowsers( + dir: string, + isDevelopment: boolean, +): string[] | undefined { + let browsers: any; + try { + browsers = browserslist.loadConfig({ + path: dir, + env: isDevelopment ? 'development' : 'production', + }); + } catch { + consola.debug('[browsers]', 'fail to load config of browsers'); + } + return browsers; +} + export default compilationPlugin; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9dcdfdb6e1..69320dc285 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1429,6 +1429,7 @@ importers: specifiers: '@ice/bundles': 0.1.4 '@rollup/pluginutils': ^4.2.0 + browserslist: ^4.19.3 consola: ^2.15.3 esbuild: ^0.16.5 fast-glob: ^3.2.11 @@ -1439,6 +1440,7 @@ importers: dependencies: '@ice/bundles': link:../bundles '@rollup/pluginutils': 4.2.1 + browserslist: 4.21.5 consola: 2.15.3 fast-glob: 3.2.12 process: 0.11.10