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: watch close #401

Merged
merged 5 commits into from
Jun 4, 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
5 changes: 5 additions & 0 deletions .changeset/clean-ravens-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"10up-toolkit": patch
---

Fix: watch close when using modules
30 changes: 21 additions & 9 deletions packages/toolkit/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ if (hasWebpackConfig()) {
configPath = fromProjectRoot('webpack.config.js');
}

let server;
let compiler;

const runWebpack = () => {
const config = require(configPath);
const compiler = webpack(config);
compiler = webpack(config);

const { devServer } = config;

if (devServer) {
const devServerOptions = { ...devServer, host: '127.0.0.1', open: false };
const server = new WebpackDevServer(devServerOptions, compiler);
server = new WebpackDevServer(devServerOptions, compiler);

server.start();
} else {
Expand All @@ -52,13 +55,6 @@ const runWebpack = () => {
const hot = hasArgInCLI('--hot');

if (hot) {
process.on('SIGINT', () => {
// when gracefully leaving hot mode, clean up dist folder.
// this avoids leaving js code with the fast refresh instrumentation and thus reducing confusion
console.log('\n10up-toolkit: Cleaning up dist folder...');

fs.rmSync(fromProjectRoot('dist'), { recursive: true, force: true });
});
// compile the fast refresh bundle
const config = require(fromConfigRoot('webpack-fast-refresh.config.js'));
const compiler = webpack(config);
Expand All @@ -78,3 +74,19 @@ if (hot) {
} else {
runWebpack();
}

process.on('SIGINT', () => {
if (server) {
server.close();
}

compiler.close();

if (hot) {
// when gracefully leaving hot mode, clean up dist folder.
// this avoids leaving js code with the fast refresh instrumentation and thus reducing confusion
console.log('\n10up-toolkit: Cleaning up dist folder...');

fs.rmSync(fromProjectRoot('dist'), { recursive: true, force: true });
}
});
Loading