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

[Bug]: afterBuild is not running when rebuild with watch option #5704

Open
chenyuncai opened this issue Apr 30, 2024 · 3 comments
Open

[Bug]: afterBuild is not running when rebuild with watch option #5704

chenyuncai opened this issue Apr 30, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@chenyuncai
Copy link

Version

Progress: resolved 1, reused 0, downloaded 1, added 1, done

  System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
    Memory: 1.98 GB / 13.87 GB
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @modern-js/eslint-config: 2.49.2 => 2.49.2
    @modern-js/module-tools: 2.49.2 => 2.49.2
    @modern-js/plugin-rspress: 1.19.1 => 1.19.1

Details

Begin with the modern npm package development guide and try to add a plugin to run a task after build everytime, but just trigger the plugin first time built

pnpm run build --watch

Reproduce link

none, just every clean default code

Reproduce Steps

  1. init ptoject
  2. add a plugin with afterBuild hook and console.log('here i am running')
  3. pnpm run build --watch

expect run everytime when i change the src/index.ts code like hello world 2

@chenyuncai chenyuncai added the bug Something isn't working label Apr 30, 2024
@Timeless0911
Copy link
Collaborator

When in watch mode, the dts build sub task is implemented by running tsc --watch in a sub process, the watch command doesn't have a terminated state, so we can not get when this sub task ended. Therefore, afterBuild task will not be executed when in watch mode with dts build sub task.

You can do something in afterBuildTask hook instead, and use options.config.format to judge whether it's esm build sub task or cjs.

@FeSeason
Copy link

+1

@Timeless0911
Copy link
Collaborator

Timeless0911 commented May 23, 2024

rebuild has other hooks which not be written in docs, you can use buildWatchJs hook when before rebuild

code logic is as below:

export const buildWatchHooks = {
buildWatchJs: createParallelWorkflow<WatchJsHookContext, void>(),
buildWatchDts: createParallelWorkflow<WatchDtsHookContext, void>(),
};

const handleChange = async (filePath: string, _events: Stats) => {
const {
context: { root },
watchedFiles,
} = compiler;
if (watchedFiles.has(path.resolve(root, filePath))) {
logger.info(`File changed: ${chalk.dim(filePath)}`);
const runner = api.useHookRunners();
runner.buildWatchJs({ buildConfig: config });
await compiler.reBuild('change', config);
}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants