【问题标题】:Why am I getting 2 builds with Webpack?为什么我会使用 Webpack 构建 2 个版本?
【发布时间】:2016-11-27 12:29:43
【问题描述】:

我需要知道构建的哈希,所以我试图在回调函数中捕获它:

const compiler = webpack(webpackConfig, function (err, stats) {
  debug("Hash", stats.hash)
})

但是当我这样做时,会发生一些奇怪的事情:我得到 2 个构建并且只能捕获 1 个哈希 - 显然我需要另一个而不是回调中的那个。

有回调时的输出:

app:server Hash +7s ea34c7a4d34baae1c353
webpack built d796ba7633cf5b1023a0 in 6673ms
Hash: ea34c7a4d34baae1c353
Version: webpack 1.13.1
Time: 6649ms
Asset       Size  Chunks             Chunk Names
app.ea34c7a4d34baae1c353.js    1.27 MB       0  [emitted]  app
1.counter.ea34c7a4d34baae1c353.js     247 kB       1  [emitted]  counter
vendor.ea34c7a4d34baae1c353.js     389 kB       2  [emitted]  vendor
app.ea34c7a4d34baae1c353.js.map    1.54 MB       0  [emitted]  app
1.counter.ea34c7a4d34baae1c353.js.map     303 kB       1  [emitted]  counter
vendor.ea34c7a4d34baae1c353.js.map     466 kB       2  [emitted]  vendor
favicon.ico    24.8 kB          [emitted]
index.html  595 bytes          [emitted]
Child html-webpack-plugin for "index.html":
Asset    Size  Chunks       Chunk Names
index.html  553 kB       0
webpack: bundle is now VALID.
  Hash: d796ba7633cf5b1023a0
Version: webpack 1.13.1
Time: 6673ms
Asset       Size  Chunks             Chunk Names
app.d796ba7633cf5b1023a0.js    1.17 MB       0  [emitted]  app
vendor.d796ba7633cf5b1023a0.js     389 kB       1  [emitted]  vendor
app.d796ba7633cf5b1023a0.js.map    1.42 MB       0  [emitted]  app
vendor.d796ba7633cf5b1023a0.js.map     466 kB       1  [emitted]  vendor
favicon.ico    24.8 kB          [emitted]
index.html  595 bytes          [emitted]
Child html-webpack-plugin for "index.html":
Asset    Size  Chunks       Chunk Names
index.html  553 kB       0
webpack: bundle is now VALID.

无回调时输出:

Hash: c5e31d1eb986b7ef318d
Version: webpack 1.13.1
Time: 3891ms
Asset       Size  Chunks             Chunk Names
app.c5e31d1eb986b7ef318d.js    1.26 MB       0  [emitted]  app
1.counter.c5e31d1eb986b7ef318d.js    30.6 kB       1  [emitted]  counter
vendor.c5e31d1eb986b7ef318d.js     389 kB       2  [emitted]  vendor
app.c5e31d1eb986b7ef318d.js.map    1.54 MB       0  [emitted]  app
1.counter.c5e31d1eb986b7ef318d.js.map    37.9 kB       1  [emitted]  counter
vendor.c5e31d1eb986b7ef318d.js.map     466 kB       2  [emitted]  vendor
favicon.ico    24.8 kB          [emitted]
index.html  595 bytes          [emitted]
Child html-webpack-plugin for "index.html":
Asset    Size  Chunks       Chunk Names
index.html  553 kB       0
webpack: bundle is now VALID.

文件:https://github.com/davezuko/react-redux-starter-kit/blob/master/server/main.js

【问题讨论】:

    标签: javascript build webpack webpack-dev-middleware


    【解决方案1】:

    好的,我发现...在 Webpack 的源代码中,如果有一个回调给 webpack(options, callback),那么它会自动运行:

    function webpack(options, callback) {
        ...
        if(callback) {
          ...
          compiler.run(callback);
        }
        return compiler;
    }
    

    如果有人想添加回调,请添加plugin,如下所示:

    compiler.plugin("done", stats => {
      debug(hash)
    })
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多