Skip to content

Commit

Permalink
fix: merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Dec 6, 2024
2 parents d3d7106 + 35e45f8 commit 4d537df
Show file tree
Hide file tree
Showing 12 changed files with 1,640 additions and 150 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-socks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': patch
'@modern-js/uni-builder': patch
'@modern-js/utils': patch
---

feat(app-tools): enable Rsbuild CLI shortcuts
5 changes: 5 additions & 0 deletions .changeset/shiny-hairs-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-js/uni-builder': patch
---

feat(uni-builder): support environment tools.babel config
2 changes: 1 addition & 1 deletion packages/cli/uni-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@rsbuild/plugin-react": "1.0.7",
"@rsbuild/plugin-rem": "1.0.2",
"@rsbuild/plugin-sass": "1.1.1",
"@rsbuild/plugin-source-build": "1.0.1",
"@rsbuild/plugin-source-build": "1.0.2",
"@rsbuild/plugin-styled-components": "1.1.0",
"@rsbuild/plugin-svgr": "1.0.5",
"@rsbuild/plugin-toml": "1.0.1",
Expand Down
46 changes: 44 additions & 2 deletions packages/cli/uni-builder/src/rspack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import type {
RsbuildInstance,
RsbuildPlugin,
} from '@rsbuild/core';
import type { PluginBabelOptions } from '@rsbuild/plugin-babel';
import { compatLegacyPlugin } from '../shared/compatLegacyPlugin';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import { SERVICE_WORKER_ENVIRONMENT_NAME } from '../shared/utils';
import { SERVICE_WORKER_ENVIRONMENT_NAME, castArray } from '../shared/utils';
import type {
CreateBuilderCommonOptions,
CreateUniBuilderOptions,
Expand Down Expand Up @@ -58,7 +59,48 @@ export async function parseConfig(
);
}

if (uniBuilderConfig.tools?.babel) {
const hasEnvironmentBabelConfig = Object.values(
uniBuilderConfig.environments || {},
).some(c => c.tools?.babel !== undefined);

if (hasEnvironmentBabelConfig) {
const mergeSharedBabelConfig = (
config: PluginBabelOptions['babelLoaderOptions'],
) => {
if (uniBuilderConfig.tools?.babel) {
return castArray(config).concat(
...castArray(uniBuilderConfig.tools?.babel),
);
}
return config;
};

const { pluginBabel } = await import('@rsbuild/plugin-babel');
const { pluginBabelPost } = await import('./plugins/babel-post');
Object.entries(uniBuilderConfig.environments!).forEach(([name, config]) => {
const environmentConfig = rsbuildConfig.environments?.[name];
if (!environmentConfig) {
return;
}
if (config.tools?.babel) {
environmentConfig.plugins ??= [];
environmentConfig.plugins.push(
pluginBabel({
babelLoaderOptions: mergeSharedBabelConfig(config.tools?.babel),
}),
pluginBabelPost(),
);
} else if (uniBuilderConfig.tools?.babel) {
environmentConfig.plugins ??= [];
environmentConfig.plugins.push(
pluginBabel({
babelLoaderOptions: uniBuilderConfig.tools?.babel,
}),
pluginBabelPost(),
);
}
});
} else if (uniBuilderConfig.tools?.babel) {
const { pluginBabel } = await import('@rsbuild/plugin-babel');
const { pluginBabelPost } = await import('./plugins/babel-post');
rsbuildPlugins.push(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/uni-builder/src/shared/parseCommonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ export async function parseCommonConfig(
};
}

const { dev: RsbuildDev, server } = transformToRsbuildServerOptions(
const { dev: rsbuildDev, server } = transformToRsbuildServerOptions(
dev || {},
devServer || {},
);

rsbuildConfig.server = removeUndefinedKey(server);

rsbuildConfig.dev = removeUndefinedKey(RsbuildDev);
rsbuildConfig.dev = removeUndefinedKey(rsbuildDev);
rsbuildConfig.html = html;
rsbuildConfig.output = output;

Expand Down
12 changes: 11 additions & 1 deletion packages/cli/uni-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ConfigChainWithContext,
DevConfig,
DistPathConfig,
EnvironmentConfig,
HtmlConfig,
HtmlTagDescriptor,
OutputConfig,
Expand Down Expand Up @@ -452,5 +453,14 @@ export type UniBuilderConfig = {
source?: Omit<SourceConfig, 'alias' | 'transformImport'>;
// plugins is a new field, should avoid adding modern plugin by mistake
plugins?: RsbuildPlugins;
environments?: RsbuildConfig['environments'];
environments?: {
[key: string]: EnvironmentConfig & {
tools?: {
/**
* Modify the options of [babel-loader](https://github.com/babel/babel-loader)
*/
babel?: PluginBabelOptions['babelLoaderOptions'];
};
};
};
} & UniBuilderExtraConfig;
77 changes: 66 additions & 11 deletions packages/cli/uni-builder/src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
type RsbuildPlugin,
createRsbuild,
} from '@rsbuild/core';
import type { PluginBabelOptions } from '@rsbuild/plugin-babel';
import { compatLegacyPlugin } from '../shared/compatLegacyPlugin';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import { pluginPostcss } from '../shared/plugins/postcss';
import { SERVICE_WORKER_ENVIRONMENT_NAME } from '../shared/utils';
import { SERVICE_WORKER_ENVIRONMENT_NAME, castArray } from '../shared/utils';
import type {
CreateBuilderCommonOptions,
CreateUniBuilderOptions,
Expand Down Expand Up @@ -36,16 +37,70 @@ export async function parseConfig(
}),
);

rsbuildPlugins.push(
pluginBabel(
{
babelLoaderOptions: uniBuilderConfig.tools?.babel,
},
{
transformLodash: uniBuilderConfig.performance?.transformLodash ?? true,
},
),
);
const hasEnvironmentBabelConfig = Object.values(
uniBuilderConfig.environments || {},
).some(c => c.tools?.babel !== undefined);

// Add babel plugin into each environment separately
if (hasEnvironmentBabelConfig) {
const mergeSharedBabelConfig = (
config: PluginBabelOptions['babelLoaderOptions'],
) => {
if (uniBuilderConfig.tools?.babel) {
return castArray(config).concat(
...castArray(uniBuilderConfig.tools?.babel),
);
}
return config;
};

Object.entries(uniBuilderConfig.environments!).forEach(([name, config]) => {
const environmentConfig = rsbuildConfig.environments?.[name];
if (!environmentConfig) {
return;
}
environmentConfig.plugins ??= [];

if (config.tools?.babel) {
environmentConfig.plugins.push(
pluginBabel(
{
babelLoaderOptions: mergeSharedBabelConfig(config.tools?.babel),
},
{
transformLodash:
uniBuilderConfig.performance?.transformLodash ?? true,
},
),
);
} else {
environmentConfig.plugins.push(
pluginBabel(
{
babelLoaderOptions: uniBuilderConfig.tools?.babel,
},
{
transformLodash:
uniBuilderConfig.performance?.transformLodash ?? true,
},
),
);
}
});
} else {
rsbuildPlugins.push(
pluginBabel(
{
babelLoaderOptions: uniBuilderConfig.tools?.babel,
},
{
transformLodash:
uniBuilderConfig.performance?.transformLodash ?? true,
},
),
);
}

rsbuildPlugins.push(pluginReact());

if (uniBuilderConfig.tools?.tsLoader) {
Expand Down
Loading

0 comments on commit 4d537df

Please sign in to comment.