Files
test-repo/scripts/build/esbuild-plugins/umd.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
3.9 KiB
JavaScript
Raw Normal View History

import fs from "node:fs";
import camelCase from "camelcase";
import { outdent } from "outdent";
const PLACEHOLDER = "__PLACEHOLDER__";
2023-02-15 12:35:14 +08:00
function getUmdWrapper({ name }, build) {
const path = name.split(".");
const { minify } = build.initialOptions;
const temporaryName = minify ? "m" : camelCase(name);
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);
} else {
globalObjectText.push(`${object} = ${object} || {};`);
}
}
globalObjectText = globalObjectText
.map((text) => `${" ".repeat(4)}${text}`)
.join("\n");
let wrapper = outdent`
(function (factory) {
2023-02-15 12:35:14 +08:00
function interopModuleDefault() {
2022-02-17 09:47:35 +08:00
var module = factory();
2023-02-15 12:35:14 +08:00
return module.default || module;
2022-02-17 09:47:35 +08:00
}
if (typeof exports === "object" && typeof module === "object") {
2023-02-15 12:35:14 +08:00
module.exports = interopModuleDefault();
} else if (typeof define === "function" && define.amd) {
2023-02-15 12:35:14 +08:00
define(interopModuleDefault);
} else {
var root =
typeof globalThis !== "undefined"
? globalThis
: typeof global !== "undefined"
? global
: typeof self !== "undefined"
? self
: this || {};
2023-02-15 12:35:14 +08:00
${globalObjectText.trimStart()} = interopModuleDefault();
}
})(function () {
"use strict";${PLACEHOLDER}
});
`;
if (minify) {
wrapper = build.esbuild
.transformSync(wrapper, { loader: "js", minify })
.code.trim();
if (!wrapper.includes(PLACEHOLDER)) {
throw new Error("Unexpected code");
}
}
const [intro, outro] = wrapper.split(PLACEHOLDER);
return {
name: temporaryName,
intro,
outro,
2022-03-13 20:09:21 +08:00
expectedOutput: {
start: minify
2022-03-14 10:38:40 +08:00
? `var ${temporaryName}=(()=>{`
: `var ${temporaryName} = (() => {`,
2022-03-13 20:09:21 +08:00
end: "})();",
},
};
}
2022-02-17 09:47:35 +08:00
export default function esbuildPluginUmd(options) {
return {
name: "umd",
setup(build) {
2022-02-17 09:47:35 +08:00
const esbuildOptions = build.initialOptions;
const { globalName, format, outfile } = esbuildOptions;
if (globalName) {
2022-02-17 09:47:35 +08:00
throw new Error("'globalName' in esbuildOptions cannot be set.");
}
if (format !== "umd") {
2022-02-17 09:47:35 +08:00
throw new Error("'format' esbuildOptions must be 'umd'.");
}
if (!outfile) {
2022-02-17 09:47:35 +08:00
throw new Error("'outfile' esbuildOptions is required.");
}
const {
name: temporaryName,
intro,
outro,
expectedOutput,
2022-02-17 09:47:35 +08:00
} = getUmdWrapper(options, build);
esbuildOptions.globalName = temporaryName;
esbuildOptions.format = "iife";
build.onEnd((result) => {
if (result.errors.length > 0) {
return;
}
if (!fs.existsSync(outfile)) {
throw new Error(`${outfile} not exists`);
}
2022-03-14 10:38:40 +08:00
let text = fs.readFileSync(outfile, "utf8").trim();
// We already insert `"use strict";` in the wrapper
if (text.startsWith('"use strict";')) {
text = text.slice('"use strict";'.length).trimStart();
}
2022-03-13 20:09:21 +08:00
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]}
2022-02-17 09:47:35 +08:00
2022-03-13 20:09:21 +08:00
Got:
${actualOutput[property]}
`);
throw new Error("Unexpected output");
}
}
2022-03-13 20:09:21 +08:00
fs.writeFileSync(
outfile,
intro +
2022-03-14 11:06:16 +08:00
text
.slice(expectedOutput.start.length, -expectedOutput.end.length)
.trimEnd() +
2022-03-13 20:09:21 +08:00
outro,
);
});
},
};
}