forked from ibi-group/trimet-mod-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
95 lines (92 loc) · 2.81 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const path = require('path')
const fs = require('fs-extra')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
/**
* Webpack can be passed a few environment variables to override the default
* files used to run this project. The environment variables are CUSTOM_CSS,
* HTML_FILE, YAML_CONFIG, and JS_CONFIG. They must each be passed in the
* format --env.*=/path/to/file. For example:
*
* yarn start --env.YAML_CONFIG=/absolute/path/to/config.yml
*/
module.exports = async env => {
// Gather the CSS, HTML, YAML, and JS override files.
const CUSTOM_CSS = env && env.CUSTOM_CSS || './lib/style.scss'
const HTML_FILE = env && env.HTML_FILE || 'lib/index.tpl.html'
const YAML_CONFIG = env && env.YAML_CONFIG || './config.yml'
// resolve the custom js file. If it is present, copy the file to a
// temporary folder within this project so that the file will be able to
// use the node_modules from this project
let customJsFile = './config.js'
if (env && env.JS_CONFIG) {
const splitPath = env.JS_CONFIG.split(path.sep)
customJsFile = `../tmp/${splitPath[splitPath.length - 1]}`
// copy location is relative to root, while js file for app is relative to lib
await fs.copy(env.JS_CONFIG, `./tmp/${splitPath[splitPath.length - 1]}`)
}
return {
devtool: 'source-map',
entry: [
'./lib/main.js',
CUSTOM_CSS
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.(yml|yaml)$/,
loader: ['json-loader', 'yaml-loader']
},
{
test: /\.(sc|c)ss$/,
use: [
'css-hot-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: path.join(__dirname, '/dist'),
publicPath: '',
filename: 'bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
template: HTML_FILE,
inject: 'body',
filename: 'index.html'
}),
new MiniCssExtractPlugin(),
new webpack.DefinePlugin({
// Optionally override the default config files with some other
// files.
YAML_CONFIG: JSON.stringify(YAML_CONFIG),
JS_CONFIG: JSON.stringify(customJsFile)
})
],
optimization: {
minimizer: [
new TerserPlugin(),
new OptimizeCSSAssetsPlugin({})
]
},
devServer: {
contentBase: './',
historyApiFallback: true
}
}
}