forked from scottohara/loot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
236 lines (211 loc) · 5.27 KB
/
webpack.common.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const path = require("path"),
webpack = require("webpack"),
ExtractTextPlugin = require("extract-text-webpack-plugin"),
CleanWebpackPlugin = require("clean-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
CopyWebpackPlugin = require("copy-webpack-plugin"),
WorkboxWebpackPlugin = require("workbox-webpack-plugin"),
packageJson = require("./package"),
// Default entry
entry = {
app: "loot",
vendor: [
"jquery",
"bootstrap",
"angular",
"@uirouter/angularjs",
"angular-ui-bootstrap",
"date-fns/esm"
]
},
// Default output
output = {
path: path.resolve(__dirname, "public")
},
// Rule for font processing
fontRule = {
test: /\.(ttf|woff|woff2|eot|svg)$/,
loader: "url-loader",
options: {
// Use file-loader for anything bigger than 1 byte
limit: 1,
// Include a hash in the file name
name: "fonts/[name]-[hash:6].[ext]"
}
},
// Rule for *.ico processing
iconRule = {
test: /\.ico$/,
loader: "url-loader",
options: {
// Use file-loader for anything bigger than 1 byte
limit: 1,
// Include a hash in the file name
name: "[name]-[hash:6].[ext]"
}
},
// Rule for *.html processing
htmlRule = {
test: /\.html$/,
include: /views/,
use: [
{
loader: "ngtemplate-loader",
options: {
// Strip path prefixes up to (and including) 'src/'
relativeTo: "src/"
}
},
{
loader: "html-loader",
options: {
attrs: [":typeahead-template-url"]
}
}
]
},
// Cleans the build directory
cleanBuildDirectory = new CleanWebpackPlugin(["./public"]),
/*
* Exposes a global jQuery object (Bootstrap expects this global to exist)
* window.jQuery is needed to prevent Angular from using jqLite
*/
providejQuery = new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
}),
/*
* Ensures all vendor dependencies are bundled separately to app code, and that the webpack manifest is kept
* separate so that changes to app code don't change the hash of the vendor chunk (or vice versa)
*/
separateBundles = new webpack.optimize.CommonsChunkPlugin({
names: [
"vendor",
"manifest"
],
minChunks: Infinity
}),
// Creates index.html with the bundled resources
createIndexHtml = new HtmlWebpackPlugin({template: "./src/index.html"}),
// Copies static resources to the build directory
copyStaticAssets = new CopyWebpackPlugin([
"*.html",
"robots.txt"
], {
context: "./src",
ignore: ["index.html"]
}),
// Default config
config = {
// Ensure that the context is the directory where the webpack.*.js config file is
context: path.resolve(__dirname),
// Default rules for all environments
module: {
rules: [htmlRule]
},
// Default resolve paths
resolve: {
modules: [
path.resolve(__dirname, "src"),
path.resolve(__dirname, "node_modules")
]
},
// Abort on first error
bail: true
};
// Rule for *.less processing
function lessRule(extractor, {minimize} = {minimize: false}) {
return {
test: /\.less$/,
use: extractor.extract({
use: [
{
loader: "css-loader",
options: {
// Apply the next loader (less-loader) to any @imports
importLoaders: 1,
// Minify using cssnano
minimize,
// Generate sourcemaps
sourceMap: true
}
},
{
loader: "less-loader",
options: {
// Generate sourcemaps
sourceMap: true
}
}
]
})
};
}
// Rule for *.css processing
function cssRule(extractor) {
return {
test: /\.css$/,
use: extractor.extract({
loader: "css-loader",
options: {
// Generate sourcemaps
sourceMap: true
}
})
};
}
// Ensure that the environment is set
function defineEnvironment(env) {
return new webpack.DefinePlugin({"process.env.NODE_ENV": JSON.stringify(`${env}`)});
}
/*
* Creates external *.css files from any imported styles (e.g. import "./my-styles.css";)
* Note: HtmlWebpackPlugin injects <link> tags in the order listed in the plugins array, so vendor must be first
*/
function extractAppCss(hashFilename) {
return new ExtractTextPlugin({
filename: hashFilename ? "app-[contenthash:6].css" : "app.css",
disable: false,
allChunks: true
});
}
function extractVendorCss(hashFilename) {
return new ExtractTextPlugin({
filename: hashFilename ? "vendor-[contenthash:6].css" : "vendor.css",
disable: false,
allChunks: true
});
}
// Generate a service worker to precache static assets
function generateServiceWorker({handleFetch} = {handleFetch: true}) {
return new WorkboxWebpackPlugin({
swDest: "public/service-worker.js",
globDirectory: "public",
globPatterns: ["**/*.{html,js,css,ico}", "fonts/*"],
cacheId: packageJson.name,
skipWaiting: true,
clientsClaim: true,
modifyUrlPrefix: {public: ""},
dontCacheBustUrlsMatching: /.*\.(js|css|ico|ttf|woff|woff2|eot|svg)$/,
handleFetch
});
}
module.exports = {
entry,
output,
lessRule,
cssRule,
fontRule,
iconRule,
defineEnvironment,
cleanBuildDirectory,
providejQuery,
separateBundles,
extractAppCss,
extractVendorCss,
createIndexHtml,
copyStaticAssets,
generateServiceWorker,
config
};