This repository was archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebpack.config.js
More file actions
162 lines (153 loc) · 4.74 KB
/
webpack.config.js
File metadata and controls
162 lines (153 loc) · 4.74 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
'use strict';
const path = require('path');
const {LoaderOptionsPlugin, DefinePlugin, optimize} = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkManifestHtmlWebpackPlugin = require('inline-chunk-manifest-html-webpack-plugin');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin-loader');
const PreloadWebpackPlugin = require('preload-webpack-plugin');
const dotenvSafe = require('dotenv-safe').load();
const pkg = require('./package.json');
module.exports = ({production = false, ssr = false, lite = false} = {}) => {
process.env.NODE_ENV = production ? 'production' : 'development';
// output filenames for main and chunks
const output = {
path: path.resolve(__dirname, './build'),
filename: production ? '[name].[chunkhash].js' : '[name].js',
chunkFilename: production ? '[name].[chunkhash].js' : '[name].js'
};
// source map config
const sourceMap = production ? 'cheap-module-source-map' : 'source-map';
// firebase configs
const firebaseConfig = JSON.stringify({
apiKey: process.env.FIREBASE_API_KEY,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
databaseURL: process.env.FIREBASE_DATABASE_URL
});
// redirect the request of importing react to react-lite
const resolve = {
alias: lite ? {
'react': 'react-lite',
'react-dom': 'react-lite'
} : {}
};
// webpack default configs
const webpackConfig = {
entry: {
main: ['./src/main.js'],
vendor: (lite ? [] : ['./src/stdlib.js']).concat(['react', 'react-dom'])
},
output,
module: {
loaders: [{
test: /\.(js|jsx)$/,
include: path.resolve(__dirname, './src'),
loaders: 'babel-loader'
}]
},
devtool: sourceMap,
resolve,
plugins: [
new optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new optimize.CommonsChunkPlugin({
children: true,
async: 'common',
minChunks: 2
}),
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
firebaseConfig: firebaseConfig
}),
new HtmlWebpackPlugin(Object.assign({
filename: `index.${ssr ? 'ejs' : 'html'}`,
template: './src/views/index.ejs',
favicon: './public/favicon.ico',
markup: `<div id="app">${ssr ? '<%- markup %>' : ''}</div>`
}, production ? {
collapseWhitespace: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
} : {})),
new InlineChunkManifestHtmlWebpackPlugin({
filename: "chunk-manifest.json",
chunkManifestVariable: 'webpackChunkManifest',
dropAsset: true
}),
new PreloadWebpackPlugin({
include: ['common', 'greeting']
}),
new PreloadWebpackPlugin({
rel: 'prefetch',
include: ['users', 'notification']
}),
new CopyWebpackPlugin([{
context: './public',
from: '*.*'
}, {
from: './src/firebase-messaging-sw.js',
to: 'firebase-messaging-sw.js',
transform: c => {
return c.toString().replace(/firebaseConfig/, firebaseConfig)
}
}]),
new SWPrecacheWebpackPlugin({
cacheId: `${pkg.name}-${pkg.version}`,
staticFileGlobs: [
path.join(output.path, '**/*')
],
runtimeCaching: [{
urlPattern: /https:\/\/.+.firebaseio.com/,
handler: 'networkFirst'
}],
logger: function () {},
filename: 'sw.js',
minify: production
})
],
devServer: {
contentBase: './public',
inline: true,
host: 'localhost',
port: 8080
}
};
// webpack additional build for production ready version
if (production) {
webpackConfig.plugins = webpackConfig.plugins.concat([
// https://webpack.js.org/plugins/uglifyjs-webpack-plugin/
new optimize.UglifyJsPlugin({
sourceMap,
mangle: true,
beautify: false,
comments: false,
// http://lisperator.net/uglifyjs/compress
compress: {
unused: true,
dead_code: true,
warnings: false,
drop_debugger: true,
conditionals: true,
evaluate: true,
drop_console: true,
sequences: true,
booleans: true,
},
extractComments: true
}),
new LoaderOptionsPlugin({
minimize: true,
debug: false
})
]);
}
return webpackConfig;
};