Skip to content

Commit

Permalink
chore: unify uni-builder config type (#5131)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Dec 20, 2023
1 parent d2c79a8 commit 60d7564
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 85 deletions.
10 changes: 5 additions & 5 deletions packages/builder/uni-builder/src/rspack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import type {
} from '@rsbuild/core';
import type { RsbuildProvider, StartServerResult } from '@rsbuild/shared';
import type {
UniBuilderRspackConfig,
CreateRspackBuilderOptions,
BuilderConfig,
CreateUniBuilderOptions,
CreateBuilderCommonOptions,
} from '../types';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import type { StartDevServerOptions } from '../shared/devServer';

export async function parseConfig(
uniBuilderConfig: UniBuilderRspackConfig,
uniBuilderConfig: BuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
rsbuildPlugins: RsbuildPlugin[];
}> {
const { rsbuildConfig, rsbuildPlugins } = await parseCommonConfig<'rspack'>(
const { rsbuildConfig, rsbuildPlugins } = await parseCommonConfig(
uniBuilderConfig,
options,
);
Expand Down Expand Up @@ -69,7 +69,7 @@ type UniBuilderInstance = Omit<
};

export async function createRspackBuilder(
options: CreateRspackBuilderOptions,
options: CreateUniBuilderOptions,
): Promise<UniBuilderInstance> {
const { cwd = process.cwd(), config, ...rest } = options;

Expand Down
14 changes: 4 additions & 10 deletions packages/builder/uni-builder/src/shared/parseCommonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import {
type RsbuildPlugin,
type RsbuildConfig,
} from '@rsbuild/core';
import type {
CreateBuilderCommonOptions,
UniBuilderRspackConfig,
UniBuilderWebpackConfig,
} from '../types';
import type { CreateBuilderCommonOptions, BuilderConfig } from '../types';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginGlobalVars } from './plugins/globalVars';
import { pluginRuntimeChunk } from './plugins/runtimeChunk';
Expand Down Expand Up @@ -85,10 +81,8 @@ async function getBrowserslistWithDefault(
return DEFAULT_BROWSERSLIST[target];
}

export async function parseCommonConfig<B = 'rspack' | 'webpack'>(
uniBuilderConfig: B extends 'rspack'
? UniBuilderRspackConfig
: UniBuilderWebpackConfig,
export async function parseCommonConfig(
uniBuilderConfig: BuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
Expand Down Expand Up @@ -135,7 +129,7 @@ export async function parseCommonConfig<B = 'rspack' | 'webpack'>(
for (const target of targets) {
// Incompatible with the scenario where target contains both 'web' and 'modern-web'
overrideBrowserslist[target] = await getBrowserslistWithDefault(
cwd!,
cwd,
uniBuilderConfig,
target,
);
Expand Down
64 changes: 24 additions & 40 deletions packages/builder/uni-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,13 @@ export type CreateBuilderCommonOptions = {
frameworkConfigPath?: string;
target?: RsbuildTarget | RsbuildTarget[];
/** The root path of current project. */
cwd?: string;
cwd: string;
};

export type CreateWebpackBuilderOptions = {
bundlerType: 'webpack';
config: UniBuilderWebpackConfig;
} & CreateBuilderCommonOptions;

export type CreateRspackBuilderOptions = {
bundlerType: 'rspack';
config: UniBuilderRspackConfig;
} & CreateBuilderCommonOptions;

export type CreateUniBuilderOptions =
| CreateWebpackBuilderOptions
| CreateRspackBuilderOptions;
export type CreateUniBuilderOptions = {
bundlerType: 'rspack' | 'webpack';
config: BuilderConfig;
} & Partial<CreateBuilderCommonOptions>;

export type GlobalVars = Record<string, any>;

Expand Down Expand Up @@ -106,6 +97,13 @@ export type UniBuilderExtraConfig = {
* Note that `Object.assign` is a shallow copy and will completely overwrite the built-in `presets` or `plugins` array, please use it with caution.
*/
babel?: PluginBabelOptions['babelLoaderOptions'];
/**
* Modify the options of [ts-loader](https://github.com/TypeStrong/ts-loader).
* When `tools.tsLoader` is not undefined, Rsbuild will use ts-loader instead of babel-loader to compile TypeScript code.
*
* Tips: this configuration is not yet supported in rspack
*/
tsLoader?: PluginTsLoaderOptions;
};
dev?: {
/**
Expand Down Expand Up @@ -232,12 +230,23 @@ export type UniBuilderExtraConfig = {
templateParametersByEntries?: Record<string, Record<string, unknown>>;
};
security?: {
/**
* Adding an integrity attribute (`integrity`) to sub-resources introduced by HTML allows the browser to
* verify the integrity of the introduced resource, thus preventing tampering with the downloaded resource.
*
* Tips: this configuration is not yet supported in rspack
*/
sri?: SriOptions | boolean;
/**
* Analyze the build artifacts to identify advanced syntax that is incompatible with the current browser scope.
*/
checkSyntax?: boolean | PluginCheckSyntaxOptions;
};
experiments?: {
/**
* Tips: this configuration is not yet supported in rspack
*/
lazyCompilation?: LazyCompilationOptions;
/**
* Enable the ability for source code building
*/
Expand All @@ -251,29 +260,4 @@ export type SriOptions = {
hashLoading?: 'eager' | 'lazy';
};

export type UniBuilderWebpackConfig = RsbuildConfig &
UniBuilderExtraConfig & {
security?: {
/**
* Adding an integrity attribute (`integrity`) to sub-resources introduced by HTML allows the browser to
* verify the integrity of the introduced resource, thus preventing tampering with the downloaded resource.
*/
sri?: SriOptions | boolean;
};
experiments?: {
lazyCompilation?: LazyCompilationOptions;
};
tools?: {
/**
* Modify the options of [ts-loader](https://github.com/TypeStrong/ts-loader).
* When `tools.tsLoader` is not undefined, Rsbuild will use ts-loader instead of babel-loader to compile TypeScript code.
*/
tsLoader?: PluginTsLoaderOptions;
};
};

export type UniBuilderRspackConfig = RsbuildConfig & UniBuilderExtraConfig;

export type BuilderConfig<B = 'rspack'> = B extends 'rspack'
? UniBuilderRspackConfig
: UniBuilderWebpackConfig;
export type BuilderConfig = RsbuildConfig & UniBuilderExtraConfig;
10 changes: 5 additions & 5 deletions packages/builder/uni-builder/src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from '@rsbuild/core';
import type { StartServerResult } from '@rsbuild/shared';
import type {
UniBuilderWebpackConfig,
CreateWebpackBuilderOptions,
BuilderConfig,
CreateUniBuilderOptions,
CreateBuilderCommonOptions,
} from '../types';
import { parseCommonConfig } from '../shared/parseCommonConfig';
Expand All @@ -17,13 +17,13 @@ import { pluginReact } from './plugins/react';
import type { StartDevServerOptions } from '../shared/devServer';

export async function parseConfig(
uniBuilderConfig: UniBuilderWebpackConfig,
uniBuilderConfig: BuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
rsbuildPlugins: RsbuildPlugin[];
}> {
const { rsbuildConfig, rsbuildPlugins } = await parseCommonConfig<'webpack'>(
const { rsbuildConfig, rsbuildPlugins } = await parseCommonConfig(
uniBuilderConfig,
options,
);
Expand Down Expand Up @@ -84,7 +84,7 @@ type UniBuilderWebpackInstance = Omit<RsbuildInstance, 'startDevServer'> & {
};

export async function createWebpackBuilder(
options: CreateWebpackBuilderOptions,
options: CreateUniBuilderOptions,
): Promise<UniBuilderWebpackInstance> {
const { cwd = process.cwd(), config, ...rest } = options;

Expand Down
35 changes: 10 additions & 25 deletions tests/e2e/builder/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ export const createUniBuilder = async (
) => {
const { createUniBuilder } = await import('@modern-js/uni-builder');

const builder =
process.env.PROVIDE_TYPE === 'rspack'
? await createUniBuilder({
...builderOptions,
bundlerType: 'rspack',
config: builderConfig,
})
: await createUniBuilder({
...builderOptions,
bundlerType: 'webpack',
config: builderConfig as UniBuilderConfig<'webpack'>,
});
const builder = await createUniBuilder({
...builderOptions,
bundlerType: process.env.PROVIDE_TYPE === 'rspack' ? 'rspack' : 'webpack',
config: builderConfig,
});

return builder;
};
Expand Down Expand Up @@ -102,11 +95,7 @@ function getRandomPort(defaultPort = Math.ceil(Math.random() * 10000) + 10000) {
}
}

const updateConfigForTest = <BuilderType>(
config: BuilderType extends 'webpack'
? UniBuilderConfig<'webpack'>
: UniBuilderConfig<'rspack'>,
) => {
const updateConfigForTest = (config: UniBuilderConfig) => {
// make devPort random to avoid port conflict
config.dev = {
...(config.dev || {}),
Expand Down Expand Up @@ -141,14 +130,12 @@ const updateConfigForTest = <BuilderType>(
}
};

export async function dev<BuilderType = 'webpack'>({
export async function dev({
serverOptions,
builderConfig = {},
...options
}: CreateBuilderOptions & {
builderConfig?: BuilderType extends 'webpack'
? UniBuilderConfig<'webpack'>
: UniBuilderConfig<'rspack'>;
builderConfig?: UniBuilderConfig;
serverOptions?: StartDevServerOptions['serverOptions'];
}) {
process.env.NODE_ENV = 'development';
Expand All @@ -162,7 +149,7 @@ export async function dev<BuilderType = 'webpack'>({
});
}

export async function build<BuilderType = 'webpack'>({
export async function build({
plugins,
runServer = false,
builderConfig = {},
Expand All @@ -173,9 +160,7 @@ export async function build<BuilderType = 'webpack'>({
runServer?: boolean;
/** TODO: should removed when all test cases migrate to uniBuilder */
useUniBuilder?: boolean;
builderConfig?: BuilderType extends 'webpack'
? UniBuilderConfig<'webpack'>
: UniBuilderConfig<'rspack'>;
builderConfig?: UniBuilderConfig;
}) {
process.env.NODE_ENV = 'production';

Expand Down

0 comments on commit 60d7564

Please sign in to comment.