Skip to content

Commit

Permalink
fix(uni-builder): some type & logic problems (#5171)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Dec 29, 2023
1 parent 4f41540 commit 3bc8198
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/builder/uni-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"compiled",
"dist"
],
"scripts": {
Expand Down
37 changes: 31 additions & 6 deletions packages/builder/uni-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { createRspackBuilder } from './rspack';
import { createWebpackBuilder } from './webpack';
import { createRspackBuilder, type UniBuilderInstance } from './rspack';
import {
createWebpackBuilder,
type UniBuilderWebpackInstance,
} from './webpack';
import type { CreateUniBuilderOptions } from './types';
import HtmlWebpackPlugin from 'html-webpack-plugin';

export type { CreateUniBuilderOptions };
export type { BundlerChain, RsbuildPlugin } from '@rsbuild/shared';
export type { BuilderConfig } from './types';
export { HtmlWebpackPlugin };

export type {
CreateUniBuilderOptions,
UniBuilderInstance,
UniBuilderWebpackInstance,
};
export type {
BundlerChain,
RsbuildPlugin,
ChainedConfig,
CopyPluginOptions,
ChainIdentifier,
NormalizedConfig,
RspackConfig,
CacheGroup,
} from '@rsbuild/shared';
export type { UniBuilderConfig } from './types';
export type { StartDevServerOptions } from './shared/devServer';

export async function createUniBuilder(options: CreateUniBuilderOptions) {
Expand All @@ -13,5 +32,11 @@ export async function createUniBuilder(options: CreateUniBuilderOptions) {
: createWebpackBuilder(options);
}

export { logger } from '@rsbuild/core';
export {
logger,
type Rspack,
type RsbuildContext,
type RsbuildConfig,
} from '@rsbuild/core';
export type { webpack, WebpackChain, WebpackConfig } from '@rsbuild/webpack';
export { RUNTIME_CHUNK_NAME } from './shared/constants';
17 changes: 8 additions & 9 deletions packages/builder/uni-builder/src/rspack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import type {
RsbuildPlugin,
RsbuildInstance,
} from '@rsbuild/core';
import type { RsbuildProvider, StartServerResult } from '@rsbuild/shared';
import type {
BuilderConfig,
UniBuilderConfig,
CreateUniBuilderOptions,
CreateBuilderCommonOptions,
} from '../types';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import type { StartDevServerOptions } from '../shared/devServer';
import type {
StartDevServerOptions,
UniBuilderStartServerResult,
} from '../shared/devServer';

export async function parseConfig(
uniBuilderConfig: BuilderConfig,
uniBuilderConfig: UniBuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
Expand Down Expand Up @@ -54,18 +56,15 @@ export async function parseConfig(
};
}

type UniBuilderInstance = Omit<
RsbuildInstance<RsbuildProvider>,
'startDevServer'
> & {
export type UniBuilderInstance = Omit<RsbuildInstance, 'startDevServer'> & {
/**
* should be used in conjunction with the upper-layer framework:
*
* missing route.json (required in modern server)
*/
startDevServer: (
options: StartDevServerOptions,
) => Promise<StartServerResult>;
) => Promise<UniBuilderStartServerResult>;
};

export async function createRspackBuilder(
Expand Down
16 changes: 10 additions & 6 deletions packages/builder/uni-builder/src/shared/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import {
deepmerge,
mergeChainedOptions,
} from '@rsbuild/shared';
import type { ModernDevServerOptionsNew } from '@modern-js/server';
import type { Server, ModernDevServerOptionsNew } from '@modern-js/server';
import { type ModernServerOptions } from '@modern-js/prod-server';
import { BuilderConfig } from '../types';
import { UniBuilderConfig } from '../types';

type ServerOptions = Partial<Omit<ModernDevServerOptionsNew, 'config'>> & {
config?: Partial<ModernDevServerOptionsNew['config']>;
};

const getServerOptions = (
builderConfig: BuilderConfig,
builderConfig: UniBuilderConfig,
): ModernServerOptions['config'] => {
return {
output: {
Expand All @@ -42,7 +42,7 @@ const getDevServerOptions = async ({
serverOptions,
port,
}: {
builderConfig: BuilderConfig;
builderConfig: UniBuilderConfig;
serverOptions: ServerOptions;
port: number;
}): Promise<{
Expand Down Expand Up @@ -88,10 +88,14 @@ export type StartDevServerOptions = Omit<
serverOptions?: ServerOptions;
};

export type UniBuilderStartServerResult = Omit<StartServerResult, 'server'> & {
server: Server;
};

export async function startDevServer(
rsbuild: RsbuildInstance,
options: StartDevServerOptions = {},
builderConfig: BuilderConfig,
builderConfig: UniBuilderConfig,
) {
debug('create dev server');

Expand Down Expand Up @@ -139,7 +143,7 @@ export async function startDevServer(

await server.init();

return new Promise<StartServerResult>(resolve => {
return new Promise<UniBuilderStartServerResult>(resolve => {
server.listen(
{
host,
Expand Down
11 changes: 7 additions & 4 deletions packages/builder/uni-builder/src/shared/parseCommonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type RsbuildPlugin,
type RsbuildConfig,
} from '@rsbuild/core';
import type { CreateBuilderCommonOptions, BuilderConfig } from '../types';
import type { CreateBuilderCommonOptions, UniBuilderConfig } from '../types';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginGlobalVars } from './plugins/globalVars';
import { pluginRuntimeChunk } from './plugins/runtimeChunk';
Expand Down Expand Up @@ -82,14 +82,15 @@ async function getBrowserslistWithDefault(
}

export async function parseCommonConfig(
uniBuilderConfig: BuilderConfig,
uniBuilderConfig: UniBuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
rsbuildPlugins: RsbuildPlugin[];
}> {
const { cwd, frameworkConfigPath, entry, target } = options;
const rsbuildConfig = deepmerge({}, uniBuilderConfig);
const rsbuildConfig = deepmerge({}, uniBuilderConfig) as RsbuildConfig &
UniBuilderConfig;
const { dev = {}, html = {}, output = {}, tools = {} } = rsbuildConfig;

if (output.cssModuleLocalIdentName) {
Expand Down Expand Up @@ -192,7 +193,9 @@ export async function parseCommonConfig(
dev.progressBar = true;
}

dev.writeToDisk ??= (file: string) => !file.includes('.hot-update.');
dev.writeToDisk =
tools.devServer?.devMiddleware?.writeToDisk ??
((file: string) => !file.includes('.hot-update.'));

const server: ServerConfig = isProd()
? {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const pluginFrameworkConfig = (configPath: string): RsbuildPlugin => ({

const cache = chain.get('cache');

if (!cache) {
return;
}

cache.buildDependencies = {
...cache.buildDependencies,
frameworkConfig: [configPath],
Expand Down
17 changes: 15 additions & 2 deletions packages/builder/uni-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { PluginTypeCheckerOptions } from '@rsbuild/plugin-type-check';
import type { PluginCheckSyntaxOptions } from '@rsbuild/plugin-check-syntax';
import type { PluginPugOptions } from '@rsbuild/plugin-pug';
import type { PluginBabelOptions } from '@rsbuild/plugin-babel';
import type { AliasOption } from '@modern-js/utils';

export type CreateBuilderCommonOptions = {
entry?: RsbuildEntry;
Expand All @@ -34,7 +35,7 @@ export type CreateBuilderCommonOptions = {

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

export type GlobalVars = Record<string, any>;
Expand Down Expand Up @@ -120,6 +121,8 @@ export type UniBuilderExtraConfig = {
port?: number;
};
source?: {
// TODO: need to support rsbuild alias type in server/utils
alias?: AliasOption;
/**
* Define global variables. It can replace expressions like `process.env.FOO` in your code after compile.
*/
Expand Down Expand Up @@ -212,6 +215,8 @@ export type UniBuilderExtraConfig = {
* @deprecated use `html.title` instead
*/
titleByEntries?: Record<string, string>;
// TODO: need support rsbuild favicon type in server/utils
favicon?: string;
/**
* @deprecated use `html.favicon` instead
*/
Expand Down Expand Up @@ -260,4 +265,12 @@ export type SriOptions = {
hashLoading?: 'eager' | 'lazy';
};

export type BuilderConfig = RsbuildConfig & UniBuilderExtraConfig;
export type UniBuilderConfig = {
dev?: RsbuildConfig['dev'];
html?: RsbuildConfig['html'];
output?: RsbuildConfig['output'];
performance?: RsbuildConfig['performance'];
security?: RsbuildConfig['security'];
tools?: RsbuildConfig['tools'];
source?: Omit<NonNullable<RsbuildConfig['source']>, 'alias'>;
} & UniBuilderExtraConfig;
18 changes: 12 additions & 6 deletions packages/builder/uni-builder/src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import {
type RsbuildPlugin,
type RsbuildInstance,
} from '@rsbuild/core';
import type { StartServerResult } from '@rsbuild/shared';
import type { RsbuildProvider } from '@rsbuild/shared';
import type {
BuilderConfig,
UniBuilderConfig,
CreateUniBuilderOptions,
CreateBuilderCommonOptions,
} from '../types';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import { pluginModuleScopes } from './plugins/moduleScopes';
import { pluginBabel } from './plugins/babel';
import { pluginReact } from './plugins/react';
import type { StartDevServerOptions } from '../shared/devServer';
import type {
StartDevServerOptions,
UniBuilderStartServerResult,
} from '../shared/devServer';

export async function parseConfig(
uniBuilderConfig: BuilderConfig,
uniBuilderConfig: UniBuilderConfig,
options: CreateBuilderCommonOptions,
): Promise<{
rsbuildConfig: RsbuildConfig;
Expand Down Expand Up @@ -77,10 +80,13 @@ export async function parseConfig(
};
}

type UniBuilderWebpackInstance = Omit<RsbuildInstance, 'startDevServer'> & {
export type UniBuilderWebpackInstance = Omit<
RsbuildInstance<RsbuildProvider<'webpack'>>,
'startDevServer'
> & {
startDevServer: (
options: StartDevServerOptions,
) => Promise<StartServerResult>;
) => Promise<UniBuilderStartServerResult>;
};

export async function createWebpackBuilder(
Expand Down

0 comments on commit 3bc8198

Please sign in to comment.