@b9g/libuild 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,71 +0,0 @@
1
- // src/umd-plugin.ts
2
- import * as FS from "fs/promises";
3
- import * as Path from "path";
4
- import MagicString from "magic-string";
5
- function umdPlugin(options) {
6
- return {
7
- name: "umd-wrapper",
8
- setup(build) {
9
- build.onEnd(async (result) => {
10
- if (result.errors.length > 0)
11
- return;
12
- const outputDir = build.initialOptions.outdir;
13
- if (outputDir) {
14
- const files = await FS.readdir(outputDir);
15
- for (const file of files) {
16
- if (file.endsWith(".js") && !file.endsWith(".js.map")) {
17
- await wrapWithUmd(Path.join(outputDir, file), options.globalName);
18
- }
19
- }
20
- }
21
- });
22
- }
23
- };
24
- }
25
- async function wrapWithUmd(filePath, globalName) {
26
- const code = await FS.readFile(filePath, "utf-8");
27
- const ms = new MagicString(code);
28
- const sourcemapComment = code.match(/\/\/# sourceMappingURL=.+$/m);
29
- if (sourcemapComment) {
30
- const index = code.lastIndexOf(sourcemapComment[0]);
31
- ms.remove(index, index + sourcemapComment[0].length);
32
- }
33
- const exportsMatch = code.match(/\nmodule\.exports\s*=\s*([^;]+);?\s*$/);
34
- if (exportsMatch) {
35
- const index = code.lastIndexOf(exportsMatch[0]);
36
- ms.overwrite(index, index + exportsMatch[0].length, `
37
- return ${exportsMatch[1]};`);
38
- }
39
- const umdHeader = `(function (root, factory) {
40
- if (typeof define === 'function' && define.amd) {
41
- // AMD
42
- define([], factory);
43
- } else if (typeof module === 'object' && module.exports) {
44
- // CommonJS
45
- module.exports = factory();
46
- } else {
47
- // Browser globals
48
- root.${globalName} = factory();
49
- }
50
- }(typeof self !== 'undefined' ? self : this, function () {
51
- `;
52
- const umdFooter = `
53
- }));`;
54
- ms.prepend(umdHeader);
55
- ms.append(umdFooter);
56
- const result = ms.toString();
57
- const map = ms.generateMap({
58
- file: Path.basename(filePath),
59
- source: Path.basename(filePath).replace(".js", ".ts"),
60
- includeContent: true
61
- });
62
- await FS.writeFile(filePath, result);
63
- await FS.writeFile(filePath + ".map", map.toString());
64
- await FS.writeFile(filePath, result + `
65
- //# sourceMappingURL=${Path.basename(filePath)}.map`);
66
- }
67
-
68
- export {
69
- umdPlugin
70
- };
71
- //# sourceMappingURL=chunk-EVLPGECD.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/umd-plugin.ts"],
4
- "sourcesContent": ["import * as FS from \"fs/promises\";\nimport * as Path from \"path\";\nimport MagicString from \"magic-string\";\n\ninterface UmdPluginOptions {\n globalName: string;\n}\n\nexport function umdPlugin(options: UmdPluginOptions) {\n return {\n name: \"umd-wrapper\",\n setup(build: any) {\n build.onEnd(async (result: any) => {\n if (result.errors.length > 0) return;\n\n // ESBuild doesn't provide outputFiles by default unless write: false\n // We need to find the generated files in the output directory\n const outputDir = build.initialOptions.outdir;\n if (outputDir) {\n const files = await FS.readdir(outputDir);\n for (const file of files) {\n if (file.endsWith(\".js\") && !file.endsWith(\".js.map\")) {\n await wrapWithUmd(Path.join(outputDir, file), options.globalName);\n }\n }\n }\n });\n },\n };\n}\n\nasync function wrapWithUmd(filePath: string, globalName: string) {\n const code = await FS.readFile(filePath, \"utf-8\");\n const ms = new MagicString(code);\n\n // Remove sourcemap comment\n const sourcemapComment = code.match(/\\/\\/# sourceMappingURL=.+$/m);\n if (sourcemapComment) {\n const index = code.lastIndexOf(sourcemapComment[0]);\n ms.remove(index, index + sourcemapComment[0].length);\n }\n\n // Replace module.exports with return\n const exportsMatch = code.match(/\\nmodule\\.exports\\s*=\\s*([^;]+);?\\s*$/);\n if (exportsMatch) {\n const index = code.lastIndexOf(exportsMatch[0]);\n ms.overwrite(index, index + exportsMatch[0].length, `\\nreturn ${exportsMatch[1]};`);\n }\n\n // UMD wrapper\n const umdHeader = `(function (root, factory) {\n if (typeof define === 'function' && define.amd) {\n // AMD\n define([], factory);\n } else if (typeof module === 'object' && module.exports) {\n // CommonJS\n module.exports = factory();\n } else {\n // Browser globals\n root.${globalName} = factory();\n }\n}(typeof self !== 'undefined' ? self : this, function () {\n`;\n\n const umdFooter = `\n}));`;\n\n // Wrap with UMD\n ms.prepend(umdHeader);\n ms.append(umdFooter);\n\n // Generate sourcemap and write files\n const result = ms.toString();\n const map = ms.generateMap({\n file: Path.basename(filePath),\n source: Path.basename(filePath).replace(\".js\", \".ts\"),\n includeContent: true,\n });\n\n await FS.writeFile(filePath, result);\n await FS.writeFile(filePath + \".map\", map.toString());\n await FS.writeFile(filePath, result + `\\n//# sourceMappingURL=${Path.basename(filePath)}.map`);\n}\n"],
5
- "mappings": ";AAAA,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,OAAO,iBAAiB;AAMjB,SAAS,UAAU,SAA2B;AACnD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM,OAAY;AAChB,YAAM,MAAM,OAAO,WAAgB;AACjC,YAAI,OAAO,OAAO,SAAS;AAAG;AAI9B,cAAM,YAAY,MAAM,eAAe;AACvC,YAAI,WAAW;AACb,gBAAM,QAAQ,MAAS,WAAQ,SAAS;AACxC,qBAAW,QAAQ,OAAO;AACxB,gBAAI,KAAK,SAAS,KAAK,KAAK,CAAC,KAAK,SAAS,SAAS,GAAG;AACrD,oBAAM,YAAiB,UAAK,WAAW,IAAI,GAAG,QAAQ,UAAU;AAAA,YAClE;AAAA,UACF;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,eAAe,YAAY,UAAkB,YAAoB;AAC/D,QAAM,OAAO,MAAS,YAAS,UAAU,OAAO;AAChD,QAAM,KAAK,IAAI,YAAY,IAAI;AAG/B,QAAM,mBAAmB,KAAK,MAAM,6BAA6B;AACjE,MAAI,kBAAkB;AACpB,UAAM,QAAQ,KAAK,YAAY,iBAAiB,CAAC,CAAC;AAClD,OAAG,OAAO,OAAO,QAAQ,iBAAiB,CAAC,EAAE,MAAM;AAAA,EACrD;AAGA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