-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.shared.js
230 lines (210 loc) · 6.74 KB
/
webpack.config.shared.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
const { CreateFileWebpack } = require('./.tools/CreateFileWebpack');
const PACKAGE = require('./package.json');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const WebpackFreeTexPacker = require('webpack-free-tex-packer');
const fs = require('fs');
const path = require('path');
const buildPath = path.resolve(__dirname, 'dist');
const webpack = require("webpack");
const ImageminPlugin = require('image-minimizer-webpack-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const WorkboxPlugin = require('workbox-webpack-plugin');
const JSONC = require('jsonc-parser');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const providePlugin = new webpack.ProvidePlugin({
THREE: 'three'
});
const copyPlugin = new CopyWebpackPlugin({
patterns: [{
from: 'assets', to: '', globOptions: {
ignore: ["**/thumbs.db", "**/Thumbs.db"],
},
transform: function (content, path) {
if (path.endsWith('.json')) {
return Buffer.from(JSON.stringify(JSON.parse(content.toString())), 'utf8');
}
return content;
}
},
{
from: "src/assets.jsonc", to: "assets.json",
// convert json5 to json
transform: function (content, path) {
return Buffer.from(JSON.stringify(JSONC.parse(content.toString())), 'utf8');
}
}]
});
const dirMain = path.resolve(__dirname, 'atlas');
const atlasPackages = [];
const manifest = [];
const readDirMain = fs.readdirSync(dirMain);
readDirMain.forEach((dirNext) => {
if (fs.lstatSync(dirMain + "/" + dirNext).isDirectory() && fs.readdirSync(dirMain + "/" + dirNext).length > 0) {
const texPackerPlugin = new WebpackFreeTexPacker(path.resolve(dirMain, dirNext), "atlas", {
textureName: dirNext,
width: 2048, //if we can make this smaller, we can make smaller final files
height: 2048, //if we can make this smaller, we can make smaller final files
fixedSize: false,
powerOfTwo: true,
padding: 2,
allowRotation: true,
detectIdentical: true,
allowTrim: true,
exporter: "Pixi",
removeFileExtension: false,
prependFolderName: true,
packer: "OptimalPacker",
omitZeroIndex: true
});
atlasPackages.push(texPackerPlugin);
manifest.push({ name: dirNext, url: "atlas/" + dirNext + ".json" })
}
});
const texPackerManifest = new CreateFileWebpack({
// file name
outputFile: 'atlas/autoPackedAtlas.json',
// content of the file
content: JSON.stringify(manifest)
});
const htmlPlugin = new HTMLWebpackPlugin({
template: 'src/index.ejs',
filename: 'index.html',
templateParameters: { PACKAGE: PACKAGE, buildDate: new Date() },
hash: true,
minify: false
});
const definePlugin = new webpack.DefinePlugin({
'process.env.DATE': Date.now(),
});
const defaultCompression = new ImageminPlugin({
minimizer: {
implementation: ImageminPlugin.sharpMinify,
options: {
encodeOptions: {
jpeg: {
// https://sharp.pixelplumbing.com/api-output#jpeg
quality: 90,
},
webp: {
// https://sharp.pixelplumbing.com/api-output#webp
nearLossless: true,
},
avif: {
// https://sharp.pixelplumbing.com/api-output#avif
lossless: true,
},
png: {
// https://sharp.pixelplumbing.com/api-output#png
adaptiveFiltering: true,
quality: 100,
compressionLevel: 7,
},
gif: {
// https://sharp.pixelplumbing.com/api-output#gif
reoptimise: true
},
},
},
},
});
const pwaManifest = new WebpackPwaManifest({
name: PACKAGE.pwa.name,
short_name: PACKAGE.pwa.short_name,
description: PACKAGE.pwa.description,
background_color: PACKAGE.pwa.background_color,
crossorigin: 'use-credentials', //I think this must remain like this....
orientation: "landscape",
icons: [
{
src: path.resolve('icon.png'),
sizes: [96, 120, 128, 144, 152, 180, 192, 256, 384, 512], // multiple sizes
purpose: 'any maskable'
}
],
ios: true
})
const pwaWorker = new WorkboxPlugin.GenerateSW({
swDest: 'sw.js',
// these options encourage the ServiceWorkers to get in there fast
// and not allow any straggling "old" SWs to hang around
clientsClaim: true,
skipWaiting: true,
cacheId: PACKAGE.name,
cleanupOutdatedCaches: true
});
const typeChecker = new ForkTsCheckerWebpackPlugin();
exports.providePlugin = providePlugin;
exports.copyPlugin = copyPlugin;
exports.htmlPlugin = htmlPlugin;
exports.definePlugin = definePlugin;
exports.atlasPackages = atlasPackages;
exports.texPackerManifest = texPackerManifest;
exports.defaultCompression = defaultCompression;
exports.pwaManifest = pwaManifest;
exports.pwaWorker = pwaWorker;
exports.typeChecker = typeChecker;
exports.config = {
stats: 'minimal',
entry: './src/index.ts',
devServer: {
compress: true, static: false,
client: {
logging: "warn",
// Can be used only for `errors`/`warnings`
//
overlay: {
errors: false,
warnings: false,
},
progress: true,
},
port: 3000, hot: true, host: '0.0.0.0'
},
module:
{
rules: [
// Typescript
{
test: /\.tsx?$/,
loader: 'esbuild-loader',
options: {
loader: 'ts',
target: 'es2015'
},
exclude: /node_modules/
},
//Shaders
{
test: /\.((frag)|(vert))$/,
use: 'raw-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js'], alias: {
root: __dirname,
src: path.resolve(__dirname, 'src'),
},
fallback: {
path: false,
fs: false,
}
},
plugins: [
providePlugin,
copyPlugin,
...atlasPackages,
texPackerManifest,
typeChecker,
htmlPlugin,
definePlugin,
// pwaManifest,
// pwaWorker,
],
performance: {
hints: false,
},
output: { filename: 'main.js', path: buildPath }
}