-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
173 lines (158 loc) · 4.64 KB
/
webpack.config.js
File metadata and controls
173 lines (158 loc) · 4.64 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const _ = require('lodash');
const VersioningPlugin = require('versioning-webpack-plugin')
const WebpackMd5Hash = require('webpack-md5-hash')
var PRODUCTION = process.env.NODE_ENV === 'production';
console.log(`FLAVOR ${process.env.FLAVOR}`);
let suffix = process.env.FLAVOR == 'dev' ? '.dev' : '';
let envFilePath = `.env${suffix}`;
console.log(`envFilePath ${envFilePath}`);
var dotenvConfig = require('dotenv').config(
{path: envFilePath}
).parsed;
console.log(dotenvConfig);
var plugins = [
new ExtractTextPlugin({ filename: 'css/[name].css' }),
new HtmlWebpackPlugin(
{
title: 'Custom template',
template: './src/index.ejs'
}
),
new webpack.DefinePlugin({
'process.env': _(process.env)
.pick(_.keys(dotenvConfig))
.mapValues((v) => (JSON.stringify(v)))
.value()
}),
// these are the default options
new VersioningPlugin({
cleanup: true, // should it remove old files?
basePath: './', // manifest.json base path
manifestFilename: 'manifest.json' // name of the manifest file
}),
new WebpackMd5Hash()
];
var productionPlugins = [
new UglifyJSPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
})
];
var devPlugins = [];
function styleLoader(loaders) {
if (PRODUCTION)
return ExtractTextPlugin.extract({ fallback: 'style-loader', use: loaders });
return [ 'style-loader', ...loaders ];
}
/**
*
* @returns {object}
*/
function postCSSLoader() {
return {
loader: "postcss-loader",
options: {
plugins: function () {
return [require("autoprefixer")];
}
}
}
}
const config = {
entry: "./src/index.js",
devServer: {
contentBase: './dist',
historyApiFallback: true
},
output: {
filename: '[name]_[chunkhash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
pathinfo: false
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: styleLoader(['css-loader', postCSSLoader()])
},
{
test: /\.less/,
exclude: /\.module\.less/,
use: styleLoader(['css-loader', postCSSLoader(), 'less-loader'])
},
{
test: /\.scss/,
exclude: /\.module\.scss/,
use: styleLoader(['css-loader', postCSSLoader(), 'sass-loader'])
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: "url-loader?limit=10000&minetype=application/font-woff&name=fonts/[name].[ext]"
},
{
test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: "file-loader?name=fonts/[name].[ext]"
},
{
test: /\.jpg|\.png|\.gif$/,
use: "file-loader?name=images/[name].[ext]"
},
{
test: /\.svg/,
use: "file-loader?name=svg/[name].[ext]!svgo-loader"
},
{
test: /\.yaml$/,
use: 'js-yaml-loader',
}
]
},
plugins: PRODUCTION
? plugins.concat(productionPlugins)
: plugins.concat(devPlugins),
optimization: {
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
chunks: 'initial',
minChunks: 2
}
}
},
}
};
module.exports = (env, argv) => {
let mode = argv.mode;
console.log(`mode is ${mode}`);
if(mode === undefined) {
console.log("setting mode to development ...");
mode = 'development';
}
config.mode = mode;
if (mode === 'development') {
config.devtool = 'source-map';
}
if (mode === 'production') {
//...
}
return config;
};