rollup-plugin-external-globals
Advanced tools
Comparing version 0.11.0 to 0.12.0
@@ -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 ? { |
@@ -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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
17159
301
209
0
9