-
Notifications
You must be signed in to change notification settings - Fork 95
/
webpack.config.js
137 lines (115 loc) · 3.18 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
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
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ScriptExtHtmlPlugin = require('script-ext-html-webpack-plugin');
const StripAssertionCode = require('ts-transformer-unassert').default;
const HANDLE_TYPESCRIPT_WITH_ATL = {test: /\.ts$/, loader: "awesome-typescript-loader"};
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 addProductionConfigTo(options) {
options.mode = 'production';
removeATLRuleFrom(options.module);
const assertionStrippingConfig = {
options: {
getCustomTransformers: () => {
return ({before: [StripAssertionCode]});
}
}
};
stripTSAssertionsRule = Object.assign(assertionStrippingConfig, HANDLE_TYPESCRIPT_WITH_ATL);
options.module.rules.push(stripTSAssertionsRule);
}
function removeATLRuleFrom(webpackModuleOptions) {
webpackModuleOptions.rules = webpackModuleOptions.rules.filter((rule) => rule !== HANDLE_TYPESCRIPT_WITH_ATL);
}
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);
}
function commonOptions() {
const buildId = getBuildId();
const options = {
entry: './src/micropolis.js',
module: {
rules: [
HANDLE_TYPESCRIPT_WITH_ATL
]
},
output: {
path: path.resolve(__dirname, OUTPUT_DIRECTORY),
filename: 'src/micropolis.js'
},
plugins: [
cleanUpLeftovers(),
copyStaticAssets(),
injectBundleIntoHTML(buildId),
injectBuildIdIntoAbout(buildId),
deferInjectedBundle()
],
resolve: {
extensions: [
".js", ".json", ".ts"
]
}
};
return options;
}
module.exports = function(env, argv) {
const options = commonOptions();
if (env.development) {
addDevelopmentConfigTo(options);
} else {
addProductionConfigTo(options);
}
return options;
};