Skip to content

Commit

Permalink
feat: support server config
Browse files Browse the repository at this point in the history
  • Loading branch information
HomyeeKing committed Jan 21, 2025
1 parent 198cd8c commit 504a96c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
35 changes: 22 additions & 13 deletions packages/ice/src/service/serverCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
23 changes: 13 additions & 10 deletions packages/ice/src/service/webpackServerCompiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -27,7 +27,7 @@ type CSSRuleConfig = [string, string?, Record<string, any>?];

export class WebpackServerCompiler {
private config: webpack.Configuration;
private options: Record<string, any>;
private options: { userServerConfig: UserConfig['server']; [key: string]: any };

constructor(options: any) {
this.options = options;
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -158,7 +161,7 @@ export class WebpackServerCompiler {
extractComments: false,
}),
],
...webpackConfig.optimization,
...(webpackConfig.optimization as any),
},
resolve: {
alias: options.alias,
Expand All @@ -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;
Expand All @@ -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: {
Expand Down
17 changes: 17 additions & 0 deletions packages/ice/src/types/userConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<WebpackConfiguration, 'plugins' | 'optimization'> & {
/**
* 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.
Expand Down

0 comments on commit 504a96c

Please sign in to comment.