diff --git a/packages/ice/src/service/serverCompiler.ts b/packages/ice/src/service/serverCompiler.ts index d37c42f8b1..9b8ef497a2 100644 --- a/packages/ice/src/service/serverCompiler.ts +++ b/packages/ice/src/service/serverCompiler.ts @@ -261,21 +261,30 @@ export function createServerCompiler(options: Options) { esbuildResult = await context.rebuild(); } else { if (Array.isArray(buildOptions.entryPoints)) { + // this build phase is aimed to generate .ice/ esbuildResult = await esbuild.build(buildOptions); } else { - const webpackServerCompiler = new WebpackServerCompiler({ - ...buildOptions, - externals, - plugins: [ - compilationInfo && new VirualAssetPlugin({ compilationInfo, rootDir }), - ...transformWebpackPlugins, - new ModifyRequirePlugin(), - ].filter(Boolean), - rootDir, - userServerConfig: server, - }); - esbuildResult = (await webpackServerCompiler.build())?.compilation; - }(buildOptions); + switch (server.bundler) { + case 'webpack': + const webpackServerCompiler = new WebpackServerCompiler({ + ...buildOptions, + externals, + plugins: [ + compilationInfo && new VirualAssetPlugin({ compilationInfo, rootDir }), + ...transformWebpackPlugins, + new ModifyRequirePlugin(), + ].filter(Boolean), + rootDir, + userServerConfig: server, + }); + esbuildResult = (await webpackServerCompiler.build())?.compilation; + break; + case 'esbuild': + default: + esbuildResult = await esbuild.build(buildOptions); + break; + } + } } logger.debug('[esbuild]', `time cost: ${new Date().getTime() - startTime}ms`); diff --git a/packages/ice/src/service/webpackServerCompiler/compiler.ts b/packages/ice/src/service/webpackServerCompiler/compiler.ts index f5e1274822..d440f66fc5 100644 --- a/packages/ice/src/service/webpackServerCompiler/compiler.ts +++ b/packages/ice/src/service/webpackServerCompiler/compiler.ts @@ -1,15 +1,15 @@ import { createRequire } from 'module'; import path from 'path'; import { fileURLToPath } from 'url'; -import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; +import { esbuild, less, sass } from '@ice/bundles'; import MiniCssExtractPlugin from '@ice/bundles/compiled/mini-css-extract-plugin/dist/index.js'; -import { sass, less } from '@ice/bundles'; import TerserPlugin from '@ice/bundles/compiled/terser-webpack-plugin/index.js'; -import webpack from 'webpack'; -import { esbuild } from '@ice/bundles'; +import { getCSSModuleLocalIdent, getPostcssOpts } from '@ice/shared-config'; import type { Config } from '@ice/shared-config/types'; +import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; import type { LoaderContext } from 'webpack'; -import { getCSSModuleLocalIdent, getPostcssOpts } from '@ice/shared-config'; +import webpack from 'webpack'; +import type { UserConfig } from '../../types/userConfig.js'; import { logger } from '../../utils/logger.js'; const require = createRequire(import.meta.url); @@ -27,7 +27,7 @@ type CSSRuleConfig = [string, string?, Record?]; export class WebpackServerCompiler { private config: webpack.Configuration; - private options: Record; + private options: { userServerConfig: UserConfig['server']; [key: string]: any }; constructor(options: any) { this.options = options; @@ -79,7 +79,10 @@ export class WebpackServerCompiler { }; } - private createWebpackConfig(options: any): webpack.Configuration { + private createWebpackConfig(options: { + userServerConfig: UserConfig['server']; + [key: string]: any; + }): webpack.Configuration { const { userServerConfig } = options; const { webpackConfig = {} } = userServerConfig; const cssRules = [ @@ -158,7 +161,7 @@ export class WebpackServerCompiler { extractComments: false, }), ], - ...webpackConfig.optimization, + ...(webpackConfig.optimization as any), }, resolve: { alias: options.alias, @@ -177,9 +180,8 @@ export class WebpackServerCompiler { // // Match `.js`, `.jsx`, `.ts` or `.tsx` files test: /\.m?[jt]sx?$/, exclude(path) { - // TODO: more universal if (path.includes('node_modules')) { - if (path.includes('@ali/alimod-ufirst-bottom-bar')) { + if (webpackConfig.transformInclude && webpackConfig.transformInclude.test(path)) { return false; } else { return true; @@ -189,6 +191,7 @@ export class WebpackServerCompiler { }, use: [ { + // TODO: compile to @ice/bundle loader: 'esbuild-loader', // available options: https://github.com/evanw/esbuild/blob/88821b7e7d46737f633120f91c65f662eace0bcf/lib/shared/types.ts#L158-L172 options: { diff --git a/packages/ice/src/types/userConfig.ts b/packages/ice/src/types/userConfig.ts index f5fb319391..7a6dcbaac4 100644 --- a/packages/ice/src/types/userConfig.ts +++ b/packages/ice/src/types/userConfig.ts @@ -2,6 +2,7 @@ import type { DefineRouteFunction, RouteItem } from '@ice/route-manifest'; import type { PluginList } from 'build-scripts'; import type { UnpluginOptions } from '@ice/bundles/compiled/unplugin/index.js'; import type { ProcessOptions } from '@ice/bundles'; +import type { Configuration as WebpackConfiguration } from '@ice/bundles/compiled/webpack'; import type { Config, ModifyWebpackConfig, MinimizerOptions } from '@ice/shared-config/types'; import type { OverwritePluginAPI } from './plugin'; @@ -242,6 +243,22 @@ export interface UserConfig { * externals config for server bundle */ externals?: string[]; + /** + * bundler for server bundle, support webpack and esbuild + * @default esbuild + */ + bundler?: 'webpack' | 'esbuild'; + /** + * webpack config, only works when bundler is webpack + */ + webpackConfig?: Pick & { + /** + * we exclude the node_modules/* by default + * + * use this if you need to transform some packages inside of node_modues + */ + transformInclude?: RegExp; + }; }; /** * Optimization options for build.