|
|
2 éve | |
|---|---|---|
| .. | ||
| lib | 2 éve | |
| node_modules | 2 éve | |
| LICENSE | 2 éve | |
| README.md | 2 éve | |
| index.js | 2 éve | |
| package.json | 2 éve | |
Transform external imports into global variables like Rollup's output.globals option. See rollup/rollup#2374
npm install -D rollup-plugin-external-globals
import externalGlobals from "rollup-plugin-external-globals";
export default {
input: ["entry.js"],
output: {
dir: "dist",
format: "es"
},
plugins: [
externalGlobals({
jquery: "$"
})
]
};
The above config transforms
import jq from "jquery";
console.log(jq(".test"));
into
console.log($(".test"));
It also transforms dynamic import:
import("jquery")
.then($ => {
$ = $.default || $;
console.log($(".test"));
});
// transformed
Promise.resolve($)
.then($ => {
$ = $.default || $;
console.log($(".test"));
});
Note: when using dynamic import, you should notice that in ES module, the resolved object is aways a module namespace, but the global variable might be not.
Note: this plugin only works with import/export syntax. If you are using a module loader transformer e.g.
rollup-plugin-commonjs, you have to put this plugin after the transformer plugin.
This module exports a single function.
const plugin = createPlugin(
globals: Object | Function,
{
include?: Array,
exclude?: Array,
dynamicWrapper?: Function
} = {}
);
globals is a moduleId/variableName map. For example, to map jquery module to $:
const globals = {
jquery: "$"
}
or provide a function that takes the moduleId and returns the variableName.
const globals = (id) => {
if (id === "jquery") {
return "$";
}
}
include is an array of glob patterns. If defined, only matched files would be transformed.
exclude is an array of glob patterns. Matched files would not be transformed.
dynamicWrapper is used to specify dynamic imports. Below is the default.
const dynamicWrapper = (id) => {
return `Promise.resolve(${id})`;
}
Virtual modules are always transformed.
0.6.1 (Oct 21, 2020)
0.6.0 (Aug 14, 2020)
0.5.0 (Dec 8, 2019)
dynamicWrapper option.globals can be a function.0.4.0 (Sep 24, 2019)
import("foo") => Promise.resolve(FOO).0.3.1 (Jun 6, 2019)
0.3.0 (Mar 25, 2019)
0.2.1 (Oct 2, 2018)
0.2.0 (Sep 12, 2018)
transform hook.0.1.0 (Aug 5, 2018)