Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rollup-plugin-external-globals

Package Overview
Dependencies
Maintainers
0
Versions
20
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.11.0 to 0.12.0

5

index.d.ts

@@ -25,2 +25,7 @@ import type { Plugin } from "rollup";

dynamicWrapper?: (variableName: VariableName) => string;
/**
* [constBindings] is used to decide whether to use `const` to declare variables. Default is `false`
*/
constBindings?: boolean;
};

@@ -27,0 +32,0 @@

30

index.js

@@ -7,3 +7,7 @@ const MagicString = require("magic-string");

function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper} = {}) {
function isVirtualModule(id) {
return id.startsWith("\0");
}
function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper, constBindings = false} = {}) {
if (!globals) {

@@ -28,10 +32,27 @@ throw new TypeError("Missing mandatory option 'globals'");

}
async function resolveId(importee, _, options) {
if (isVirtualModule(importee) || options.isEntry) return null;
const globalName = getName(importee)
return globalName ? false : null;
}
const filter = createFilter(include, exclude);
return {
name: "rollup-plugin-external-globals",
options,
transform
};
async function options(rawOptions) {
const plugins = Array.isArray(rawOptions.plugins)
? [...rawOptions.plugins]
: rawOptions.plugins
? [rawOptions.plugins]
: [];
plugins.unshift({
name: 'rollup-plugin-external-globals--resolver',
resolveId
});
return { ...rawOptions, plugins };
}
async function transform(code, id) {
if ((id[0] !== "\0" && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
if ((!isVirtualModule(id) && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
return;

@@ -54,3 +75,4 @@ }

getName,
getDynamicWrapper: dynamicWrapper
getDynamicWrapper: dynamicWrapper,
constBindings
});

@@ -57,0 +79,0 @@ return isTouched ? {

24

lib/import-to-globals.js

@@ -35,3 +35,3 @@ const {attachScopes, makeLegalIdentifier} = require("@rollup/pluginutils");

function writeSpecLocal(code, root, spec, name, tempNames) {
function writeSpecLocal(code, root, spec, name, tempNames, constBindings) {
if (spec.isOverwritten) return;

@@ -42,3 +42,3 @@ // we always need an extra assignment for named export statement

if (!tempNames.has(localName)) {
code.appendRight(root.start, `const ${localName} = ${name};\n`);
code.appendRight(root.start, `${constBindings ? "const" : "var"} ${localName} = ${name};\n`);
tempNames.add(localName);

@@ -77,3 +77,3 @@ }

function analyzeExportNamed(node, code, getName, tempNames) {
function analyzeExportNamed(node, code, getName, tempNames, constBindings) {
if (node.declaration || !node.source || !node.source.value) {

@@ -88,3 +88,3 @@ return false;

const globalName = makeGlobalName(spec.local.name, name);
writeSpecLocal(code, node, spec, globalName, tempNames);
writeSpecLocal(code, node, spec, globalName, tempNames, constBindings);
}

@@ -99,2 +99,10 @@ if (node.specifiers.length) {

function analyzeExportAll(node, code, getName) {
const name = getName(node.source.value);
if (!name) {
return;
}
throw new Error("Cannot export all properties from an external variable");
}
function writeDynamicImport(code, node, content) {

@@ -134,3 +142,3 @@ code.overwrite(node.start, node.end, content);

async function importToGlobals({ast, code, getName, getDynamicWrapper}) {
async function importToGlobals({ast, code, getName, getDynamicWrapper, constBindings}) {
await prepare();

@@ -148,3 +156,5 @@ let scope = attachScopes(ast, "scope");

} else if (node.type === "ExportNamedDeclaration") {
isTouched = analyzeExportNamed(node, code, getName, tempNames) || isTouched;
isTouched = analyzeExportNamed(node, code, getName, tempNames, constBindings) || isTouched;
} else if (node.type === "ExportAllDeclaration") {
analyzeExportAll(node, code, getName, tempNames, constBindings);
}

@@ -170,3 +180,3 @@ }

if (parent.type === "ExportSpecifier") {
writeSpecLocal(code, topStatement, parent, bindings.get(node.name), tempNames);
writeSpecLocal(code, topStatement, parent, bindings.get(node.name), tempNames, constBindings);
} else {

@@ -173,0 +183,0 @@ writeIdentifier(code, node, parent, bindings.get(node.name));

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

@@ -32,9 +32,15 @@ "keywords": [

},
"repository": "eight04/rollup-plugin-external-globals",
"repository": {
"type": "git",
"url": "git+https://github.com/eight04/rollup-plugin-external-globals.git"
},
"author": "eight04 <eight04@gmail.com>",
"license": "MIT",
"devDependencies": {
"@eslint/js": "^9.9.0",
"@rollup/plugin-commonjs": "^26.0.1",
"c8": "^10.1.2",
"endent": "^2.1.0",
"eslint": "^9.5.0",
"globals": "^15.9.0",
"mocha": "^10.5.2",

@@ -41,0 +47,0 @@ "rollup": "^4.18.0",

@@ -85,3 +85,4 @@ rollup-plugin-external-globals

exclude?: Array,
dynamicWrapper?: Function
dynamicWrapper?: Function,
constBindings?: Boolean
} = {}

@@ -123,5 +124,13 @@ );

`constBindings` is a boolean. If true, the plugin will use `const` instead of `var` to declare the variable. This usually happens when you try to re-export the global variable. Default is false.
Changelog
---------
* 0.12.0 (Aug 11, 2024)
- Change: throw on export all declaration.
- Change: define variables with `var`, add `constBindings` option to use `const` instead.
- Change: resolve identifiers as external.
* 0.11.0 (Jun 27, 2024)

@@ -128,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