This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
webpack.config.babel.js
185 lines (167 loc) · 4.86 KB
/
webpack.config.babel.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCSSExtractPlugin = require("mini-css-extract-plugin");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const DirListWebpackPlugin = require("./dir-list-webpack-plugin");
const JSONWebpackPlugin = require("./json-webpack-plugin");
const thisPackage = require("./package.json");
const NODE_ENV = (() => {
if (process.env.NODE_ENV) {
return process.env.NODE_ENV.toLowerCase();
} else if (process.env.NODE_SUGGESTED_ENV) {
return process.env.NODE_SUGGESTED_ENV.toLowerCase();
}
return "development";
})();
const cssLoader = {
loader: "css-loader",
options: {
modules: true,
camelCase: "dashes",
importLoaders: 1,
localIdentName: "[name]__[local]___[hash:base64:5]",
},
};
let extraPlugins = [];
let extraLoaders = [];
let extraCopy = [];
let htmlMinifyOptions = false;
if (NODE_ENV === "production") {
extraPlugins.push(
new MiniCSSExtractPlugin(),
);
extraLoaders.push({
test: /\.css$/,
exclude: /node_modules/,
use: [
MiniCSSExtractPlugin.loader,
cssLoader,
],
});
extraCopy.push({from: "webextension/locales/locales.json",
to: "webextension/locales/"});
htmlMinifyOptions = {
removeComments: true,
collapseWhitespace: true,
};
} else {
extraPlugins.push(
new DirListWebpackPlugin({
directory: "webextension/locales",
filename: "webextension/locales/locales.json",
filter(file, stats) {
return file.charAt(0) !== "." && stats.isDirectory();
},
compareFunction(a, b) {
// Ensure en-US goes first, since it's the default.
const pre = (s) => s === "en-US" ? s : "z" + s;
return pre(a).localeCompare(pre(b));
},
}),
);
extraLoaders.push({
test: /\.css$/,
exclude: /node_modules/,
use: [
"style-loader",
cssLoader,
],
});
}
module.exports = {
mode: NODE_ENV,
context: path.join(__dirname, "src"),
devtool: "cheap-module-source-map",
optimization: {
minimize: false,
},
entry: {
"webextension/background": "./webextension/background/index.js",
"webextension/list/manage/index": "./webextension/list/manage/index.js",
"webextension/list/popup/index": "./webextension/list/popup/index.js",
"webextension/firstrun/index": "./webextension/firstrun/index.js",
"webextension/unlock/index": "./webextension/unlock/index.js",
"webextension/settings/index": "./webextension/settings/index.js",
},
output: {
filename: "[name].js",
path: path.join(__dirname, "dist"),
publicPath: "",
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
}, ...extraLoaders],
},
plugins: [
new CopyWebpackPlugin([
{from: "bootstrap.js"},
{from: "icon.png"},
{from: "webextension/fonts/*"},
{from: "webextension/icons/*"},
{from: "webextension/images/*"},
{from: "webextension/locales/**/*.ftl"},
...extraCopy,
]),
new webpack.DefinePlugin({
"process.env": {
"NODE_ENV": JSON.stringify(NODE_ENV),
},
}),
new HTMLWebpackPlugin({
template: "template.ejs",
filename: "webextension/firstrun/index.html",
chunks: ["webextension/firstrun/index"],
inject: false,
minify: htmlMinifyOptions,
icon: "/icons/lb_locked.svg",
}),
new HTMLWebpackPlugin({
template: "template.ejs",
filename: "webextension/list/manage/index.html",
chunks: ["webextension/list/manage/index"],
inject: false,
minify: htmlMinifyOptions,
icon: "/icons/lb_unlocked.svg",
}),
new HTMLWebpackPlugin({
template: "template.ejs",
filename: "webextension/list/popup/index.html",
chunks: ["webextension/list/popup/index"],
inject: false,
minify: htmlMinifyOptions,
}),
new HTMLWebpackPlugin({
template: "template.ejs",
filename: "webextension/unlock/index.html",
chunks: ["webextension/unlock/index"],
inject: false,
minify: htmlMinifyOptions,
}),
new HTMLWebpackPlugin({
template: "template.ejs",
filename: "webextension/settings/index.html",
chunks: ["webextension/settings/index"],
inject: false,
minify: htmlMinifyOptions,
}),
new HTMLWebpackPlugin({
template: "install.rdf.ejs",
filename: "install.rdf",
inject: false,
package: thisPackage,
}),
new JSONWebpackPlugin({
template: "webextension/manifest.json.tpl",
filename: "webextension/manifest.json",
data: thisPackage,
}),
...extraPlugins,
],
};