https://www.viget.com/articles/run-multiple-webpack-configs-sequentially/

const path = require('path');
const serverConfig = {
entry: './src/b.ts'
} const config2 = {
entry: './src/index.ts',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
"devServer": {
"port": 8083
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
module.exports = [config2, serverConfig]

  

---------------------------------------------

When one webpack config depends on the outcome of another, running the configurations sequentially can save the day.

I have a webpack build process, and it is has two isolated configurations (one for my server-side build, and another for my client-side build). Here's an incredibly simplified example:

// webpack.config.js

const clientConfig = {
entry: './src/client/index.js',
} const serverConfig = {
entry: './src/server/index.js'
} module.exports = [clientConfig, serverConfig]

Everything clicked together quickly and easily, webpack is able to run these two builds and export different bundles for each environment. But then, of course, the joyous innocence of the early days faded as we added more functionality.

The Rub

We pulled in a library called React Loadable (side note: fantastic library), which comes with a webpack plugin, which can generate a file during the build. This file generation is defined in the client config, but application code within the server depends on that file. Because the file itself is a build artifact, we're not checking it into our source control, and thus the build must succeed without the up-front existence of this file.

// webpack.config.js
const { ReactLoadablePlugin } = require('react-loadable/webpack') const clientConfig = {
entry: './src/client/index.js',
plugins: [
// This plugin creates the react-loadable.json file
new ReactLoadablePlugin({
filename: path.resolve(__dirname, 'react-loadable.json')
}),
]
} const serverConfig = {
// This entry point makes use of the built react-loadable.json file
entry: './src/server/index.js'
} module.exports = [clientConfig, serverConfig]

This is where things stop working. I've deduced that the configurations are being built in parallel, because even though the client configuration successfully creates the new file (I can see it in the build logs, and I can see those logs before I see any mention of the server build), the server build fails with a "I can't find that file!" error. If the file exists before the build runs, then everything flows smoothly, so the question became how to make it work without the pre-existence of that artifact.

The Solution

The internet had a few things to say about this. One option is to break apart the build and manually build each configuration in isolation. This requires some fiddling with your CLI usage, but does ensure that one config completely finishes before starting the new one. I was tempted to find a more streamlined solution, one that would allow me to run webpack the way that you'd expect.

What I ended up with was an extension of WebpackBeforeBuildPlugin which simply polled for the existence of the required file.

// WaitPlugin.js
const WebpackBeforeBuildPlugin = require('before-build-webpack')
const fs = require('fs') class WaitPlugin extends WebpackBeforeBuildPlugin {
constructor(file, interval = 100, timeout = 10000) {
super(function(stats, callback) {
let start = Date.now() function poll() {
if (fs.existsSync(file)) {
callback()
} else if (Date.now() - start > timeout) {
throw Error("Maybe it just wasn't meant to be.")
} else {
setTimeout(poll, interval)
}
} poll()
})
}
} module.exports = WaitPlugin
// webpack.config.js
const { ReactLoadablePlugin } = require('react-loadable/webpack')
const WaitPlugin = require('./WaitPlugin') const clientConfig = {
entry: './src/client/index.js',
plugins: [
new ReactLoadablePlugin({
filename: path.resolve(__dirname, 'react-loadable.json')
}),
]
} const serverConfig = {
entry: './src/server/index.js',
plugins: [
new WaitPlugin('react-loadable.json')
]
} module.exports = [clientConfig, serverConfig]

Now, the server config will hang while the client config wraps up. Checking every 100 milliseconds (for a maximum of 10 seconds), the build will only proceed when the file in question exists.

I admit that I was hoping to be able to more definably run the multiple configuration builds in sequence, but this has certainly done the trick. If you've found another solution to this problem, we'd love to hear about it in the comments below!

Run Multiple Webpack Configs Sequentially的更多相关文章

  1. 【转帖】如何在redhat单机服务器上运行postgresql的多个实例(howto run multiple postgresql instance on one redhat server)

    Running multiple PostgreSQL 9.2 Instances on one server in CentOS 6/RHEL 6/Fedora 原帖网站速度很慢,故转帖在此 Thi ...

  2. PHPFarm - How to run multiple versions of PHP on the same computer

    How to Run Multiple Versions of PHP on One Server 转载:http://www.sitepoint.com/run-multiple-versions- ...

  3. TestNG – Run multiple test classes (suite test)

    In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka sui ...

  4. [Webpack] Create Separate webpack Configs for Development and Production with webpack-merge

    The development and production modes in webpack optimize the output in different ways. In developmen ...

  5. React 和 ES6 工作流之 Webpack的使用(第六部分)

    这是React和ECMAScript2015系列文章的最后一篇,我们将继续探索React 和 Webpack的使用. 下面是所有系列文章章节的链接: React . ES6 - 介绍(第一部分) Re ...

  6. webpack+react+antd 单页面应用实例

    React框架已经火了好长一段时间了,再不学就out了! 对React还没有了解的同学可以看看我之前的一篇文章,可以快速简单的认识一下React.React入门最好的实例-TodoList 自己从开始 ...

  7. gulp + webpack + sass 学习

    笔记: new webpack.optimize.CommonsChunkPlugin 核心作用是抽离公共代码,chunks:['index.js','main.js'] 另一个作用就是单独生成一个j ...

  8. webpack配置备份

    package.json: { "name": "webpackTest", "version": "1.0.0", & ...

  9. [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...

随机推荐

  1. django:下拉框二级联动实现

    注意:只列举核心部分代码 前台模板: 第一级下拉菜单: <div class="col-sm-4"> <select data-placeholder=" ...

  2. IIS中发布FTP支持断点续传

    IIS10中发布FTP默认就是支持断点续传的.

  3. DB2执行计划分析

    多表连接的三种方式详解 hash join.merge join. nested loop 项目中的SQL执行效率太低,就用执行计划看一下执行SQL,看不懂,百度一下,纪录下来: 大多数人从来没有听说 ...

  4. Oracle Spatial图层元数据坐标范围影响R-TREE索引的ROOT MBR吗?

    Oracle Spatial的空间索引R-TREE,其实现原理为一级级的MBR(最小定界矩形).我突然想到一个问题,它的ROOT MBR是怎么确定的?是根据元数据表user_sdo_geom_meta ...

  5. C++中print和printf的区别

    print与printf的区别 1,print 中不能使用%s ,%d 或%c: 2,print 自动换行,printf 没有自动换行.

  6. spring_boot实战日记(二)logback的使用和配置

    日志:描述系统运行状态的所有信息都是日志. 日志能力: 1.定制输出目标. 2.定制输出格式. 3.携带上下文信息 4.运行时选择输出. 5.灵活的配置 日志选择: 日志门面:JCL(和Logback ...

  7. springboot统一返回json数据格式并配置系统异常拦截

    本文链接:https://blog.csdn.net/syystx/article/details/82870217通常进行前后端分离开发时我们需要定义统一的json数据交互格式并对系统未处理异常进行 ...

  8. ERP解析外围系统json数据格式

    外围系统调用ERP的WebService接口,将数据以json格式传到ERP,ERP解析json 1.创建java source jsp,提供java方法解析json数据 create or repl ...

  9. json转义问题

    后端程序接受前台传递过来json 1正常json没有问题 比如  {"id":21,"userName":"2张天师","phon ...

  10. 【LEETCODE】39、第561题 Array Partition I

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...