Skip to content

Commit

Permalink
fix: exclude rule of compilation and load compile target (#5906)
Browse files Browse the repository at this point in the history
* fix: compilation options

* fix: regexp

* chore: changelog

* fix: optimize code
  • Loading branch information
ClarkXia authored Feb 15, 2023
1 parent f94af22 commit 7705f2f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/ice/src/service/serverCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions packages/webpack-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-config/src/compileExcludes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 4 additions & 3 deletions packages/webpack-config/src/getCompilerPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -206,6 +206,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio
module: true,
}, minimizerOptions);
const compilation = compilationPlugin({
rootDir,
cacheDir,
sourceMap,
fastRefresh,
Expand Down
31 changes: 30 additions & 1 deletion packages/webpack-config/src/unPlugins/compilation.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,6 +13,7 @@ const { merge } = lodash;
type JSXSuffix = 'jsx' | 'tsx';

interface Options {
rootDir: string;
mode: 'development' | 'production' | 'none';
fastRefresh: boolean;
compileIncludes?: (string | RegExp)[];
Expand All @@ -26,6 +29,7 @@ const formatId = (id: string) => id.split(path.sep).join('/');

const compilationPlugin = (options: Options): UnpluginOptions => {
const {
rootDir,
sourceMap,
mode,
fastRefresh,
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -170,6 +174,8 @@ const compilationPlugin = (options: Options): UnpluginOptions => {
};

interface GetJsxTransformOptions {
rootDir: string;
mode: Options['mode'];
suffix: JSXSuffix;
fastRefresh: boolean;
polyfill: Config['polyfill'];
Expand All @@ -181,6 +187,8 @@ function getJsxTransformOptions({
fastRefresh,
polyfill,
enableEnv,
mode,
rootDir,
}: GetJsxTransformOptions) {
const reactTransformConfig: ReactConfig = {
refresh: fastRefresh,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7705f2f

Please sign in to comment.