forked from moonad/TaelinArena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
94 lines (90 loc) · 2.64 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const formalityResolver = require('formality-loader').resolver
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const webpack = require("webpack");
const fs = require("fs");
module.exports = {
mode: 'production',
optimization: {minimize: true},
//devtool: "source-map",
entry: './src/index.js',
module: {
rules: [
{
test: /\.fm$/,
loader: 'formality-loader',
options: { typeCheckMode: 'none' }
},
{
test: /\.vox$/i,
use: 'arraybuffer-loader',
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
},
],
},
]
},
output: {
filename: 'index.[contenthash].js',
path: path.resolve(__dirname, 'docs')
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
resolve: { plugins: [formalityResolver] },
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
}),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./docs/models.json')
}),
inject_models_js,
new CompressionWebpackPlugin(),
]
};
// This is a temporary hack that replaces `models.js` by
// `models.[hash].js` on `index.html`. I couldn't find a way
// to do it with webpack since the `models` file is
// generated as a DLL.
function inject_models_js() {
var dir = path.join(__dirname, "docs");
// After plugins are done
this.hooks.afterEmit.tap("inject_models_js", function() {
// Reads all files on /docs
fs.readdir(dir, function(err, files) {
// Sorts them by modification date
var files = files.map(function (file_name) {
return {
name: file_name,
time: fs.statSync(path.join(dir,file_name)).mtime.getTime()
};
}).sort(function (a, b) { return a.time - b.time; });
// Finds the latest models.[hash].js, injects it on
// the index.html and deletes index.html.gz because it
// would be incorrect.
for (var i = 0; i < files.length; ++i) {
var file = files[i];
if (/^models.*js$/.test(file.name)) {
var html_path = path.join(dir, "index.html");
var html_text = fs.readFileSync(html_path, "utf8");
var html_text = html_text.replace("models.js", file.name);
var html_gz_path = path.join(dir, "index.html.gz");
fs.writeFileSync(html_path, html_text);
if (fs.existsSync(html_gz_path)) {
fs.unlinkSync(html_gz_path);
}
}
}
});
});
}