Skip to content

Commit

Permalink
fix: compatible with webpack-dev-server v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Aug 27, 2021
1 parent bcaf487 commit 516cefa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/build-scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1

- [fix] compatible with webpack-dev-server v3

## 1.1.0

- [refactor] support custom command by extend Context
Expand Down
8 changes: 6 additions & 2 deletions packages/build-scripts/bin/child-process-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ const defaultPort = parseInt(DEFAULT_PORT || 3333, 10);
});

['SIGINT', 'SIGTERM'].forEach(function(sig) {
process.on(sig, function() {
devServer.close();
process.on(sig, async function() {
if (devServer.close) {
devServer.close();
} else if (devServer.stop) {
await devServer.stop();
}
process.exit(0);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/build-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-scripts",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"description": "scripts core",
"main": "lib/index.js",
Expand Down
37 changes: 30 additions & 7 deletions packages/build-scripts/src/service/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,50 @@ export = async function(context: Context, options?: IRunOptions): Promise<void |
stats,
});
});

let devServer: WebpackDevServer;
// require webpack-dev-server after context setup
// context may hijack webpack resolve
// eslint-disable-next-line @typescript-eslint/no-var-requires
const DevServer = require('webpack-dev-server');
const devServer: WebpackDevServer = new DevServer(devServerConfig, compiler);

// static method getFreePort in v4
if (DevServer.getFreePort) {
devServer = new DevServer(devServerConfig, compiler);
} else {
devServer = new DevServer(compiler, devServerConfig);
}

await applyHook(`before.${command}.devServer`, {
url: serverUrl,
urls,
devServer,
});

devServer.startCallback(
() => {
applyHook(`after.${command}.devServer`, {
if (devServer.startCallback) {
devServer.startCallback(
() => {
applyHook(`after.${command}.devServer`, {
url: serverUrl,
urls,
devServer,
});
},
);
} else {
devServer.listen(devServerConfig.port, devServerConfig.host, async (err: Error) => {
if (err) {
log.info('WEBPACK',chalk.red('[ERR]: Failed to start webpack dev server'));
log.error('WEBPACK', (err.stack || err.toString()));
}
await applyHook(`after.${command}.devServer`, {
url: serverUrl,
urls,
devServer,
err,
});
},
);
});
}


return devServer;
};

0 comments on commit 516cefa

Please sign in to comment.