-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
esbuild.mjs
151 lines (134 loc) · 5.33 KB
/
esbuild.mjs
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
import ESBUILD from "esbuild";
import { deepmerge } from "carbon-pipeline";
import { scriptFiles as files, asyncForEach, watch, minify, compression, silent, production } from "./Lib/helper.mjs";
import { browserlist, options, importPlugins, flowSettings } from "./Lib/esbuildHelper.mjs";
async function build() {
// Pre-import plugins
const plugins = await importPlugins();
await asyncForEach(files, async ({ entryPoints, sourcemap, outdir, format, external, inline, extension }) => {
const firstOutdir = outdir[0];
const multiplePackages = outdir.length > 1;
const write = !compression || inline;
let splitting = false
if (options.splitting && format == "esm") {
splitting = true;
}
let additionlOptionsForSvelte = {};
if (plugins?.svelte?.plugin) {
additionlOptionsForSvelte = {
mainFields: ["svelte", "browser", "module", "main"],
conditions: ["svelte", "browser", "default", "import", "module"],
};
}
if (silent) {
options.logLevel = "silent";
}
if (inline) {
options.legalComments = "none";
}
const esOptions = {
...additionlOptionsForSvelte,
...options,
splitting,
entryPoints,
sourcemap,
bundle: true,
platform: "browser",
format,
minify,
external,
write,
target: browserlist,
outdir: firstOutdir,
outExtension: {
".js": extension,
},
define: {
...flowSettings,
"process.env.npm_package_version": '"' + process.env.npm_package_version + '"',
},
loader: {
...options.loader,
".cjsx": "jsx",
".ctsx": "tsx",
".mjsx": "jsx",
".mtsx": "tsx",
},
plugins: (() => {
let returnValue = [];
const svelte = plugins.svelte;
if (svelte?.plugin) {
const filterWarnings = {
code: [],
filenameStartsWith: [],
};
if (svelte?.filterWarnings?.code) {
const obj = svelte.filterWarnings.code;
for (const key in obj) {
if (obj[key]) {
filterWarnings.code.push(key);
}
}
}
if (svelte?.filterWarnings?.filenameStartsWith) {
const obj = svelte.filterWarnings.filenameStartsWith;
for (const key in obj) {
if (obj[key]) {
filterWarnings.filenameStartsWith.push(key);
}
}
}
const compilerOptions = deepmerge({ dev: !production }, svelte.options?.compilerOptions || {});
returnValue.push(
svelte.plugin({
preprocess: svelte.preprocess(),
...svelte.options,
compilerOptions,
filterWarnings: (warning) => {
let returnValue = true;
filterWarnings.code.forEach((code) => {
if (warning.code === code) {
returnValue = false;
}
});
filterWarnings.filenameStartsWith.forEach((startsWith) => {
if (warning.filename.startsWith(startsWith)) {
returnValue = false;
}
});
return returnValue;
},
})
);
}
for (const key in plugins) {
const entry = plugins[key];
if (key === "compress" || key === "copyFolder" || !entry.isStandardPlugin || !entry?.plugin) {
continue;
}
returnValue.push(entry.plugin(entry.options));
}
if (compression && !inline) {
returnValue.push(plugins.compress());
}
if (multiplePackages) {
const source = firstOutdir;
// We skip the first entry with index = 1
for (let index = 1; index < outdir.length; index++) {
const target = outdir[index];
returnValue.push(plugins.copyFolder({ source, target }));
}
}
return returnValue;
})(),
};
if (watch) {
const context = await ESBUILD.context(esOptions);
// Enable watch mode
await context.watch();
} else {
await ESBUILD.build(esOptions);
}
});
}
build();