-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.common.js
326 lines (300 loc) · 12.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*******************************************************************************
*
* cordova-plugin-webpack-transpiler webpack.config.js
* ---------------------------------------------------
*
* Feel free to modify this file to your needs; it will not be overwritten by
* the plugin unless you delete the file entirely.
*
* Notes:
*
* - External/Sibling mode is detected automatically. When in External mode,
* additional assets can be copied (for example, from "www.src -> www").
* - The names of all the files and paths are not hard-coded; you can change
* them below to match your configuration.
*
******************************************************************************/
// portions based on https://github.com/simonfl3tcher/react-progressive-web-app/blob/master/webpack.config.js
var path = require("path"),
fs = require("fs"),
webpack = require("webpack"),
ExtractTextPlugin = require("extract-text-webpack-plugin"),
CopyWebpackPlugin = require("copy-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin");
var extensions = [".js", ".ts", ".jsx", ".es", // typical JS extensions
".jsm", ".esm", // jsm is node's ES6 module ext
".json", // some modules require json without an extension
".css", ".scss", // CSS & SASS extensions
"*"]; // allow extensions on imports
var dirs = {
css: "css",
es: "es",
external: "www.src",
html: "html",
img: "img",
js: "js",
lib: "lib",
scss: "scss",
ts: "ts",
vendor: "vendor",
www: "www",
aliases: {
Lib: "lib",
Vendor: "vendor",
Components: "$JS/components",
Controllers: "$JS/controllers",
Models: "$JS/models",
Pages: "$JS/pages",
Routes: "$JS/routes",
Templates: "$JS/templates",
Utilities: "$JS/util",
Views: "$JS/views",
}
}
/*
* These files and globs will be copied if using the external configuration
* (www.src -> www).
*
* For full information, see https://github.com/kevlened/copy-webpack-plugin
******************************************************************************/
var assetsToCopyIfExternal = [
{ from: "*.*" },
{ from: dirs.img + "/**/*" },
{ from: dirs.css + "/**/*" },
{ from: dirs.js + "/**/*" },
{ from: dirs.vendor + "/**/*" },
{ from: dirs.lib + "/**/*" },
{ from: dirs.html + "/**/*" },
];
var assetsToCopyIfSibling = [];
var vendor = [];
/*
* If you're just changing file and path names, you don't need to proceed any
* further. If you need to change the logic, proceed.
******************************************************************************/
function config(options) {
var src = options.src,
allowTypeScript = options.allowTypeScript || false,
allowScss = options.allowScss || false,
assetsToCopy = [],
assetsToCopyIfExternal = options.assetsToCopyIfExternal,
assetsToCopyIfSibling = options.assetsToCopyIfSibling,
cssFallbackLoader = options.cssFallbackLoader || "style-loader",
dirs = options.dirs,
devtool = options.devtool || "inline-source-map",
entryFiles = options.entryFiles,
extensions = options.extensions || [
".js", ".ts", ".jsx", ".es", // typical JS extensions
".jsm", ".esm", // jsm is node's ES6 module ext
".json", // some modules require json without an extension
".css", ".scss", // CSS & SASS extensions
"*"
], // allow extensions on imports
indexes = options.indexes,
mode = options.mode || process.env.NODE_ENV || "development",
operatingMode = "external",
outputFile = options.outputFile,
outputPaths = options.outputPaths,
sourcePaths = options.sourcePaths,
transpiler = options.transpiler || (allowTypeScript ? "ts-loader" : "babel-loader"),
vendor = options.vendor;
/*
* determine operating mode (external vs sibling)
*****************************************************************************/
if (!src) {
src = path.resolve(__dirname, dirs.external);
assetsToCopy = assetsToCopyIfExternal;
operatingMode = "external";
}
if (!fs.existsSync(src)) {
src = path.resolve(__dirname, dirs.www);
assetsToCopy = assetsToCopyIfSibling;
operatingMode = "sibling"
}
/*
* it is assumed that you will be using "es/*.js" for ES2015+, "ts/*.ts" for
* TypeScript, and "scss/*.scss" for SASS. You can change these as needed.
******************************************************************************/
sourcePaths = Object.assign({}, {
src: src,
es: dirs.es,
ts: dirs.ts,
css: dirs.css,
scss: dirs.scss
}, sourcePaths ? sourcePaths : {});
/*
* It is assumed that your output is going to "www", and that the JavaScript
* (or TypeScript) should be stored in "www/js", and that any generated CSS
* should be stored in "www/css".
******************************************************************************/
outputPaths = Object.assign({}, {
www: dirs.www,
js: dirs.js,
css: dirs.css
}, outputPaths ? outputPaths : {});
/*
* Feel free to change the following if these assumptions are incorrect:
* - If you are using SASS, the entry file will be named "styles.scss" and
* will be copied to bundle.css
* - If you are using ES2015+, the entry file will be named "index.js" and
* will be copied to bundle.js
* - If you are using TypeScript, the entry file will be named "index.ts"
* and will be copied to bundle.js
******************************************************************************/
indexes = Object.assign({}, {
css: { from: path.join(sourcePaths.css, "styles.css"), to: path.join(outputPaths.css, "bundle.css") },
scss: { from: path.join(sourcePaths.scss, "styles.scss"), to: path.join(outputPaths.css, "bundle.css") },
es: { from: path.join(sourcePaths.es, "index.js"), to: path.join(outputPaths.js, "bundle.js") },
ts: { from: path.join(sourcePaths.ts, "index.ts"), to: path.join(outputPaths.js, "bundle.js") },
vendor: { js: path.join(outputPaths.js, "vendor.js"), css: path.join(outputPaths.css, "vendor.css") }
}, indexes ? indexes : {});
// if allowTypeScript is true AND the typescript path in sourcePaths exists,
// we'll assume we're using typescript
var usingTypeScript = allowTypeScript && fs.existsSync(path.resolve(sourcePaths.src, sourcePaths.ts));
// set up our entry files
var jsEntryFile = usingTypeScript ? indexes.ts.from : indexes.es.from,
cssEntryFile = allowScss ? indexes.scss.from : indexes.css.from;
// define our extract plugins for CSS; if allowScss is true, we assume using scss/styles.scss
var extractCss = new ExtractTextPlugin(allowScss ? indexes.scss.to : indexes.css.to),
extractVendorCss = new ExtractTextPlugin(indexes.vendor.css);
// If no output file specified, default to indexes.(es|ts).to
if (!outputFile) {
outputFile = usingTypeScript ? indexes.ts.to : indexes.es.to;
}
// if no entry files passed in, we need to come up with suitable defaults
if (!entryFiles) {
// include the javascript file
entryFiles = { app: ["./" + jsEntryFile] };
// and the (s)css file, if present
if (fs.existsSync(path.resolve(sourcePaths.src, cssEntryFile))) {
entryFiles.app.push("./" + cssEntryFile);
}
}
// if vendors have been provided, add them to entryFiles
if (vendor.length > 0) {
entryFiles.vendor = vendor;
}
return {
context: sourcePaths.src,
entry: entryFiles,
devtool: devtool,
output: {
filename: outputFile,
path: outputPaths.www,
},
resolve: {
extensions: extensions,
modules: [
path.resolve(sourcePaths.src, dirs.es, "lib"),
path.resolve(sourcePaths.src, dirs.es, "vendor"),
path.resolve(sourcePaths.src, dirs.ts, "lib"),
path.resolve(sourcePaths.src, dirs.ts, "vendor"),
path.resolve(sourcePaths.src, "lib"),
path.resolve(sourcePaths.src, "vendor"),
"node_modules"
],
alias: Object.assign({}, (function getAliases() {
var k, v, obj = {}, p;
for (k in dirs.aliases) {
if (Object.prototype.hasOwnProperty.call(dirs.aliases, k)) {
v = dirs.aliases[k];
p = v;
if (v.substr(0,4) === "$JS/") {
p = path.join(usingTypeScript ? dirs.ts : dirs.es, v.substr(4));
}
obj["$" + k.toUpperCase()] = p;
obj[k] = p;
}
}
return obj;
}()))
},
module: {
rules: [
// HTML & TEXT files should just be loaded as raw (included in bundle)
{ test: /\.(html|txt)$/, use: "raw-loader" },
// Images and web fonts should be copied & file path returned
// note: copying only occurs when in external mode
{
test: /\.(png|jpe?g|svg|gif|eot|ttf|woff|woff2)$/,
use: ["file-loader?name=[path][name].[ext]&emitFile=" +
(operatingMode === "external" ? "true" : "false")]
},
// JSON/JSON5 should use the JSON5 loader and be included in bundle
{ test: /\.(json|json5)$/, use: "json5-loader" },
// Extract our app's CSS into the bundle
{
test: extractCss ? /\.s?css$/ : /\.css$/,
exclude: /node_modules\/.*\.css$/,
use: extractCss.extract({
fallback: cssFallbackLoader,
publicPath: "../",
use: ((function () {
var arr = [
{ loader: "css-loader?sourceMap=true" },
{ loader: "resolve-url-loader?sourceMap=true" },
];
if (cssEntryFile === indexes.scss.from && allowScss) {
arr.push({ loader: "sass-loader?sourceMap=true" });
}
return arr;
})())
})
},
// css files in node_modules are put into a vendor bundle
{
test: /node_modules\/.*\.css$/,
use: extractVendorCss.extract({
fallback: cssFallbackLoader,
publicPath: "../",
use: [
{ loader: "css-loader?sourceMap=true" },
{ loader: "resolve-url-loader?sourceMap=true" },
]
})
},
// JavaScript / TypeScript code
{
test: allowTypeScript ? /\.(js|jsx|ts|tsx)$/ : /\.(js|jsx)$/,
use: [transpiler + (allowTypeScript ? (usingTypeScript ? "" : "?entryFileIsJs") : "")],
exclude: /node_modules/
},
]
},
plugins: (function () {
var plugins = [
new CopyWebpackPlugin(assetsToCopy),
extractCss
];
// only generate index.html with dependencies IF we're in external mode --
// otherwise we'd overwrite our user's own index.html in www
if (operatingMode === "external") {
plugins.push(new HtmlWebpackPlugin({
filename: "index.html",
template: "index.html",
inject: true,
chunksSortMode: "dependency"
}));
}
// only create the JS vendor chunk if we've been given vendors
if (vendor.length > 0) {
plugins.push(new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
filename: indexes.vendor.js
}));
}
return plugins;
}())
};
}
module.exports = {
defaults: {
dirs: dirs,
extensions: extensions,
assetsToCopyIfExternal: assetsToCopyIfExternal,
assetsToCopyIfSibling: assetsToCopyIfSibling,
vendor: vendor,
transpiler: "ts-loader",
},
config: config
}