rollup-plugin-external-globals
Advanced tools
Comparing version 0.10.0 to 0.11.0
11
index.js
@@ -37,3 +37,12 @@ const MagicString = require("magic-string"); | ||
} | ||
const ast = this.parse(code); | ||
let ast; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.debug({ | ||
message: `Failed to parse code, skip ${id}`, | ||
cause: err | ||
}); | ||
return; | ||
} | ||
code = new MagicString(code); | ||
@@ -40,0 +49,0 @@ const isTouched = await importToGlobals({ |
@@ -64,2 +64,6 @@ const {attachScopes, makeLegalIdentifier} = require("@rollup/pluginutils"); | ||
parent.value.isOverwritten = true; | ||
} else if (parent.type === "ExportSpecifier" && parent.local.start === parent.exported.start) { | ||
code.appendLeft(node.start, `${name} as `); | ||
parent.local.isOverwritten = true; | ||
parent.exported.isOverwritten = true; | ||
} else { | ||
@@ -105,2 +109,23 @@ code.overwrite(node.start, node.end, name, {contentOnly: true}); | ||
// export left hand analyzer | ||
class ExportLeftHand { | ||
constructor() { | ||
this.inDeclaration = false; | ||
this.inLeftHand = false; | ||
} | ||
enter(node, parent) { | ||
if (parent && parent.type === "Program") { | ||
this.inDeclaration = node.type === "ExportNamedDeclaration"; | ||
} | ||
if (this.inDeclaration && parent.type === "VariableDeclarator" && parent.id === node) { | ||
this.inLeftHand = true; | ||
} | ||
} | ||
leave(node, parent) { | ||
if (this.inLeftHand && parent.type === "VariableDeclarator") { | ||
this.inLeftHand = false; | ||
} | ||
} | ||
} | ||
async function importToGlobals({ast, code, getName, getDynamicWrapper}) { | ||
@@ -113,2 +138,3 @@ await prepare(); | ||
const tempNames = new Set; | ||
const exportLeftHand = new ExportLeftHand; | ||
@@ -126,2 +152,3 @@ for (const node of ast.body) { | ||
enter(node, parent) { | ||
exportLeftHand.enter(node, parent); | ||
if (parent && parent.type === "Program") { | ||
@@ -145,3 +172,8 @@ topStatement = node; | ||
} else if (globals.has(node.name) && scope.contains(node.name)) { | ||
// conflict with local variable | ||
writeIdentifier(code, node, parent, `_local_${node.name}`); | ||
if (exportLeftHand.inLeftHand) { | ||
code.appendLeft(topStatement.end, `export {_local_${node.name} as ${node.name}};\n`); | ||
code.remove(topStatement.start, topStatement.declaration.start); | ||
} | ||
} | ||
@@ -158,3 +190,4 @@ } | ||
}, | ||
leave(node) { | ||
leave(node, parent) { | ||
exportLeftHand.leave(node, parent); | ||
if (node.scope) { | ||
@@ -161,0 +194,0 @@ scope = node.scope.parent; |
{ | ||
"name": "rollup-plugin-external-globals", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "Transform external imports into global variables like output.globals.", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "eslint **/*.js --cache && c8 --reporter lcov mocha", | ||
"test": "eslint . --cache && c8 --reporter lcov mocha", | ||
"preversion": "npm test", | ||
@@ -37,7 +37,7 @@ "postversion": "git push --follow-tags && npm publish" | ||
"devDependencies": { | ||
"c8": "^9.1.0", | ||
"c8": "^10.1.2", | ||
"endent": "^2.1.0", | ||
"eslint": "^8.56.0", | ||
"mocha": "^10.2.0", | ||
"rollup": "^4.9.6", | ||
"eslint": "^9.5.0", | ||
"mocha": "^10.5.2", | ||
"rollup": "^4.18.0", | ||
"tempdir-yaml": "^0.3.0" | ||
@@ -49,3 +49,3 @@ }, | ||
"is-reference": "^3.0.2", | ||
"magic-string": "^0.30.5" | ||
"magic-string": "^0.30.10" | ||
}, | ||
@@ -52,0 +52,0 @@ "peerDependencies": { |
@@ -125,2 +125,7 @@ rollup-plugin-external-globals | ||
* 0.11.0 (Jun 27, 2024) | ||
- Fix: local variable conflict in export declaration. | ||
- Change: don't throw on parse error. | ||
* 0.10.0 (Apr 5, 2024) | ||
@@ -127,0 +132,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
15506
266
200
Updatedmagic-string@^0.30.10