-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathwebpack.config.corelibs.js
More file actions
174 lines (159 loc) · 5.67 KB
/
webpack.config.corelibs.js
File metadata and controls
174 lines (159 loc) · 5.67 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
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
import path, { dirname } from "path";
import fs from "fs";
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
import ModuleScopePlugin from "@k88/module-scope-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
export default (isLiveBuild, buildInfo, minify = false, analyze = false, sourceMap = false) =>
{
const __dirname = dirname(fileURLToPath(import.meta.url));
const getDirectories = function (arr, namespace)
{
const names = [];
for (let i = 0; i < arr.length; i++)
{
const dirent = arr[i];
if (dirent.isDirectory() && !dirent.name.startsWith("."))
{
const hasIndexJs = fs.existsSync(path.join(dirent.path, dirent.name, "index.js"));
const hasNamespaceJs = fs.existsSync(path.join(dirent.path, dirent.name, dirent.name + ".js"));
const isSubDir = dirent.path.endsWith(namespace);
const isCoreLib = hasIndexJs || (hasNamespaceJs && !isSubDir);
if (isCoreLib)
{
names.push(dirent.name);
}
}
}
return names;
};
const getEntryFile = function (arr, namespace)
{
let entryFile = "index.js";
const possibleEntryFiles = ["index.js", namespace + ".js"];
const names = [];
for (let i = 0; i < arr.length; i++)
{
const dirent = arr[i];
const fileName = dirent.name;
if (!dirent.isDirectory() && !fileName.startsWith(".") && fileName.endsWith(".js"))
{
names.push(dirent.name);
}
}
if (names.includes("index.js"))
{
entryFile = "index.js";
}
else
{
entryFile = names.find((name) => { return possibleEntryFiles.includes(name); });
}
return entryFile;
};
const createOutputEntryObjectsNamespace = (namespace) =>
{
const outputs = [];
const dirContent = fs.readdirSync(path.join(__dirname, "src", "corelibs", namespace), { "withFileTypes": true });
const namespaceEntryFile = getEntryFile(dirContent, namespace);
const namespaceParts = namespace.split("_");
let libraryNamespace = "CABLES";
if (namespaceParts.length > 1)
{
libraryNamespace = "";
namespaceParts.pop();
namespaceParts.forEach((part, i) =>
{
if (i > 0) libraryNamespace += ".";
libraryNamespace += part.toUpperCase();
});
}
const output = {
"entry": {
"main": {
"import": path.join(__dirname, "src", "corelibs", namespace, namespaceEntryFile),
"filename": namespace + ".js"
}
},
"output": {
"path": path.join(__dirname, "build", "corelibs"),
"library": {
"name": libraryNamespace.toUpperCase(),
"type": "assign-properties"
}
}
};
const libraryExternals = {
"cables": "CABLES",
"cables-shared-client": "CABLES.SHARED",
};
if (libraryNamespace.startsWith("THREE."))
{
libraryExternals.three = "THREE";
}
output.externals = libraryExternals;
outputs.push(output);
return outputs;
};
const readLibraryFiles = () =>
{
const LIBDIR_ENTRIES = fs.readdirSync(path.join(__dirname, "src", "corelibs"), { "withFileTypes": true });
const NAMESPACE_DIRS = getDirectories(LIBDIR_ENTRIES);
const outputObjects = [];
for (let i = 0; i < NAMESPACE_DIRS.length; i++)
{
const namespace = NAMESPACE_DIRS[i];
outputObjects.push(createOutputEntryObjectsNamespace(namespace, isLiveBuild));
}
return outputObjects.flat();
};
const entryAndOutputObjects = readLibraryFiles(isLiveBuild);
const defaultConfig = {
"mode": "production",
"devtool": false,
"optimization": {
"concatenateModules": true,
"minimizer": [new TerserPlugin({
"extractComments": false,
"terserOptions": { "output": { "comments": false } }
})],
"minimize": minify,
"usedExports": true
},
"module": {
"rules": [
{
"test": /\.frag/,
"use": "raw-loader",
},
{
"test": /\.vert/,
"use": "raw-loader",
},
{
"test": /\.wgsl/,
"use": "raw-loader",
}
].filter(Boolean),
},
"resolve": {
"extensions": [".json", ".js", ".jsx"],
"plugins": [
new ModuleScopePlugin.default("src/corelibs/"),
],
},
};
const configs = [];
for (let i = 0; i < entryAndOutputObjects.length; i++)
{
const entryAndOutput = entryAndOutputObjects[i];
if (analyze)
{
if (!entryAndOutput.plugins) entryAndOutput.plugins = [];
const baseName = path.basename(entryAndOutput.entry.main.filename, ".js");
entryAndOutput.plugins.push(new BundleAnalyzerPlugin({ "analyzerMode": "static", "openAnalyzer": false, "reportTitle": baseName, "reportFilename": path.join(__dirname, "build", baseName + ".html") }));
}
configs.push({ ...defaultConfig, ...entryAndOutput });
}
return configs;
};