-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathumd-wrapper-plugin.mjs
180 lines (169 loc) · 4.56 KB
/
umd-wrapper-plugin.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable no-undef */
import path from 'path';
import fs from 'fs';
/*
Plugin is based on the `esbuild-plugin-umd-wrapper`, found at:
https://github.com/inqnuam/esbuild-plugin-umd-wrapper
*/
// eslint-disable-next-line no-unused-vars
const createWrapperWithLib = ({
depsKeys,
depsValKey,
amdLoader,
lib,
defineDeps,
globalDeps,
requireDeps,
}) => {
return `(function (g, f) {
if ("object" == typeof exports && "object" == typeof module) {
module.exports = f(${requireDeps});
} else if ("function" == typeof ${amdLoader} && ${amdLoader}.amd) {
${amdLoader}("${lib}", ${defineDeps}, f);
} else if ("object" == typeof exports) {
exports["${lib}"] = f(${requireDeps});
} else {
g["${lib}"] = {};
Object.assign(g["${lib}"], f(${globalDeps}));
}
}(this, (${depsKeys}) => {
var exports = {};
var module = { exports };\n\n`;
};
export const alphabet = [
'__da',
'__db',
'__dc',
'__dd',
'__de',
'__df',
'__dg',
'__dh',
'__di',
'__dj',
'__dk',
'__dl',
'__dm',
'__dn',
'__do',
'__dp',
'__dq',
'__dr',
'__ds',
'__dt',
'__du',
'__dv',
'__dw',
'__dx',
'__dy',
'__dz',
];
function getUmdBanner(opts) {
const external = opts.external ?? [];
const defineDeps = external?.length ? `['${external.join("', '")}']` : '[]';
const globalDeps = external?.map((x) => `g["${x}"]`).join(', ') ?? '';
const requireDeps = external?.map((x) => `require('${x}')`).join(', ') ?? '';
let deps = [];
if (external) {
deps = external.map((x, i) => {
return {
key: alphabet[i],
val: x,
};
});
}
const depsKeys = deps.map((x) => x.key).join(', ');
const depsValKey = deps.map((x) => `"${x.val}": ${x.key}`).join(', ');
const options = {
depsKeys,
depsValKey,
amdLoader: 'define',
defineDeps,
globalDeps,
requireDeps,
lib: opts.libraryName,
};
const result = createWrapperWithLib(options);
return result;
}
export const umdFooter = `if (typeof module.exports == "object" && typeof exports == "object") {
var __cp = (to, from, except, desc) => {
if ((from && typeof from === "object") || typeof from === "function") {
for (let key of Object.getOwnPropertyNames(from)) {
if (!Object.prototype.hasOwnProperty.call(to, key) && key !== except)
Object.defineProperty(to, key, {
get: () => from[key],
enumerable: !(desc = Object.getOwnPropertyDescriptor(from, key)) || desc.enumerable,
});
}
}
return to;
};
module.exports = __cp(module.exports, exports);
}
return module.exports;
}))\n\n\n`;
export const umdWrapperSetup = (build) => {
const { initialOptions } = build;
const external = initialOptions.external;
const content = getUmdBanner(external);
if (initialOptions.footer) {
if (initialOptions.footer.js) {
initialOptions.footer.js += umdFooter;
} else {
initialOptions.footer.js = umdFooter;
}
} else {
initialOptions.footer = {
js: umdFooter,
};
}
if (initialOptions.banner) {
if (initialOptions.banner.js) {
initialOptions.banner.js += content;
} else {
initialOptions.banner.js = content;
}
} else {
initialOptions.banner = {
js: content,
};
}
};
export const createUmdWrapper = (opts) => {
let pluginExternalDependencies = [];
return {
name: 'add-umd-wrapper',
esbuildOptions(options) {
options.format = 'cjs';
pluginExternalDependencies = [];
return options;
},
buildEnd(result) {
try {
result.writtenFiles.forEach((file) => {
const filePath = path.join(process.cwd(), file.name);
if (file.name.endsWith('.js')) {
const fileName = path.basename(file.name);
const umdBanner = getUmdBanner({
...opts,
external: pluginExternalDependencies,
});
const content = fs.readFileSync(filePath, 'utf-8');
const patchedFileContents = content.replace(
`//# sourceMappingURL=${fileName}.map`,
''
);
const scriptContent = `\n\n\n${patchedFileContents}\n\n\n`;
const wrappedContent = `${umdBanner}${scriptContent}${umdFooter}\n\n//# sourceMappingURL=${fileName}.map\n`;
const newContent = `/* umd */\n${wrappedContent}`;
fs.writeFileSync(filePath, newContent, 'utf8');
}
});
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
},
};
};