forked from excalidraw/excalidraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildPackage.js
165 lines (148 loc) · 3.94 KB
/
buildPackage.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
const { build } = require("esbuild");
const { sassPlugin } = require("esbuild-sass-plugin");
const { externalGlobalPlugin } = require("esbuild-plugin-external-global");
// Will be used later for treeshaking
//const fs = require("fs");
// const path = require("path");
// function getFiles(dir, files = []) {
// const fileList = fs.readdirSync(dir);
// for (const file of fileList) {
// const name = `${dir}/${file}`;
// if (
// name.includes("node_modules") ||
// name.includes("config") ||
// name.includes("package.json") ||
// name.includes("main.js") ||
// name.includes("index-node.ts") ||
// name.endsWith(".d.ts")
// ) {
// continue;
// }
// if (fs.statSync(name).isDirectory()) {
// getFiles(name, files);
// } else if (
// !(
// name.match(/\.(sa|sc|c)ss$/) ||
// name.match(/\.(woff|woff2|eot|ttf|otf)$/) ||
// name.match(/locales\/[^/]+\.json$/)
// )
// ) {
// continue;
// } else {
// files.push(name);
// }
// }
// return files;
// }
const browserConfig = {
entryPoints: ["index.tsx"],
bundle: true,
format: "esm",
plugins: [
sassPlugin(),
externalGlobalPlugin({
react: "React",
"react-dom": "ReactDOM",
}),
],
splitting: true,
loader: {
".woff2": "file",
},
};
const createESMBrowserBuild = async () => {
// Development unminified build with source maps
await build({
...browserConfig,
outdir: "dist/browser/dev",
sourcemap: true,
chunkNames: "excalidraw-assets-dev/[name]-[hash]",
assetNames: "excalidraw-assets-dev/[name]-[hash]",
define: {
"import.meta.env": JSON.stringify({ DEV: true }),
},
});
// production minified build without sourcemaps
await build({
...browserConfig,
outdir: "dist/browser/prod",
minify: true,
chunkNames: "excalidraw-assets/[name]-[hash]",
assetNames: "excalidraw-assets/[name]-[hash]",
define: {
"import.meta.env": JSON.stringify({ PROD: true }),
},
});
};
// const BASE_PATH = `${path.resolve(`${__dirname}/..`)}`;
// const filesinExcalidrawPackage = [
// ...getFiles(`${BASE_PATH}/packages/excalidraw`),
// `${BASE_PATH}/packages/utils/export.ts`,
// `${BASE_PATH}/packages/utils/bbox.ts`,
// ...getFiles(`${BASE_PATH}/public/fonts`),
// ];
// const filesToTransform = filesinExcalidrawPackage.filter((file) => {
// return !(
// file.includes("/__tests__/") ||
// file.includes(".test.") ||
// file.includes("/tests/") ||
// file.includes("example")
// );
// });
const rawConfigCommon = {
bundle: true,
format: "esm",
plugins: [sassPlugin()],
assetNames: "[dir]/[name]-[hash]",
loader: {
".json": "copy",
".woff2": "file",
},
packages: "external",
// chunks are always external, so they are not bundled within and get build separately
external: ["*.chunk"],
};
const rawConfigIndex = {
...rawConfigCommon,
entryPoints: ["index.tsx"],
};
const rawConfigChunks = {
...rawConfigCommon,
// create a separate chunk for each
entryPoints: ["**/*.chunk.ts"],
};
function buildDev(chunkConfig) {
const config = {
...chunkConfig,
sourcemap: true,
define: {
"import.meta.env": JSON.stringify({ DEV: true }),
},
outdir: "dist/dev",
};
return build(config);
}
function buildProd(chunkConfig) {
const config = {
...chunkConfig,
minify: true,
define: {
"import.meta.env": JSON.stringify({ PROD: true }),
},
outdir: "dist/prod",
};
return build(config);
}
const createESMRawBuild = async () => {
// development unminified build with source maps
await buildDev(rawConfigIndex);
await buildDev(rawConfigChunks);
// production minified buld without sourcemaps
await buildProd(rawConfigIndex);
await buildProd(rawConfigChunks);
};
// otherwise throws "ERROR: Could not resolve "./subset-worker.chunk"
(async () => {
await createESMRawBuild();
await createESMBrowserBuild();
})();