Socket
Socket
Sign inDemoInstall

rollup-plugin-external-globals

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-external-globals - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/import-to-globals.js

83

index.js
const MagicString = require("magic-string");
const {walk} = require("estree-walker");
const {attachScopes} = require("rollup-pluginutils");
const isReference = require("is-reference");
const {createFilter} = require("rollup-pluginutils");
function extractBindings(code, bindings, node) {
const source = node.source.value;
for (const spec of node.specifiers) {
if (spec.type === "ImportDefaultSpecifier") {
bindings.set(spec.local.name, [source, "default"]);
} else {
bindings.set(spec.local.name, [source, spec.imported.name]);
}
}
code.remove(node.start, node.end);
}
const importToGlobals = require("./lib/import-to-globals");
function writeBinding(code, node, [bindingSource, bindingName], globals) {
if (bindingName === "default") {
code.overwrite(node.start, node.end, globals[bindingSource]);
} else {
code.overwrite(node.start, node.end, `${globals[bindingSource]}.${bindingName}`);
}
}
function createPlugin(globals) {
function createPlugin(globals, {include, exclude} = {}) {
const filter = createFilter(include, exclude);
return {
name: "rollup-plugin-cjs-es",
options,
transformChunk
name: "rollup-plugin-external-globals",
transform
};
function options(options) {
options.external.push(...Object.keys(globals));
return options;
}
function transformChunk(code, options, chunk) {
if (chunk.dependencies.every(m => !globals.hasOwnProperty(m.id))) {
function transform(code, id) {
if (!filter(id)) {
return;
}
if (Object.keys(globals).every(id => !code.includes(id))) {
return;
}
const ast = this.parse(code);
let scope = attachScopes(ast, "scope");
const bindings = new Map;
code = new MagicString(code);
walk(ast, {
enter(node, parent) {
if (node.scope) {
scope = node.scope;
}
if (node.type === "ImportDeclaration" && globals.hasOwnProperty(node.source.value)) {
extractBindings(code, bindings, node);
this.skip();
} else if (
node.type === "Identifier" &&
isReference(node, parent) &&
!scope.contains(node.name) &&
bindings.has(node.name)
) {
writeBinding(code, node, bindings.get(node.name), globals);
}
},
leave(node) {
if (node.scope) {
// FIXME: this would break if someone called this.skip during enter().
scope = node.scope.parent;
}
}
const isTouched = importToGlobals({
ast,
code,
names: globals
});
if (bindings.size) {
return {
code: code.toString(),
map: code.generateMap()
};
}
return isTouched ? {
code: code.toString(),
map: code.generateMap()
} : undefined;
}

@@ -77,0 +32,0 @@ }

{
"name": "rollup-plugin-external-globals",
"version": "0.1.0",
"version": "0.2.0",
"description": "Transform external imports into global variables like output.globals.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -5,6 +5,6 @@ rollup-plugin-external-globals

[![Build Status](https://travis-ci.org/eight04/rollup-plugin-external-globals.svg?branch=master)](https://travis-ci.org/eight04/rollup-plugin-external-globals)
[![Coverage Status](https://coveralls.io/repos/github/eight04/rollup-plugin-external-globals/badge.svg?branch=master)](https://coveralls.io/github/eight04/rollup-plugin-external-globals?branch=master)
[![codecov](https://codecov.io/gh/eight04/rollup-plugin-external-globals/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/rollup-plugin-external-globals)
[![install size](https://packagephobia.now.sh/badge?p=rollup-plugin-external-globals)](https://packagephobia.now.sh/result?p=rollup-plugin-external-globals)
Transform external imports into global variables like `output.globals`.
Transform external imports into global variables like Rollup's `output.globals` option. See [rollup/rollup#2374](https://github.com/rollup/rollup/issues/2374)

@@ -52,7 +52,42 @@ Installation

API
----
This module exports a single function.
### createPlugin
```js
const plugin = createPlugin(
globals: Object,
{
include?: Array,
exclude?: Array
} = {}
);
```
`globals` is a `moduleId`/`variableName` map. For example, to map `jquery` module to `$`:
```js
{
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.
Changelog
---------
* 0.2.0 (Sep 12, 2018)
- Change: use `transform` hook.
- Add: rewrite conflicted variable names.
- Add: handle export from.
* 0.1.0 (Aug 5, 2018)
- Initial release.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc