Skip to content

Commit

Permalink
fix: watch close (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio authored Jun 4, 2024
1 parent 7eb0e79 commit dcc09bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
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 });
}
});

0 comments on commit dcc09bb

Please sign in to comment.