Run Multiple Webpack Configs Sequentially
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的更多相关文章
- 【转帖】如何在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 ...
- 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- ...
- 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 ...
- [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 ...
- React 和 ES6 工作流之 Webpack的使用(第六部分)
这是React和ECMAScript2015系列文章的最后一篇,我们将继续探索React 和 Webpack的使用. 下面是所有系列文章章节的链接: React . ES6 - 介绍(第一部分) Re ...
- webpack+react+antd 单页面应用实例
React框架已经火了好长一段时间了,再不学就out了! 对React还没有了解的同学可以看看我之前的一篇文章,可以快速简单的认识一下React.React入门最好的实例-TodoList 自己从开始 ...
- gulp + webpack + sass 学习
笔记: new webpack.optimize.CommonsChunkPlugin 核心作用是抽离公共代码,chunks:['index.js','main.js'] 另一个作用就是单独生成一个j ...
- webpack配置备份
package.json: { "name": "webpackTest", "version": "1.0.0", & ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
随机推荐
- LeetCode:第K个排列【60】
LeetCode:第K个排列[60] 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: &quo ...
- 使用 Node.js 写一个代码生成器
背景 第一次接触代码生成器用的是动软代码生成器,数据库设计好之后,一键生成后端 curd代码.之后也用过 CodeSmith , T4.目前市面上也有很多优秀的代码生成器,而且大部分都提供可视化界面操 ...
- vscode failed to excute git
将代码提交到github时 提示 “failed to excute git” 解决:配置git git config --global user.email "youremailid@ex ...
- /x86_64-linux-gnu/libSM.so: undefined reference to `uuid_generate@UUID_1.0'错误
在编译PCL的时候总是报错,其他人都没问题 后来发现是我cmakePCL的时候,QT引用的是anaconda里的qt,把这个一改果然没问题了,耽误了一天时间. 感谢stack上这位老铁
- MapReduce面试题
什么是mapreduce Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架.容错高,扩展好,适合pB数据处理 MapReduce 执行过程分析 ...
- ASP.NET MVC请求参数字符串之区分空与NULL
开发中经常会写增删改查的功能,这里记录下在更新操作时遇到的一个问题. 假设一个模型对应数据库中某一张表,在更新时便需要区分是一次性更新全部字段还是仅更新部分字段.希望能做到传递某个参数时便更新,未传递 ...
- 数据结构与算法(Python)
数据结构与算法(Python) Why? 我们举一个可能不太恰当的例子: 如果将最终写好运行的程序比作战场,我们码农便是指挥作战的将军,而我们所写的代码便是士兵和武器. 那么数据结构和算法是什么?答曰 ...
- spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件
spring cloud 读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String ...
- 玩转Spring全家桶笔记 02 那些好用的连接池HikariCP
1.前言 简单了解一下来自日本的一个新起之秀连接池---HikariCP github:https://github.com/brettwooldridge/HikariCP 2.HikariCP为什 ...
- docker 入坑2
上一节我们安装好了docker,那么这节我们讲一下docker基本命令使用 查看版本 $ sudo docker --version 返回:Docker version 18.09.0, build ...