/
umd.mjs
129 lines (115 loc) · 3.41 KB
/
umd.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
import fs from "node:fs";
import camelCase from "camelcase";
import { outdent } from "outdent";
function getUmdWrapper(name, build) {
const path = name.split(".");
const { minify } = build.initialOptions;
const temporaryName = minify ? "m" : camelCase(name);
const placeholder = "/*! bundled code !*/";
let globalObjectText = [];
for (let index = 0; index < path.length; index++) {
const object = ["root", ...path.slice(0, index + 1)].join(".");
if (index === path.length - 1) {
globalObjectText.push(`${object} = factory();`);
} else {
globalObjectText.push(`${object} = ${object} || {};`);
}
}
globalObjectText = globalObjectText
.map((text) => `${" ".repeat(4)}${text}`)
.join("\n");
let wrapper = outdent`
(function (factory) {
if (typeof exports === "object" && typeof module === "object") {
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
define(factory);
} else {
var root =
typeof globalThis !== "undefined"
? globalThis
: typeof global !== "undefined"
? global
: typeof self !== "undefined"
? self
: this || {};
${globalObjectText.trimStart()}
}
})(function() {
"use strict";${placeholder}
});
`;
if (minify) {
wrapper = build.esbuild
.transformSync(wrapper, { loader: "js", minify })
.code.trim();
}
const [intro, outro] = wrapper.split(placeholder);
return {
name: temporaryName,
intro,
outro,
expectedOutput: {
start: minify
? `"use strict";var ${temporaryName}=(()=>{`
: `"use strict";\nvar ${temporaryName} = (() => {`,
end: "})();",
},
};
}
export default function esbuildPluginUmd({ name }) {
return {
name: "umd",
setup(build) {
const options = build.initialOptions;
const { globalName, format, outfile } = options;
if (globalName) {
throw new Error("'globalName' in options cannot be set.");
}
if (format !== "umd") {
throw new Error("'format' options must be 'umd'.");
}
if (!outfile) {
throw new Error("'outfile' options is required.");
}
const {
name: temporaryName,
intro,
outro,
expectedOutput,
} = getUmdWrapper(name, build);
options.globalName = temporaryName;
options.format = "iife";
build.onEnd(() => {
if (!fs.existsSync(outfile)) {
throw new Error(`${outfile} not exists`);
}
const text = fs.readFileSync(outfile, "utf8").trim();
const actualOutput = {
start: text.slice(0, expectedOutput.start.length),
end: text.slice(-expectedOutput.end.length),
};
for (const property of ["start", "end"]) {
if (actualOutput[property] !== expectedOutput[property]) {
console.log();
console.error(outdent`
Expected output ${property}s with:
${expectedOutput[property]}
Got:
${actualOutput[property]}
`);
throw new Error("Unexpected output");
}
}
fs.writeFileSync(
outfile,
intro +
text
.slice(expectedOutput.start.length, -expectedOutput.end.length)
.trimEnd() +
outro
);
});
},
};
}