forked from graememcc/micropolisJS
/
webpack.config.js
111 lines (93 loc) · 2.42 KB
/
webpack.config.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
var path = require('path');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var GitRevisionPlugin = require('git-revision-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ScriptExtHtmlPlugin = require('script-ext-html-webpack-plugin');
const OUTPUT_DIRECTORY = 'dist';
function recursivelyCopy(dir) {
return {from: dir, to: dir, toType: 'dir'};
}
function cleanUpLeftovers() {
return new CleanWebpackPlugin(OUTPUT_DIRECTORY, {});
}
function copyStaticAssets() {
return new CopyWebpackPlugin([
recursivelyCopy('css'),
recursivelyCopy('images'),
recursivelyCopy('sprites'),
recursivelyCopy('thirdparty'),
'LICENSE',
'COPYING',
]);
}
function injectBundleIntoHTML(gitHash) {
return new HtmlWebpackPlugin({
gitHash,
inject: true,
hash: true,
template: './index.html',
filename: 'index.html'
});
}
function injectBuildIdIntoAbout(gitHash) {
return new HtmlWebpackPlugin({
gitHash,
inject: false,
hash: true,
template: './about.html',
filename: 'about.html'
});
}
function deferInjectedBundle() {
return new ScriptExtHtmlPlugin({
defaultAttribute: 'defer'
});
}
function addDevelopmentConfigTo(options) {
options.devServer = {
contentBase: `./${OUTPUT_DIRECTORY}`
};
options.devtool = 'source-maps';
options.mode = 'development';
}
function getBuildId() {
// Technically don't need to use the webpack plugin, as not passing it to Webpack...
const gitPlugin = new GitRevisionPlugin({
commitHashCommand: `log -1 --pretty=format:'%h' master`
});
return gitPlugin.commithash().slice(0, 12);
}
module.exports = function(env, argv) {
const buildId = getBuildId();
const typescriptThroughATL = {test: /\.ts$/, loader: "awesome-typescript-loader"};
const options = {
entry: './src/micropolis.js',
mode: 'production',
module: {
rules: [
typescriptThroughATL
]
},
output: {
path: path.resolve(__dirname, OUTPUT_DIRECTORY),
filename: 'src/micropolis.js'
},
plugins: [
cleanUpLeftovers(),
copyStaticAssets(),
injectBundleIntoHTML(buildId),
injectBuildIdIntoAbout(buildId),
deferInjectedBundle()
],
resolve: {
extensions: [
".js", ".json", ".ts"
]
}
};
if (env.development) {
addDevelopmentConfigTo(options);
}
return options;
};