Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use environment param instead of context.environment #2721

Merged
merged 9 commits into from
Jun 28, 2024
5 changes: 2 additions & 3 deletions packages/compat/plugin-swc/src/plugin.ts
9aoy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export const pluginSwc = (options: PluginSwcOptions = {}): RsbuildPlugin => ({
order: 'pre',
handler: async (chain, utils) => {
const { CHAIN_ID, environment } = utils;
const rsbuildConfig = api.getNormalizedConfig({ environment });
const { normalizedConfig: rsbuildConfig, browserslist } = environment;
const { rootPath } = api.context;

const { browserslist } = api.context.environments[environment];
const swcConfigs = await applyPluginConfig(
options,
utils,
Expand Down Expand Up @@ -116,7 +115,7 @@ export const pluginSwc = (options: PluginSwcOptions = {}): RsbuildPlugin => ({
});

api.modifyBundlerChain((chain, { CHAIN_ID, isProd, environment }) => {
const rsbuildConfig = api.getNormalizedConfig({ environment });
const rsbuildConfig = environment.normalizedConfig;
9aoy marked this conversation as resolved.
Show resolved Hide resolved

if (checkUseMinify(mainConfig, rsbuildConfig, isProd)) {
const { minify } = rsbuildConfig.output;
Expand Down
7 changes: 6 additions & 1 deletion packages/compat/webpack/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ export const build = async (
let isFirstCompile = true;
await context.hooks.onBeforeBuild.call({
bundlerConfigs: bundlerConfigs as RspackConfig[],
environments: context.environments,
});

const onDone = async (stats: Stats | MultiStats) => {
const p = context.hooks.onAfterBuild.call({ isFirstCompile, stats });
const p = context.hooks.onAfterBuild.call({
isFirstCompile,
stats,
environments: context.environments,
});
isFirstCompile = false;
await p;
};
Expand Down
1 change: 1 addition & 0 deletions packages/compat/webpack/src/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export async function createCompiler({

await context.hooks.onAfterCreateCompiler.call({
compiler,
environments: context.environments,
});
logger.debug('create compiler done');

Expand Down
5 changes: 2 additions & 3 deletions packages/compat/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export const pluginAdaptor = (): RsbuildPlugin => ({

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, target }) => {
const config = api.getNormalizedConfig({ environment });
const { tsconfigPath } = api.context.environments[environment];
const { normalizedConfig: config, tsconfigPath } = environment;

if (tsconfigPath && config.source.aliasStrategy === 'prefer-tsconfig') {
await applyTsConfigPathsPlugin({
Expand All @@ -80,7 +79,7 @@ export const pluginAdaptor = (): RsbuildPlugin => ({
const { ProgressPlugin } = await import('./progress/ProgressPlugin');
chain.plugin(CHAIN_ID.PLUGIN.PROGRESS).use(ProgressPlugin, [
{
id: environment,
id: environment.name,
...(progress === true ? {} : progress),
},
]);
Expand Down
8 changes: 6 additions & 2 deletions packages/compat/webpack/src/webpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
reduceConfigsWithContext,
} from '@rsbuild/core';
import {
type EnvironmentContext,
type ModifyWebpackChainUtils,
type ModifyWebpackConfigUtils,
castArray,
Expand Down Expand Up @@ -68,7 +69,7 @@ async function modifyWebpackConfig(

async function getChainUtils(
target: RsbuildTarget,
environment: string,
environment: EnvironmentContext,
): Promise<ModifyWebpackChainUtils> {
const { default: webpack } = await import('webpack');
const nameMap = {
Expand Down Expand Up @@ -142,7 +143,10 @@ export async function generateWebpackConfig({
target: RsbuildTarget;
context: InternalContext;
}): Promise<WebpackConfig> {
const chainUtils = await getChainUtils(target, environment);
const chainUtils = await getChainUtils(
target,
context.environments[environment],
);
const { default: webpack } = await import('webpack');
const {
BannerPlugin,
Expand Down
277 changes: 275 additions & 2 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,145 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"version": 5,
},
HtmlBasicPlugin {
"environment": "web",
"environment": {
"browserslist": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
"distPath": "<ROOT>/packages/compat/webpack/tests/dist",
"entry": {
"index": "src/index.js",
},
"htmlPaths": {
"index": "index.html",
},
"index": 0,
"name": "web",
"normalizedConfig": {
"_privateMeta": {
"configFilePath": "/path/to/rsbuild.config.ts",
},
"dev": {
"assetPrefix": "/",
"client": {
"host": "",
"overlay": true,
"path": "/rsbuild-hmr",
"port": "",
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
"html": {
"crossorigin": false,
"inject": "head",
"meta": {
"charset": {
"charset": "UTF-8",
},
"viewport": "width=device-width, initial-scale=1.0",
},
"mountId": "root",
"outputStructure": "flat",
"scriptLoading": "defer",
"title": "Rsbuild App",
},
"output": {
"assetPrefix": "/",
"charset": "ascii",
"cssModules": {
"auto": true,
"exportGlobals": false,
"exportLocalsConvention": "camelCase",
"namedExport": false,
},
"dataUriLimit": {
"font": 4096,
"image": 4096,
"media": 4096,
"svg": 4096,
},
"distPath": {
"css": "static/css",
"font": "static/font",
"html": "/",
"image": "static/image",
"js": "static/js",
"media": "static/media",
"root": "dist",
"svg": "static/svg",
"wasm": "static/wasm",
},
"emitAssets": true,
"filename": {},
"filenameHash": true,
"injectStyles": false,
"inlineScripts": false,
"inlineStyles": false,
"legalComments": "linked",
"manifest": false,
"minify": true,
"polyfill": "usage",
"sourceMap": {
"css": false,
"js": undefined,
},
"target": "web",
},
"performance": {
"buildCache": true,
"chunkSplit": {
"strategy": "split-by-experience",
},
"printFileSize": true,
"profile": false,
"removeConsole": false,
"removeMomentLocale": false,
},
"security": {
"nonce": "",
"sri": {
"enable": false,
},
},
"server": {
"compress": true,
"host": "0.0.0.0",
"htmlFallback": "index",
"open": false,
"port": 3000,
"printUrls": true,
"strictPort": false,
},
"source": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
"define": {},
"entry": {
"index": "src/index.js",
},
"preEntry": [],
"tsconfigPath": "<ROOT>/packages/compat/webpack/tests/tsconfig.json",
},
"tools": {
"cssExtract": {
"loaderOptions": {},
"pluginOptions": {
"ignoreOrder": true,
},
},
},
},
"target": "web",
"tsconfigPath": "<ROOT>/packages/compat/webpack/tests/tsconfig.json",
},
"modifyTagsFn": [Function],
"name": "HtmlBasicPlugin",
"options": {
Expand Down Expand Up @@ -761,7 +899,142 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
"version": 5,
},
HtmlBasicPlugin {
"environment": "web",
"environment": {
"browserslist": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
"distPath": "<ROOT>/packages/compat/webpack/tests/dist",
"entry": {
"index": "src/index.js",
},
"htmlPaths": {
"index": "index.html",
},
"index": 0,
"name": "web",
"normalizedConfig": {
"dev": {
"assetPrefix": "/",
"client": {
"host": "",
"overlay": true,
"path": "/rsbuild-hmr",
"port": "",
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
"html": {
"crossorigin": false,
"inject": "head",
"meta": {
"charset": {
"charset": "UTF-8",
},
"viewport": "width=device-width, initial-scale=1.0",
},
"mountId": "root",
"outputStructure": "flat",
"scriptLoading": "defer",
"title": "Rsbuild App",
},
"output": {
"assetPrefix": "/",
"charset": "ascii",
"cssModules": {
"auto": true,
"exportGlobals": false,
"exportLocalsConvention": "camelCase",
"namedExport": false,
},
"dataUriLimit": {
"font": 4096,
"image": 4096,
"media": 4096,
"svg": 4096,
},
"distPath": {
"css": "static/css",
"font": "static/font",
"html": "/",
"image": "static/image",
"js": "static/js",
"media": "static/media",
"root": "dist",
"svg": "static/svg",
"wasm": "static/wasm",
},
"emitAssets": true,
"filename": {},
"filenameHash": true,
"injectStyles": false,
"inlineScripts": false,
"inlineStyles": false,
"legalComments": "linked",
"manifest": false,
"minify": true,
"polyfill": "usage",
"sourceMap": {
"css": false,
"js": undefined,
},
"target": "web",
},
"performance": {
"buildCache": true,
"chunkSplit": {
"strategy": "split-by-experience",
},
"printFileSize": true,
"profile": false,
"removeConsole": false,
"removeMomentLocale": false,
},
"security": {
"nonce": "",
"sri": {
"enable": false,
},
},
"server": {
"compress": true,
"host": "0.0.0.0",
"htmlFallback": "index",
"open": false,
"port": 3000,
"printUrls": true,
"strictPort": false,
},
"source": {
"alias": {},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
"version": "2022-03",
},
"define": {},
"entry": {
"index": "src/index.js",
},
"preEntry": [],
"tsconfigPath": "<ROOT>/packages/compat/webpack/tests/tsconfig.json",
},
"tools": {
"cssExtract": {
"loaderOptions": {},
"pluginOptions": {
"ignoreOrder": true,
},
},
},
},
"target": "web",
"tsconfigPath": "<ROOT>/packages/compat/webpack/tests/tsconfig.json",
},
"modifyTagsFn": [Function],
"name": "HtmlBasicPlugin",
"options": {
Expand Down
Loading
Loading