Skip to content

Commit

Permalink
feat: support applymore environment configs
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Jun 21, 2024
1 parent 6009601 commit e5c5c56
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 187 deletions.
4 changes: 2 additions & 2 deletions packages/compat/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export const pluginAdaptor = (): RsbuildPlugin => ({
name: 'rsbuild-webpack:adaptor',

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID, target }) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, target }) => {
const config = api.getNormalizedConfig({ environment });

if (
api.context.tsconfigPath &&
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/plugins/basic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path';
import { isProd } from '../helpers';
import type { NormalizedConfig, RsbuildPlugin } from '../types';
import type { NormalizedEnvironmentConfig, RsbuildPlugin } from '../types';

const getJsSourceMap = (config: NormalizedConfig) => {
const getJsSourceMap = (config: NormalizedEnvironmentConfig) => {
const { sourceMap } = config.output;
if (sourceMap.js === undefined) {
return isProd() ? false : 'cheap-module-source-map';
Expand All @@ -19,7 +19,7 @@ export const pluginBasic = (): RsbuildPlugin => ({
setup(api) {
api.modifyBundlerChain(
(chain, { env, isProd, target, bundler, environment, CHAIN_ID }) => {
const config = api.getNormalizedConfig();
const config = api.getNormalizedConfig({ environment });

chain.name(environment);

Expand Down
21 changes: 10 additions & 11 deletions packages/core/src/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ import { CSS_REGEX, LOADER_PATH } from '../constants';
import { getCompiledPath } from '../helpers';
import { getCssExtractPlugin } from '../pluginHelper';
import { reduceConfigs, reduceConfigsWithContext } from '../reduceConfigs';
import type { NormalizedConfig, RsbuildPlugin } from '../types';

export const enableNativeCss = (config: NormalizedConfig) =>
!config.output.injectStyles;
import type { NormalizedEnvironmentConfig, RsbuildPlugin } from '../types';

export const isUseCssExtract = (
config: NormalizedConfig,
config: NormalizedEnvironmentConfig,
target: RsbuildTarget,
) =>
!config.output.injectStyles && target !== 'node' && target !== 'web-worker';

const getCSSModulesLocalIdentName = (
config: NormalizedConfig,
config: NormalizedEnvironmentConfig,
isProd: boolean,
) =>
config.output.cssModules.localIdentName ||
Expand Down Expand Up @@ -111,7 +108,7 @@ async function loadUserPostcssrc(root: string): Promise<PostCSSOptions> {
export const applyAutoprefixer = async (
plugins: unknown[],
browserslist: string[],
config: NormalizedConfig,
config: NormalizedEnvironmentConfig,
) => {
const pluginObjects: AcceptedPlugin[] = plugins.map((plugin) =>
isFunction(plugin) ? plugin({}) : plugin,
Expand Down Expand Up @@ -151,7 +148,7 @@ const getPostcssLoaderOptions = async ({
root,
}: {
browserslist: string[];
config: NormalizedConfig;
config: NormalizedEnvironmentConfig;
root: string;
}): Promise<PostCSSLoaderOptions> => {
const extraPlugins: AcceptedPlugin[] = [];
Expand Down Expand Up @@ -207,7 +204,7 @@ const getCSSLoaderOptions = ({
target,
localIdentName,
}: {
config: NormalizedConfig;
config: NormalizedEnvironmentConfig;
importLoaders: number;
target: RsbuildTarget;
localIdentName: string;
Expand Down Expand Up @@ -245,7 +242,7 @@ async function applyCSSRule({
importLoaders = 1,
}: {
rule: RspackChain.Rule;
config: NormalizedConfig;
config: NormalizedEnvironmentConfig;
context: RsbuildContext;
utils: ModifyChainUtils;
importLoaders?: number;
Expand Down Expand Up @@ -331,7 +328,9 @@ export const pluginCss = (): RsbuildPlugin => ({
order: 'pre',
handler: async (chain, utils) => {
const rule = chain.module.rule(utils.CHAIN_ID.RULE.CSS);
const config = api.getNormalizedConfig();
const config = api.getNormalizedConfig({
environment: utils.environment,
});
rule.test(CSS_REGEX);
await applyCSSRule({
rule,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ export const pluginResolve = (): RsbuildPlugin => ({
},
});

api.modifyRspackConfig(async (rspackConfig) => {
api.modifyRspackConfig(async (rspackConfig, { environment }) => {
const isTsProject = Boolean(api.context.tsconfigPath);
const config = api.getNormalizedConfig();
const config = api.getNormalizedConfig({ environment });

rspackConfig.resolve ||= {};

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/plugins/splitChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
name: 'rsbuild:split-chunks',
setup(api) {
api.modifyBundlerChain(
async (chain, { isServer, isWebWorker, isServiceWorker }) => {
async (
chain,
{ environment, isServer, isWebWorker, isServiceWorker },
) => {
if (isServer || isWebWorker || isServiceWorker) {
chain.optimization.splitChunks(false);

Expand All @@ -244,7 +247,7 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
return;
}

const config = api.getNormalizedConfig();
const config = api.getNormalizedConfig({ environment });
const defaultConfig: Exclude<SplitChunks, false> = {
// Optimize both `initial` and `async` chunks
chunks: 'all',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ export type {
NormalizedSecurityConfig,
NormalizedPerformanceConfig,
NormalizedModuleFederationConfig,
NormalizedEnvironmentConfig,
} from '@rsbuild/shared';
4 changes: 2 additions & 2 deletions packages/plugin-css-minimizer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export const pluginCssMinimizer = (
name: PLUGIN_CSS_MINIMIZER_NAME,

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isProd }) => {
const config = api.getNormalizedConfig({ environment });
const { minify } = config.output;

if (
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-less/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export const pluginLess = (
name: PLUGIN_LESS_NAME,

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID }) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
const config = api.getNormalizedConfig({ environment });
const rule = chain.module
.rule(CHAIN_ID.RULE.LESS)
.test(/\.less$/)
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-lightningcss/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ export const pluginLightningcss = (
},
});

api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isProd }) => {
const config = api.getNormalizedConfig({ environment });
const { minify } = config.output;
const isMinimize =
isProd &&
Expand Down
91 changes: 48 additions & 43 deletions packages/plugin-react/src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,61 @@ export const applyBasicReactSupport = (
) => {
const REACT_REFRESH_PATH = require.resolve('react-refresh');

api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd, target }) => {
const config = api.getNormalizedConfig();
const usingHMR = !isProd && config.dev.hmr && target === 'web';
const reactOptions: Rspack.SwcLoaderTransformConfig['react'] = {
development: isDev,
refresh: usingHMR,
runtime: 'automatic',
...options.swcReactOptions,
};
api.modifyBundlerChain(
async (chain, { CHAIN_ID, environment, isDev, isProd, target }) => {
const config = api.getNormalizedConfig({ environment });
const usingHMR = !isProd && config.dev.hmr && target === 'web';
const reactOptions: Rspack.SwcLoaderTransformConfig['react'] = {
development: isDev,
refresh: usingHMR,
runtime: 'automatic',
...options.swcReactOptions,
};

modifySwcLoaderOptions({
chain,
CHAIN_ID,
modifier: (opts) => {
const extraOptions: Rspack.SwcLoaderOptions = {
jsc: {
parser: {
syntax: 'typescript',
// enable supports for React JSX/TSX compilation
tsx: true,
},
transform: {
react: reactOptions,
modifySwcLoaderOptions({
chain,
CHAIN_ID,
modifier: (opts) => {
const extraOptions: Rspack.SwcLoaderOptions = {
jsc: {
parser: {
syntax: 'typescript',
// enable supports for React JSX/TSX compilation
tsx: true,
},
transform: {
react: reactOptions,
},
},
},
};
};

return deepmerge(opts, extraOptions);
},
});
return deepmerge(opts, extraOptions);
},
});

if (!usingHMR) {
return;
}
if (!usingHMR) {
return;
}

chain.resolve.alias.set('react-refresh', path.dirname(REACT_REFRESH_PATH));
chain.resolve.alias.set(
'react-refresh',
path.dirname(REACT_REFRESH_PATH),
);

const { default: ReactRefreshRspackPlugin } = await import(
'@rspack/plugin-react-refresh'
);
const { default: ReactRefreshRspackPlugin } = await import(
'@rspack/plugin-react-refresh'
);

chain
.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH)
.use(ReactRefreshRspackPlugin, [
{
include: [SCRIPT_REGEX],
...options.reactRefreshOptions,
},
]);
});
chain
.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH)
.use(ReactRefreshRspackPlugin, [
{
include: [SCRIPT_REGEX],
...options.reactRefreshOptions,
},
]);
},
);
};

export const applyReactProfiler = (api: RsbuildPluginAPI) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-react/src/splitChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const applySplitChunksRule = (
router: true,
},
) => {
api.modifyBundlerChain((chain) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain((chain, { environment }) => {
const config = api.getNormalizedConfig({ environment });
if (config.performance.chunkSplit.strategy !== 'split-by-experience') {
return;
}
Expand Down
42 changes: 22 additions & 20 deletions packages/plugin-rem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,30 @@ export const pluginRem = (options: PluginRemOptions = {}): RsbuildPlugin => ({
});
});

api.modifyBundlerChain(async (chain, { target, CHAIN_ID, HtmlPlugin }) => {
if (target !== 'web' || !userOptions.enableRuntime) {
return;
}
api.modifyBundlerChain(
async (chain, { target, CHAIN_ID, environment, HtmlPlugin }) => {
if (target !== 'web' || !userOptions.enableRuntime) {
return;
}

const { AutoSetRootFontSizePlugin } = await import(
'./AutoSetRootFontSizePlugin'
);
const { AutoSetRootFontSizePlugin } = await import(
'./AutoSetRootFontSizePlugin'
);

const entries = Object.keys(chain.entryPoints.entries() || {});
const config = api.getNormalizedConfig();
const distDir = config.output.distPath.js;
const entries = Object.keys(chain.entryPoints.entries() || {});
const config = api.getNormalizedConfig({ environment });
const distDir = config.output.distPath.js;

chain
.plugin(CHAIN_ID.PLUGIN.AUTO_SET_ROOT_SIZE)
.use(AutoSetRootFontSizePlugin, [
userOptions,
entries,
HtmlPlugin,
distDir,
config.html.scriptLoading,
]);
});
chain
.plugin(CHAIN_ID.PLUGIN.AUTO_SET_ROOT_SIZE)
.use(AutoSetRootFontSizePlugin, [
userOptions,
entries,
HtmlPlugin,
distDir,
config.html.scriptLoading,
]);
},
);
},
});
Loading

0 comments on commit e5c5c56

Please sign in to comment.