Skip to content

Commit

Permalink
fix: shared utils
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Jun 19, 2024
1 parent ede1db6 commit f0535a6
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 41 deletions.
10 changes: 6 additions & 4 deletions packages/cli/rsbuild-plugin-esbuild/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from 'path';
import { JS_REGEX, TS_REGEX, applyScriptCondition } from '@rsbuild/shared';
import { JS_REGEX, applyScriptCondition } from '@rsbuild/shared';
import type { RsbuildPlugin } from '@rsbuild/core';
import type {
LoaderOptions,
MinifyPluginOptions,
} from '../compiled/esbuild-loader';

const TS_REGEX = /\.(?:ts|mts|cts|tsx)$/;

export interface PluginEsbuildOptions {
loader?: false | LoaderOptions;
minimize?: false | MinifyPluginOptions;
Expand Down Expand Up @@ -50,7 +52,7 @@ export function pluginEsbuild(
chain.module
.rule(CHAIN_ID.RULE.JS)
.test(JS_REGEX)
.use(CHAIN_ID.USE.ESBUILD)
.use('esbuild')
.loader(esbuildLoaderPath)
.options({
loader: 'jsx',
Expand All @@ -60,7 +62,7 @@ export function pluginEsbuild(
const rule = chain.module.rule(CHAIN_ID.RULE.TS);
rule
.test(TS_REGEX)
.use(CHAIN_ID.USE.ESBUILD)
.use('esbuild')
.loader(esbuildLoaderPath)
.options({
loader: 'tsx',
Expand All @@ -87,7 +89,7 @@ export function pluginEsbuild(
.delete(CHAIN_ID.MINIMIZER.CSS);

chain.optimization
.minimizer(CHAIN_ID.MINIMIZER.ESBUILD)
.minimizer('js-css')
.use(ESBuildMinifyPlugin)
.init(
() =>
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/uni-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ export {
type RsbuildConfig,
} from '@rsbuild/core';
export type { webpack, WebpackConfig } from '@rsbuild/webpack';
export { RUNTIME_CHUNK_NAME } from './shared/constants';
export { RUNTIME_CHUNK_NAME } from './shared/utils';
3 changes: 1 addition & 2 deletions packages/cli/uni-builder/src/shared/compatLegacyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type {
UniBuilderPluginAPI,
UniBuilderContext,
} from '../types';
import type { RsbuildPlugin } from '@rsbuild/core';
import { logger } from '@rsbuild/shared';
import { type RsbuildPlugin, logger } from '@rsbuild/core';
import { join } from 'path';

function addDeprecatedWarning(
Expand Down
1 change: 0 additions & 1 deletion packages/cli/uni-builder/src/shared/constants.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/cli/uni-builder/src/shared/devServer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {
StartDevServerOptions as RsbuildStartDevServerOptions,
debug,
deepmerge,
DevConfig,
ServerConfig,
isProd,
} from '@rsbuild/shared';
import { applyOptionsChain } from '@modern-js/utils';

import type { RsbuildInstance } from '@rsbuild/core';
import { type RsbuildInstance, logger } from '@rsbuild/core';

import type { ModernDevServerOptions } from '@modern-js/server';
import type { Server } from 'node:http';
Expand Down Expand Up @@ -158,7 +157,7 @@ export async function startDevServer(
options: StartDevServerOptions = {},
builderConfig: UniBuilderConfig,
) {
debug('create dev server');
logger.debug('create dev server');

if (!options.initProdMiddlewares) {
options.initProdMiddlewares = initProdMiddlewares;
Expand Down Expand Up @@ -209,7 +208,7 @@ export async function startDevServer(
options.initProdMiddlewares,
);

debug('listen dev server');
logger.debug('listen dev server');

return new Promise<UniBuilderStartServerResult>(resolve => {
server.listen(
Expand All @@ -222,7 +221,7 @@ export async function startDevServer(
throw err;
}

debug('listen dev server done');
logger.debug('listen dev server done');

await rsbuildServer.afterListen();

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/uni-builder/src/shared/plugins/antd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isServerTarget, type RsbuildPlugin } from '@rsbuild/shared';
import { type RsbuildPlugin } from '@rsbuild/shared';
import { isServerTarget } from '../utils';

const getAntdMajorVersion = (appDirectory: string) => {
try {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/uni-builder/src/shared/plugins/arco.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isServerTarget, type RsbuildPlugin } from '@rsbuild/shared';
import { type RsbuildPlugin } from '@rsbuild/shared';
import { isPackageInstalled } from '@modern-js/utils';
import { isServerTarget } from '../../shared/utils';

export const pluginArco = (): RsbuildPlugin => ({
name: 'uni-builder:arco',
Expand Down
15 changes: 5 additions & 10 deletions packages/cli/uni-builder/src/shared/plugins/fallback.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { join } from 'path';
import {
JS_REGEX,
TS_REGEX,
getDistPath,
getFilename,
type Rspack,
} from '@rsbuild/shared';
import { JS_REGEX, type Rspack } from '@rsbuild/shared';
import type { RsbuildPlugin } from '@rsbuild/core';
import { TS_REGEX, getFilename } from '../../shared/utils';

const HTML_REGEX = /\.html$/;

Expand Down Expand Up @@ -72,7 +67,8 @@ export const pluginFallback = (): RsbuildPlugin => ({
if (api.context.bundlerType === 'webpack') {
api.modifyBundlerChain((chain, { isProd }) => {
const rsbuildConfig = api.getNormalizedConfig();
const distDir = getDistPath(rsbuildConfig, 'media');
const { distPath } = rsbuildConfig.output;
const distDir = distPath.media;
const filename = getFilename(rsbuildConfig, 'media', isProd);

chain.output.merge({
Expand All @@ -91,8 +87,7 @@ export const pluginFallback = (): RsbuildPlugin => ({
} else {
api.modifyRspackConfig((config, { isProd }) => {
const rsbuildConfig = api.getNormalizedConfig();

const distDir = getDistPath(rsbuildConfig, 'media');
const distDir = rsbuildConfig.output.distPath.media;
const filename = getFilename(rsbuildConfig, 'media', isProd);

config.output ||= {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RsbuildPlugin } from '@rsbuild/core';
import { RUNTIME_CHUNK_NAME } from '../constants';
import { RUNTIME_CHUNK_NAME } from '../utils';

export const pluginRuntimeChunk = (
disableInlineRuntimeChunk?: boolean,
Expand Down
59 changes: 59 additions & 0 deletions packages/cli/uni-builder/src/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type {
RsbuildTarget,
NormalizedConfig,
FilenameConfig,
} from '@rsbuild/core';

export const RUNTIME_CHUNK_NAME = 'builder-runtime';

export const TS_REGEX = /\.(?:ts|mts|cts|tsx)$/;

export function isServerTarget(target: RsbuildTarget[]) {
return (Array.isArray(target) ? target : [target]).some(item =>
['node', 'service-worker'].includes(item),
);
}

export function getFilename(
config: NormalizedConfig,
type: 'js',
isProd: boolean,
): NonNullable<FilenameConfig['js']>;
export function getFilename(
config: NormalizedConfig,
type: Exclude<keyof FilenameConfig, 'js'>,
isProd: boolean,
): string;
export function getFilename(
config: NormalizedConfig,
type: keyof FilenameConfig,
isProd: boolean,
) {
const { filename, filenameHash } = config.output;

const getHash = () => {
if (typeof filenameHash === 'string') {
return filenameHash ? `.[${filenameHash}]` : '';
}
return filenameHash ? '.[contenthash:8]' : '';
};

const hash = getHash();

switch (type) {
case 'js':
return filename.js ?? `[name]${isProd ? hash : ''}.js`;
case 'css':
return filename.css ?? `[name]${isProd ? hash : ''}.css`;
case 'svg':
return filename.svg ?? `[name]${hash}.svg`;
case 'font':
return filename.font ?? `[name]${hash}[ext]`;
case 'image':
return filename.image ?? `[name]${hash}[ext]`;
case 'media':
return filename.media ?? `[name]${hash}[ext]`;
default:
throw new Error(`unknown key ${type} in "output.filename"`);
}
}
3 changes: 2 additions & 1 deletion packages/cli/uni-builder/src/webpack/ModuleScopePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* modified from https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/ModuleScopePlugin.js
*/
import { dirname, relative, resolve } from 'path';
import { color, isRegExp, isString } from '@rsbuild/shared';
import { color } from '@rsbuild/shared';
import { isRegExp, isString } from '@modern-js/utils';

export class ModuleScopePlugin {
scopes: Array<string | RegExp>;
Expand Down
16 changes: 7 additions & 9 deletions packages/cli/uni-builder/src/webpack/plugins/moduleScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const pluginModuleScopes = (
name: 'uni-builder:module-scopes',

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID }) => {
api.modifyBundlerChain(async chain => {
if (!moduleScopes) {
return;
}
Expand All @@ -55,14 +55,12 @@ export const pluginModuleScopes = (
return scope;
});

chain.resolve
.plugin(CHAIN_ID.RESOLVE_PLUGIN.MODULE_SCOPE)
.use(ModuleScopePlugin, [
{
scopes: formattedScopes,
allowedFiles: [rootPackageJson],
},
]);
chain.resolve.plugin('module-scope').use(ModuleScopePlugin, [
{
scopes: formattedScopes,
allowedFiles: [rootPackageJson],
},
]);
});
},
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { RsbuildPlugin } from '@rsbuild/core';
import { PLUGIN_SWC_NAME } from '@rsbuild/core';
import { isServerTarget, type ConfigChain } from '@rsbuild/shared';
import { type ConfigChain } from '@rsbuild/shared';
import { applyOptionsChain } from '@modern-js/utils';
import type { PluginStyledComponentsOptions } from '@rsbuild/plugin-styled-components';
import { isServerTarget } from '../../shared/utils';

const getDefaultStyledComponentsConfig = (isProd: boolean, ssr: boolean) => {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/uni-builder/src/webpack/plugins/tsLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
TS_REGEX,
JS_REGEX,
castArray,
applyScriptCondition,
Expand All @@ -17,6 +16,7 @@ import { getBabelConfigForWeb } from '@rsbuild/babel-preset/web';
import type { RsbuildPlugin } from '@rsbuild/core';
import type { Options as RawTSLoaderOptions } from 'ts-loader';
import { getPresetReact } from './babel';
import { TS_REGEX } from '../../shared/utils';

export type TSLoaderOptions = Partial<RawTSLoaderOptions>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
isHtmlDisabled,
RsbuildPlugin,
RspackChain,
createVirtualModule,
ChainIdentifier,
RsbuildPluginAPI,
} from '@rsbuild/shared';
Expand All @@ -17,6 +16,9 @@ import { HtmlUserConfig } from '../../../types/config/html';
import { BottomTemplatePlugin } from '../bundlerPlugins';
import type { BuilderOptions } from '../types';

const createVirtualModule = (content: string) =>
`data:text/javascript,${content}`;

export const builderPluginAdapterHtml = <B extends Bundler>(
options: BuilderOptions<B>,
): RsbuildPlugin => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/storybook/builder/src/docgen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function applyDocgenWebpack(chain: RspackChain, options: Options) {
resolveOptions,
})
.after(CHAIN_ID.USE.BABEL)
.after(CHAIN_ID.USE.ESBUILD)
.after('esbuild')
.after(CHAIN_ID.USE.SWC)
.end();

Expand All @@ -56,7 +56,7 @@ export async function applyDocgenWebpack(chain: RspackChain, options: Options) {
resolveOptions,
})
.after(CHAIN_ID.USE.TS)
.after(CHAIN_ID.USE.ESBUILD)
.after('esbuild')
.after(CHAIN_ID.USE.SWC)
.end();
}
Expand Down

0 comments on commit f0535a6

Please sign in to comment.