Is there some way to trigger a webpack build without using the command line webpack build task in a node.js script? I know I can use the exec function to execute a command line task, but I'm looking for a way that doesn't involve the command line at all.
I'm looking for something that works like
import webpack from "webpack";
await webpack.build(...);
CodePudding user response:
You can use the webpack Node API (https://webpack.js.org/api/node/).
Create a webpack instance, as specified in the docs, like so:
import webpack from 'webpack';
const compiler = webpack({}, (err, stats) => {
if (err || stats.hasErrors()) {
// ...
}
// Done processing
});
Then, use the run method on compiler to, as the docs say, "kickstart all compilation work".
compiler.run(() => console.log('Done'));
