简体   繁体   English

在 Node.js 脚本中触发 webpack 无命令行构建

[英]Trigger webpack build WITHOUT command line in Node.js script

Is there some way to trigger a webpack build without using the command line webpack build task in a node.js script?有什么方法可以触发 webpack 构建,而无需在 node.js 脚本中使用命令行webpack build任务? 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.我知道我可以使用exec function 来执行命令行任务,但我正在寻找一种完全不涉及命令行的方法。

I'm looking for something that works like我正在寻找类似的东西

import webpack from "webpack";
await webpack.build(...);

You can use the webpack Node API ( https://webpack.js.org/api/node/ ).您可以使用 webpack 节点 API ( https://webpack.js.org/api/node/ )。

Create a webpack instance, as specified in the docs, like so:按照文档中的说明创建一个webpack实例,如下所示:

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方法,正如文档所说,“启动所有编译工作”。

compiler.run(() => console.log('Done'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM