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 ...
随机推荐
- web基础---->session的使用
前几天在博问中,看到有人提到了有关session的问题,决定自己整理写一下有关session的原理!说起session,cookie必须是要谈的! 目录 Cookie的介绍 Cookie的使用 Ses ...
- SecureCRT 使用密钥登录 Ubuntu
记录 SecureCRT 通过 SSH 使用密钥登录 Ubuntu. 具体步骤如下: 1. 使用 SecureCRT 生成密钥对: 工具 -> 创建公钥 -> 密钥类型 RSA -> ...
- php_mvc实现步骤三,四
3.match_mvc MVC 以ecshop的前台为例: 功能一: 首页 购物车数据,商品分类数据,其他的首页需要的数据 功能二: 拍卖活动 购物车数据,商品分类数据,拍卖相关数据 功能三: 团购商 ...
- select2的使用
普通的select不支持搜索,当选项很多的时候,需要一个个下拉查找. 有了select2就方便多了 下载 <https://select2.org/> 引入 <link href=& ...
- Java开发笔记(一百一十七)AWT窗口
前面介绍的所有Java代码,都只能通过日志观察运行情况,就算编译成class文件,也必须在命令行下面运行,这样的程序无疑只能给开发者做调试用,不能拿给一般人使用.因为普通用户早已习惯在窗口界面上操作, ...
- 函数的练习2——python编程从入门到实践
8-9 魔术师:创建一个包含魔术师名字的列表,并将其传递一个名为show_magicians()的函数,这个函数打印列表中每个魔术师的名字. def show_magicians(magicians) ...
- Node模块化
Node.js是一个能够在服务器端运行JavaScript的开放源代码.跨平台JavaScript运行环境.Node是对ES标准一个实现,也是一个JS引擎.与传统服务器不同是Node的服务器是单线程的 ...
- Linux守护进程编写指南
Linux守护进程编写指南 守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种很有用的进 程.Linux的大多数服务器 ...
- aspnetcore 容器化部属到阿里云全过程记录
第一次写博客,作为一个全栈er,记录一下从阿里云到产品运维上线的全过程 一.阿里云上的设置 购买阿里云ECS后: 进控制台查看实例公网IP 在控制台.网络与安全->安全组,配置规则 点击进去可以 ...
- Go语言的成功也预示着Rust的成功【转】
从整体的角度来看Go,很难理解他是怎么取得这么大的成功的.从理论的角度上来说Go是一门非常糟糕的语言,就算是和C++或者Ada之类旧语言相比也是这样. 从整体的角度来看Go,很难理解他是怎么取得这么大 ...