rollup-plugin-external-globals
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -76,2 +76,15 @@ const {walk} = require("estree-walker"); | ||
function writeDynamicImport(code, node, globalName) { | ||
code.overwrite(node.start, node.end, `Promise.resolve(${globalName})`); | ||
} | ||
function getDynamicImportSource(node) { | ||
if (node.type === "ImportExpression") { | ||
return node.source.value; | ||
} | ||
if (node.type === "CallExpression" && node.callee.type === "Import") { | ||
return node.arguments[0].value; | ||
} | ||
} | ||
function importToGlobals({ast, code, names}) { | ||
@@ -94,4 +107,5 @@ let scope = attachScopes(ast, "scope"); | ||
enter(node, parent) { | ||
if (/^import/i.test(node.type)) { | ||
if (/^importdec/i.test(node.type)) { | ||
this.skip(); | ||
return; | ||
} | ||
@@ -108,2 +122,8 @@ if (node.scope) { | ||
} | ||
const source = getDynamicImportSource(node); | ||
if (names.hasOwnProperty(source)) { | ||
writeDynamicImport(code, node, names[source]); | ||
isTouched = true; | ||
this.skip(); | ||
} | ||
}, | ||
@@ -110,0 +130,0 @@ leave(node) { |
{ | ||
"name": "rollup-plugin-external-globals", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Transform external imports into global variables like output.globals.", | ||
@@ -28,14 +28,14 @@ "keywords": [ | ||
"devDependencies": { | ||
"c8": "^5.0.1", | ||
"c8": "^5.0.4", | ||
"endent": "^1.3.0", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4", | ||
"rollup": "^1.14.0", | ||
"mocha": "^6.2.0", | ||
"rollup": "^1.21.4", | ||
"tempdir-yaml": "^0.3.0" | ||
}, | ||
"dependencies": { | ||
"estree-walker": "^0.6.1", | ||
"is-reference": "^1.1.2", | ||
"magic-string": "^0.25.2", | ||
"rollup-pluginutils": "^2.8.1" | ||
"estree-walker": "^0.8.1", | ||
"is-reference": "^1.1.3", | ||
"magic-string": "^0.25.3", | ||
"rollup-pluginutils": "^2.8.2" | ||
}, | ||
@@ -42,0 +42,0 @@ "peerDependencies": { |
@@ -51,4 +51,23 @@ rollup-plugin-external-globals | ||
Note that this plugin only works with import/export syntax. If you are using a module loader transformer e.g. `rollup-plugin-commonjs`, you have to put this plugin *after* the transformer plugin. | ||
It also transforms dynamic import: | ||
```js | ||
import("jquery") | ||
.then($ => { | ||
$ = $.default || $; | ||
console.log($(".test")); | ||
}); | ||
// transformed | ||
Promise.resolve($) | ||
.then($ => { | ||
$ = $.default || $; | ||
console.log($(".test")); | ||
}); | ||
``` | ||
> **Note:** when using dynamic import, you should notice that in ES module, the resolved object is aways a module namespace, but the global variable might be not. | ||
> **Note:** this plugin only works with import/export syntax. If you are using a module loader transformer e.g. `rollup-plugin-commonjs`, you have to put this plugin *after* the transformer plugin. | ||
API | ||
@@ -88,2 +107,6 @@ ---- | ||
* 0.4.0 (Sep 24, 2019) | ||
- Add: transform dynamic imports i.e. `import("foo")` => `Promise.resolve(FOO)`. | ||
* 0.3.1 (Jun 6, 2019) | ||
@@ -90,0 +113,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
10334
155
134
+ Addedestree-walker@0.8.1(transitive)
Updatedestree-walker@^0.8.1
Updatedis-reference@^1.1.3
Updatedmagic-string@^0.25.3
Updatedrollup-pluginutils@^2.8.2