Skip to content

Commit

Permalink
feat: add rsbuild hook test case
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Feb 18, 2025
1 parent 3a9801f commit 3e008eb
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
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"]
}

0 comments on commit 3e008eb

Please sign in to comment.