forked from eight04/rollup-plugin-external-globals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (44 loc) · 1.72 KB
/
index.js
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
import MagicString from "magic-string";
import {createFilter} from "./lib/pluginutils.js";
import importToGlobals from "./lib/import-to-globals.js";
const defaultDynamicWrapper = id => `Promise.resolve(${id})`;
function externalGlobals(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper} = {}) {
if (!globals) {
throw new TypeError("Missing mandatory option 'globals'");
}
let getName = globals;
const globalsType = typeof globals;
const isGlobalsObj = globalsType === "object";
if (isGlobalsObj) {
getName = function (name) {
if (Object.prototype.hasOwnProperty.call(globals, name)) {
return globals[name];
}
};
} else if (globalsType !== "function") {
throw new TypeError(`Unexpected type of 'globals', got '${globalsType}'`);
}
const dynamicWrapperType = typeof dynamicWrapper;
if (dynamicWrapperType !== "function") {
throw new TypeError(`Unexpected type of 'dynamicWrapper', got '${dynamicWrapperType}'`);
}
const filter = createFilter(include, exclude);
return {
name: "rollup-plugin-external-globals", transform
};
async function transform(code, id) {
if ((id[0] !== "\0" && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
return;
}
const ast = this.parse(code);
code = new MagicString(code);
const isTouched = await importToGlobals({
ast, code, getName, getDynamicWrapper: dynamicWrapper
});
return isTouched ? {
code: code.toString(), map: code.generateMap()
} : undefined;
}
}
export {externalGlobals};
export default externalGlobals;