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"));Note that 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,
{
include?: Array,
exclude?: Array
} = {}
);globals is a moduleId/variableName map. For example, to map jquery module to $:
{
jquery: "$"
}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.
Virtual modules are always transformed.
-
0.3.0 (Mar 25, 2019)
- Fix: temporary variable name conflicts.
- Breaking: transform virtual modules. Now the plugin transforms proxy modules generated by commonjs plugin.
- Bump dependencies.
-
0.2.1 (Oct 2, 2018)
- Fix: don't skip export statement.
-
0.2.0 (Sep 12, 2018)
- Change: use
transformhook. - Add: rewrite conflicted variable names.
- Add: handle export from.
- Change: use
-
0.1.0 (Aug 5, 2018)
- Initial release.