简体   繁体   English

异步webpack配置

[英]Async webpack config

I can't get this to work. 我不能让这个工作。 Please help :) 请帮忙 :)

(The documentation says webpack can handle Promises) 文档说webpack可以处理Promises)

This works: 这有效:

var compiler = webpack(webpackConfig)

But with a promise I get errors 但有一个承诺我会得到错误

var compiler = webpack(new Promise(function(resolve, reject) {
  setTimeout(() => { resolve(webpackConfig) }, 100);
}));

The error I get is: 我得到的错误是:

C:\path\node_modules\webpack\lib\webpack.js:19
                throw new 
WebpackOptionsValidationError(webpackOptionsValidationErrors);
                ^

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration misses the property 'entry'.
   object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function
   The entry point(s) of the compilation.
    at webpack (C:\path\node_modules\webpack\lib\webpack.js:19:9)
    at Object.<anonymous> (C:\path\build\dev-server.js:27:16)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What am I doing wrong? 我究竟做错了什么?

It's the webpack CLI that handles the Promise configuration. 它是处理Promise配置的webpack CLI。 If you're using the Node API you need to handle it yourself. 如果您使用的是Node API,则需要自己处理。

In your example this would be: 在您的示例中,这将是:

const configPromise = new Promise(function(resolve, reject) {
  setTimeout(() => { resolve(webpackConfig) }, 1000);
});

configPromise
  .then(webpack) // Passes the config to webpack
  .then(compiler => {
    // Do the work with the compiler
  });

The idea of this is to be able to do the asynchronous processing in the config without a hassle, that problem doesn't exist when using the Node API in the first place. 这样做的想法是能够在没有麻烦的情况下在配置中进行异步处理,首先使用Node API时该问题不存在。

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

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