forked from cables-gl/cables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.libs.js
149 lines (134 loc) · 4.83 KB
/
webpack.config.libs.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
import path, { dirname } from "path";
import fs from "fs";
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
export default (isLiveBuild, buildInfo, minify = false, analyze = false) =>
{
const getDirectories = function (arr)
{
const names = [];
for (let i = 0; i < arr.length; i++)
{
const dirent = arr[i];
if (dirent.isDirectory() && !dirent.name.startsWith("."))
{
names.push(dirent.name);
}
}
return names;
};
const getJsFiles = function (arr)
{
const names = [];
for (let i = 0; i < arr.length; i++)
{
const dirent = arr[i];
if (!dirent.isDirectory() && !dirent.name.startsWith(".") && dirent.name.endsWith(".js"))
{
names.push(dirent.name);
}
}
return names;
};
const raiseFirstChar = (str) => { return str.charAt(0).toUpperCase() + str.substring(1); };
const flattenArray = (arr) => { return [].concat.apply([], arr); }; // .flat() only availible in Node 11+
const createOutputEntryObjectsNamespace = (namespace) =>
{
const outputs = [];
const dirContent = fs.readdirSync(
path.join(__dirname, "src", "libs", namespace),
{ "withFileTypes": true }
);
const namespaceFiles = getJsFiles(dirContent);
const namespaceSubDirectories = getDirectories(dirContent);
for (let i = 0; i < namespaceFiles.length; i++)
{
const file = namespaceFiles[i];
const baseName = file.split(".")[0];
const targetName = namespace === "cables" ? baseName : namespace + "_" + baseName;
outputs.push(
{
"entry": path.join(__dirname, "src", "libs", namespace, file),
"output": {
"filename": targetName + ".js",
"path": path.join(__dirname, "build", "libs"),
"library": [namespace.toUpperCase(), "COREMODULES", raiseFirstChar(baseName)],
"libraryExport": raiseFirstChar(namespace),
"libraryTarget": "this"
}
}
);
}
for (let i = 0; i < namespaceSubDirectories.length; i++)
{
const subdir = namespaceSubDirectories[i];
const targetName = namespace === "cables" ? subdir : namespace + "_" + subdir;
outputs.push(
{
"entry": path.join(__dirname, "src", "libs", namespace, subdir, "index.js"),
"output": {
"filename": targetName + ".js",
"path": path.join(__dirname, "build", "libs"),
"library": [namespace.toUpperCase(), "COREMODULES", raiseFirstChar(subdir)],
"libraryExport": raiseFirstChar(subdir),
"libraryTarget": "this",
}
}
);
}
return outputs;
};
const readLibraryFiles = () =>
{
const LIBDIR_ENTRIES = fs.readdirSync(
path.join(__dirname, "src", "libs"),
{ "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 flattenArray(outputObjects);
};
const __dirname = dirname(fileURLToPath(import.meta.url));
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": [
{ "sideEffects": false },
{
"test": /\.frag/,
"use": "raw-loader",
},
{
"test": /\.vert/,
"use": "raw-loader",
}
].filter(Boolean),
},
"resolve": {
"extensions": [".json", ".js", ".jsx"],
},
};
const configs = [];
for (let i = 0; i < entryAndOutputObjects.length; i++)
{
const entryAndOutput = entryAndOutputObjects[i];
configs.push({ ...defaultConfig, ...entryAndOutput });
}
return configs;
};