-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathwebpack.config.js
More file actions
147 lines (126 loc) · 3.4 KB
/
webpack.config.js
File metadata and controls
147 lines (126 loc) · 3.4 KB
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
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import { GitRevisionPlugin } from 'git-revision-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import path from 'path';
// Workaround now this is a module...
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// const StripAssertionCode = require('ts-transformer-unassert').default;
const ADD_TS_EXTENSIONS_TO_WEPACK = [".ts", ".tsx", ".js"];
const SUPPORT_FULLY_QUALIFIED_TS_ESM_IMPORTS = {
".js": [".js", ".ts"],
".cjs": [".cjs", ".cts"],
".mjs": [".mjs", ".mts"],
};
const HANDLE_TYPESCRIPT_WITH_TS_LOADER = { test: /\.([cm]?ts|tsx)$/, loader: "ts-loader" };
const OUTPUT_DIRECTORY = 'dist';
function recursivelyCopy(dir) {
return {from: dir, to: dir, toType: 'dir'};
}
function cleanUpLeftovers() {
return new CleanWebpackPlugin();
}
function copyStaticAssets() {
return new CopyPlugin({
"patterns": [
recursivelyCopy('css'),
recursivelyCopy('images'),
recursivelyCopy('sprites'),
'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 injectBuildIdIntoNameLicense(gitHash) {
return new HtmlWebpackPlugin({
gitHash,
inject: false,
hash: true,
template: './name_license.html',
filename: 'name_license.html'
});
}
function addDevelopmentConfigTo(options) {
options.devServer = {
contentBase: `./${OUTPUT_DIRECTORY}`
};
options.devtool = 'source-maps';
options.mode = 'development';
}
function addProductionConfigTo(options) {
/*
const assertionStrippingConfig = {
options: {
getCustomTransformers: () => {
return ({before: [StripAssertionCode]});
}
}
};
stripTSAssertionsRule = Object.assign(assertionStrippingConfig, HANDLE_TYPESCRIPT_WITH_ATL);
options.module.rules.push(stripTSAssertionsRule);
*/
}
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' main`
});
return gitPlugin.commithash().slice(0, 12);
}
function commonOptions() {
const buildId = getBuildId();
const options = {
entry: './src/micropolis.js',
resolve: {
extensions: ADD_TS_EXTENSIONS_TO_WEPACK,
extensionAlias: SUPPORT_FULLY_QUALIFIED_TS_ESM_IMPORTS,
},
module: {
rules: [
HANDLE_TYPESCRIPT_WITH_TS_LOADER,
],
},
output: {
path: path.resolve(__dirname, OUTPUT_DIRECTORY),
filename: 'src/micropolis.js'
},
plugins: [
cleanUpLeftovers(),
copyStaticAssets(),
injectBundleIntoHTML(buildId),
injectBuildIdIntoAbout(buildId),
injectBuildIdIntoNameLicense(buildId),
],
};
return options;
}
export default function(env, argv) {
let options = commonOptions();
if (env.development) {
addDevelopmentConfigTo(options);
} else {
addProductionConfigTo(options);
}
return options;
};