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

feat: support rsbuild modify config hook #6840

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/strange-rings-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': minor
---

feat: support rsbuild modify config hook

feat: 支持 rsbuild 修改配置的 Hook 函数
2 changes: 2 additions & 0 deletions packages/solutions/app-tools/src/builder/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ async function applyBuilderPlugins<B extends Bundler>(
builderPluginAdapterBasic,
builderPluginAdapterHtml,
builderPluginAdapterSSR,
builderPluginAdapterHooks,
} = await import('../shared/builderPlugins/index.js');

builder.addPlugins([
builderPluginAdapterBasic(),
builderPluginAdapterSSR(options),
builderPluginAdapterHtml(options),
builderPluginAdapterHooks(options),
]);

builder.addPlugins([builderPluginAdapterCopy(options)], {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { RsbuildPlugin } from '@rsbuild/core';
import type { Bundler } from '../../../types';
import type { BuilderOptions } from '../types';

export const builderPluginAdapterHooks = <B extends Bundler>(
options: BuilderOptions<B>,
): RsbuildPlugin => ({
name: 'builder-plugin-support-modern-hooks',
setup(api) {
const _internalContext = options.appContext._internalContext;
const hooks = _internalContext.pluginAPI?.getHooks();
api.modifyBundlerChain(async (chain, utils) => {
await hooks?.modifyBundlerChain.call(chain, utils);
});
api.modifyRsbuildConfig(async (config, utils) => {
await hooks?.modifyRsbuildConfig.call(config, utils);
});
api.modifyRspackConfig(async (config, utils) => {
await hooks?.modifyRspackConfig.call(config, utils);
});
api.modifyWebpackChain(async (chain, utils) => {
await hooks?.modifyWebpackChain.call(chain, utils);
});
api.modifyWebpackConfig(async (config, utils) => {
await hooks?.modifyWebpackConfig.call(config, utils);
});
},
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './adapterBasic';
export * from './adapterHtml';
export * from './adapterSSR';
export * from './builderHooks';
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions tests/integration/rsbuild-hook/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
type AppTools,
type CliPluginFuture,
appTools,
defineConfig,
} from '@modern-js/app-tools';

const MyPlugin = (): CliPluginFuture<AppTools> => ({
name: 'test',
setup(api) {
api.config(() => {
return {
tools: {
webpack: () => {
console.log('tools.webpack');
},
rspack: () => {
console.log('tools.rspack');
},
bundlerChain: () => {
console.log('tools.bundlerChain');
},
webpackChain: () => {
console.log('tools.webpackChain');
},
},
};
});
api.modifyBundlerChain(async (chain, utils) => {
console.log('modifyBundlerChain');
});
api.modifyRsbuildConfig(async (config, utils) => {
console.log('modifyRsbuildConfig');
});
api.modifyRspackConfig(async (config, utils) => {
console.log('modifyRspackConfig');
});
api.modifyWebpackChain(async (chain, utils) => {
console.log('modifyWebpackChain');
});
api.modifyWebpackConfig(async (config, utils) => {
console.log('modifyWebpackConfig');
});
},
});
export default defineConfig({
runtime: {
router: true,
},
plugins: [
appTools({
bundler: process.env.BUNDLER === 'webpack' ? 'webpack' : 'rspack',
}),
MyPlugin(),
],
});
14 changes: 14 additions & 0 deletions tests/integration/rsbuild-hook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"name": "app-rsbuild-hooks",
"version": "2.64.3",
"scripts": {
"dev": "modern dev"
},
"dependencies": {
"@modern-js/app-tools": "workspace:*",
"@modern-js/runtime": "workspace:*",
"react": "^18.3.1",
"react-dom": "^18.3.1"
}
}
28 changes: 28 additions & 0 deletions tests/integration/rsbuild-hook/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { RuntimeReactContext } from '@modern-js/runtime';
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
import { useContext } from 'react';

const App = () => {
const context = useContext(RuntimeReactContext);
const { initialData } = context;
return (
<BrowserRouter>
<Routes>
<Route index element={<div>index</div>} />
<Route
path="about"
element={<div>about {initialData?.data as string}</div>}
/>
</Routes>
</BrowserRouter>
);
};

App.init = () => {
console.log('init');
return {
data: 'init data',
};
};

export default App;
2 changes: 2 additions & 0 deletions tests/integration/rsbuild-hook/src/modern-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types='@modern-js/app-tools/types' />
/// <reference types='@modern-js/runtime/types' />
5 changes: 5 additions & 0 deletions tests/integration/rsbuild-hook/src/modern.runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineRuntimeConfig } from '@modern-js/runtime';

export default defineRuntimeConfig({
plugins: [],
});
74 changes: 74 additions & 0 deletions tests/integration/rsbuild-hook/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import path from 'path';
import { modernBuild } from '../../../utils/modernTestUtils';

const appDir = path.resolve(__dirname, '../');

function processStdout(stdout: string) {
const cleaned = stdout.replace(
// biome-ignore lint/suspicious/noControlCharactersInRegex: <explanation>
/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g,
'',
);
return cleaned.replace(/\r\n/g, '\n').trim();
}

function verifyOrder(output: string, expectedOrder: string[]): boolean {
let currentIndex = 0;
const lines = output.split('\n');

for (const line of lines) {
if (line.includes(expectedOrder[currentIndex])) {
currentIndex++;
}
if (currentIndex === expectedOrder.length) {
return true;
}
}

return false;
}
describe('build with rspack', () => {
test('rspack hooks', async () => {
const buildResult = await modernBuild(appDir, [], {
env: {
BUNDLER: 'rspack',
},
});

expect(buildResult.code).toEqual(0);

const cleanOutput = processStdout(buildResult.stdout);
const expectedOrder = [
'modifyBundlerChain',
'tools.bundlerChain',
'modifyRspackConfig',
'tools.rspack',
];

expect(verifyOrder(cleanOutput, expectedOrder)).toBeTruthy();
});
});

describe('build with webpack', () => {
test('webpack hooks', async () => {
const buildResult = await modernBuild(appDir, [], {
env: {
BUNDLER: 'webpack',
},
});

expect(buildResult.code).toEqual(0);

const cleanOutput = processStdout(buildResult.stdout);
const expectedOrder = [
'modifyBundlerChain',
'tools.bundlerChain',
'modifyWebpackChain',
'tools.webpackChain',
'modifyWebpackConfig',
'tools.webpack',
];

expect(verifyOrder(cleanOutput, expectedOrder)).toBeTruthy();
});
});
14 changes: 14 additions & 0 deletions tests/integration/rsbuild-hook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@modern-js/tsconfig/base",
"compilerOptions": {
"declaration": false,
"jsx": "preserve",
"baseUrl": "./",
"outDir": "dist",
"paths": {
"@/*": ["./src/*"],
"@shared/*": ["./shared/*"]
}
},
"include": ["src", "tests", "modern.config.ts"]
}
Loading