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.4.0 to 0.5.0

32

index.js

@@ -5,4 +5,24 @@ const MagicString = require("magic-string");

const importToGlobals = require("./lib/import-to-globals");
const defaultDynamicWrapper = id => `Promise.resolve(${id})`;
function createPlugin(globals, {include, exclude} = {}) {
function createPlugin(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);

@@ -13,10 +33,7 @@ return {

};
function transform(code, id) {
if (id[0] !== "\0" && !filter(id)) {
if ((id[0] !== "\0" && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
return;
}
if (Object.keys(globals).every(id => !code.includes(id))) {
return;
}
const ast = this.parse(code);

@@ -27,3 +44,4 @@ code = new MagicString(code);

code,
names: globals
getName,
getDynamicWrapper: dynamicWrapper
});

@@ -30,0 +48,0 @@ return isTouched ? {

@@ -5,11 +5,12 @@ const {walk} = require("estree-walker");

function analyzeImport(node, importBindings, code, names, globals) {
if (!names.hasOwnProperty(node.source.value)) {
function analyzeImport(node, importBindings, code, getName, globals) {
const name = node.source.value && getName(node.source.value);
if (!name) {
return false;
}
globals.add(names[node.source.value]);
globals.add(name);
for (const spec of node.specifiers) {
importBindings.set(spec.local.name, makeGlobalName(
spec.imported ? spec.imported.name : "default",
names[node.source.value]
name
));

@@ -51,9 +52,13 @@ }

}
function analyzeExportNamed(node, code, names, tempNames) {
if (node.declaration || !node.source || !names.hasOwnProperty(node.source.value)) {
function analyzeExportNamed(node, code, getName, tempNames) {
if (node.declaration || !node.source || !node.source.value) {
return false;
}
const name = getName(node.source.value);
if (!name) {
return false;
}
for (const spec of node.specifiers) {
const globalName = makeGlobalName(spec.local.name, names[node.source.value]);
const globalName = makeGlobalName(spec.local.name, name);
const legalName = /^[\w$]+$/.test(globalName) ? null : `_global_${makeLegalIdentifier(globalName)}`;

@@ -78,4 +83,4 @@ if (!legalName) {

function writeDynamicImport(code, node, globalName) {
code.overwrite(node.start, node.end, `Promise.resolve(${globalName})`);
function writeDynamicImport(code, node, content) {
code.overwrite(node.start, node.end, content);
}

@@ -92,3 +97,3 @@

function importToGlobals({ast, code, names}) {
function importToGlobals({ast, code, getName, getDynamicWrapper}) {
let scope = attachScopes(ast, "scope");

@@ -99,11 +104,11 @@ const bindings = new Map;

const tempNames = new Set;
for (const node of ast.body) {
if (node.type === "ImportDeclaration") {
isTouched = analyzeImport(node, bindings, code, names, globals) || isTouched;
isTouched = analyzeImport(node, bindings, code, getName, globals) || isTouched;
} else if (node.type === "ExportNamedDeclaration") {
isTouched = analyzeExportNamed(node, code, names, tempNames) || isTouched;
isTouched = analyzeExportNamed(node, code, getName, tempNames) || isTouched;
}
}
walk(ast, {

@@ -126,4 +131,6 @@ enter(node, parent) {

const source = getDynamicImportSource(node);
if (names.hasOwnProperty(source)) {
writeDynamicImport(code, node, names[source]);
const name = source && getName(source);
const dynamicName = name && getDynamicWrapper(name);
if (dynamicName) {
writeDynamicImport(code, node, dynamicName);
isTouched = true;

@@ -139,3 +146,3 @@ this.skip();

});
return isTouched;

@@ -142,0 +149,0 @@ }

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

@@ -28,18 +28,18 @@ "keywords": [

"devDependencies": {
"c8": "^5.0.4",
"c8": "^6.0.1",
"endent": "^1.3.0",
"eslint": "^5.16.0",
"mocha": "^6.2.0",
"rollup": "^1.21.4",
"eslint": "^6.7.2",
"mocha": "^6.2.2",
"rollup": "^1.27.9",
"tempdir-yaml": "^0.3.0"
},
"dependencies": {
"estree-walker": "^0.8.1",
"is-reference": "^1.1.3",
"magic-string": "^0.25.3",
"estree-walker": "^1.0.0",
"is-reference": "^1.1.4",
"magic-string": "^0.25.4",
"rollup-pluginutils": "^2.8.2"
},
"peerDependencies": {
"rollup": ">=0.60"
"rollup": "^1.27.9"
}
}

@@ -81,6 +81,7 @@ rollup-plugin-external-globals

const plugin = createPlugin(
globals: Object,
globals: Object | Function,
{
include?: Array,
exclude?: Array
exclude?: Array,
dynamicWrapper?: Function
} = {}

@@ -93,3 +94,3 @@ );

```js
{
const globals = {
jquery: "$"

@@ -99,2 +100,12 @@ }

or provide a function that takes the `moduleId` and returns the `variableName`.
```js
const globals = (id) => {
if (id === "jquery") {
return "$";
}
}
```
`include` is an array of glob patterns. If defined, only matched files would be transformed.

@@ -104,2 +115,10 @@

`dynamicWrapper` is used to specify dynamic imports. Below is the default.
```js
const dynamicWrapper = (id) => {
return `Promise.resolve(${id})`;
}
```
Virtual modules are always transformed.

@@ -110,2 +129,8 @@

* 0.5.0 (Dec 8, 2019)
- Add: `dynamicWrapper` option.
- Add: now `globals` can be a function.
- Bump dependencies/peer dependencies.
* 0.4.0 (Sep 24, 2019)

@@ -112,0 +137,0 @@

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