Skip to content

Commit

Permalink
fix: should not trigger dev hooks when building
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 10, 2024
1 parent 1544c6d commit 1b9753d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
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';
};

0 comments on commit 1b9753d

Please sign in to comment.