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

fix: should not trigger dev hooks when building #4165

Merged
merged 1 commit into from
Dec 10, 2024
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
33 changes: 33 additions & 0 deletions e2e/cases/plugin-api/plugin-hooks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,39 @@ rspackOnlyTest(
},
);

rspackOnlyTest(
'should run plugin hooks correctly when running build and mode is development',
async () => {
const { plugin, names } = createPlugin();
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
mode: 'development',
plugins: [plugin],
},
});

const buildInstance = await rsbuild.build();

await buildInstance.close();

expect(names).toEqual([
'ModifyRsbuildConfig',
'ModifyEnvironmentConfig',
'ModifyBundlerChain',
'ModifyBundlerConfig',
'BeforeCreateCompiler',
'AfterCreateCompiler',
'BeforeBuild',
'BeforeEnvironmentCompile',
'ModifyHTMLTags',
'AfterEnvironmentCompile',
'AfterBuild',
'OnCloseBuild',
]);
},
);

rspackOnlyTest(
'should run plugin hooks correctly when running startDevServer',
async ({ page }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/webpack/src/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function createCompiler(options: InitConfigsOptions) {
done(stats as Rspack.Stats);
});

if (context.normalizedConfig?.mode === 'development') {
if (context.command === 'dev') {
helpers.registerDevHook({
compiler,
context,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export async function createRsbuild(
});

const preview = async (options: PreviewOptions = {}) => {
context.command = 'preview';

if (!getNodeEnv()) {
setNodeEnv('production');
}
Expand Down Expand Up @@ -174,9 +176,12 @@ export async function createRsbuild(
};

const build: Build = async (...args) => {
context.command = 'build';

if (!getNodeEnv()) {
setNodeEnv('production');
}

const buildInstance = await providerInstance.build(...args);
return {
...buildInstance,
Expand All @@ -188,16 +193,22 @@ export async function createRsbuild(
};

const startDevServer: StartDevServer = (...args) => {
context.command = 'dev';

if (!getNodeEnv()) {
setNodeEnv('development');
}

return providerInstance.startDevServer(...args);
};

const createDevServer: CreateDevServer = (...args) => {
context.command = 'dev';

if (!getNodeEnv()) {
setNodeEnv('development');
}

return providerInstance.createDevServer(...args);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/provider/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
isCompiling = true;
});

if (context.normalizedConfig?.mode === 'production') {
if (context.command === 'build') {
compiler.hooks.run.tap('rsbuild:run', logRspackVersion);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
},
);

if (context.normalizedConfig?.mode === 'development') {
if (context.command === 'dev') {
registerDevHook({
context,
compiler,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ export type InternalContext = RsbuildContext & {
environments: Record<string, EnvironmentContext>;
/** Only build specified environment. */
specifiedEnvironments?: string[];
/**
* The command type.
*
* - dev: `rsbuild dev` or `rsbuild.startDevServer()`
* - build: `rsbuild build` or `rsbuild.build()`
* - preview: `rsbuild preview` or `rsbuild.preview()`
*/
command?: 'dev' | 'build' | 'preview';
};
Loading