Webpack: 插件的顺序重要吗?

我查看了文档 给你给你,搜索了 StackOverflow 和 GitHub 的讨论,仍然找不到这些信息。在 Webpack 是否有任何资源或任何人知道插件的顺序是否重要?订单是怎么来的?

23192 次浏览

Yes, the order matters: plugins are bound to the compiler and applied in the order specified. You can look into webpack/tapable for a clearer idea on how this works.

Usually, though, you are not forced to think about ordering when binding compiler and compilation plugins, as plugin authors expose specific events that help you reason when your handlers will be invoked.

webpack is not a task runner. These plugins are tasks, which is not "webpack-style" and not supported. You can report the issue there, but there is nothing to do on webpack side (and I don't care much). -sokra

ref

Well, the answer is yes and no.

No because during the bundling, webpack emits several events which are fired at different phases of the compilation (you can read more about it here).

Each plugin must be hooked (by the plugin's author) to one of these events.

That being said, let's consider two plugins, A and B.

Even if A is listed before B in the config file, A will be executed before B only if it is hooked to an event that is fired before B's hooked event.

Yes because in case A and B are hooked to the same event, the execution order should be consistent with the order in the config file.