rollup-plugin-inject
Advanced tools
Comparing version 2.2.0 to 3.0.0
# rollup-plugin-inject | ||
## 3.0.0 | ||
* Remove node v6 from support | ||
* Use modern js | ||
## 2.1.0 | ||
@@ -4,0 +9,0 @@ |
@@ -10,190 +10,192 @@ 'use strict'; | ||
function escape (str) { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); | ||
} | ||
const escape = (str) => { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
}; | ||
function isReference (node, parent) { | ||
if (node.type === 'MemberExpression') { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
const isReference = (node, parent) => { | ||
if (node.type === "MemberExpression") { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
if (node.type === 'Identifier') { | ||
// TODO is this right? | ||
if (parent.type === 'MemberExpression') | ||
{ return parent.computed || node === parent.object; } | ||
if (node.type === "Identifier") { | ||
// TODO is this right? | ||
if (parent.type === "MemberExpression") return parent.computed || node === parent.object; | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === 'Property' && node !== parent.value) { return false; } | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === "Property" && node !== parent.value) return false; | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === 'MethodDefinition') { return false; } | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === "MethodDefinition") return false; | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === 'ExportSpecifier' && node !== parent.local) { return; } | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === "ExportSpecifier" && node !== parent.local) return; | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
function flatten (node) { | ||
var name; | ||
var parts = []; | ||
const flatten = (node) => { | ||
const parts = []; | ||
while (node.type === 'MemberExpression') { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
while (node.type === "MemberExpression") { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
name = node.name; | ||
parts.unshift(name); | ||
const name = node.name; | ||
parts.unshift(name); | ||
return { name: name, keypath: parts.join('.') }; | ||
} | ||
return { name, keypath: parts.join(".") }; | ||
}; | ||
function inject (options) { | ||
if (!options) { throw new Error('Missing options'); } | ||
function inject(options) { | ||
if (!options) throw new Error("Missing options"); | ||
var filter = rollupPluginutils.createFilter(options.include, options.exclude); | ||
const filter = rollupPluginutils.createFilter(options.include, options.exclude); | ||
var modules; | ||
let modules = options.modules; | ||
if (options.modules) { | ||
modules = options.modules; | ||
} else { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
if (!modules) { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
// Fix paths on Windows | ||
if (path.sep !== '/') { | ||
Object.keys(modules).forEach(function (key) { | ||
var module = modules[key]; | ||
const modulesMap = new Map(Object.entries(modules)); | ||
modules[key] = Array.isArray(module) | ||
? [module[0].split(path.sep).join('/'), module[1]] | ||
: module.split(path.sep).join('/'); | ||
}); | ||
} | ||
// Fix paths on Windows | ||
if (path.sep !== "/") { | ||
modulesMap.forEach((mod, key) => { | ||
modulesMap.set(key, Array.isArray(mod) | ||
? [mod[0].split(path.sep).join("/"), mod[1]] | ||
: mod.split(path.sep).join("/")); | ||
}); | ||
} | ||
var firstpass = new RegExp( | ||
("(?:" + (Object.keys(modules) | ||
.map(escape) | ||
.join('|')) + ")"), | ||
'g' | ||
); | ||
var sourceMap = options.sourceMap !== false; | ||
const firstpass = new RegExp( | ||
`(?:${Array.from(modulesMap.keys()) | ||
.map(escape) | ||
.join("|")})`, | ||
"g" | ||
); | ||
const sourceMap = options.sourceMap !== false; | ||
return { | ||
name: 'inject', | ||
return { | ||
name: "inject", | ||
transform: function transform (code, id) { | ||
if (!filter(id)) { return null; } | ||
if (code.search(firstpass) == -1) { return null; } | ||
transform(code, id) { | ||
if (!filter(id)) return null; | ||
if (code.search(firstpass) === -1) return null; | ||
if (path.sep !== '/') { id = id.split(path.sep).join('/'); } | ||
if (path.sep !== "/") id = id.split(path.sep).join("/"); | ||
var ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: 'PARSE_ERROR', | ||
message: | ||
("rollup-plugin-inject: failed to parse " + id + ". Consider restricting the plugin to particular files via options.include") | ||
}); | ||
} | ||
if (!ast) { return null; } | ||
let ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: "PARSE_ERROR", | ||
message: `rollup-plugin-inject: failed to parse ${id}. Consider restricting the plugin to particular files via options.include` | ||
}); | ||
} | ||
if (!ast) { | ||
return null; | ||
} | ||
// analyse scopes | ||
var scope = rollupPluginutils.attachScopes(ast, 'scope'); | ||
// analyse scopes | ||
let scope = rollupPluginutils.attachScopes(ast, "scope"); | ||
var imports = {}; | ||
ast.body.forEach(function (node) { | ||
if (node.type === 'ImportDeclaration') { | ||
node.specifiers.forEach(function (specifier) { | ||
imports[specifier.local.name] = true; | ||
}); | ||
} | ||
}); | ||
const imports = new Set(); | ||
ast.body.forEach(node => { | ||
if (node.type === "ImportDeclaration") { | ||
node.specifiers.forEach(specifier => { | ||
imports.add(specifier.local.name); | ||
}); | ||
} | ||
}); | ||
var magicString = new MagicString(code); | ||
const magicString = new MagicString(code); | ||
var newImports = {}; | ||
const newImports = new Map(); | ||
function handleReference (node, name, keypath) { | ||
if (keypath in modules && !scope.contains(name) && !imports[name]) { | ||
var module = modules[keypath]; | ||
if (typeof module === 'string') { module = [module, 'default']; } | ||
function handleReference(node, name, keypath) { | ||
let mod = modulesMap.get(keypath); | ||
if (mod && !imports.has(name) && !scope.contains(name)) { | ||
if (typeof mod === "string") mod = [mod, "default"]; | ||
// prevent module from importing itself | ||
if (module[0] === id) { return; } | ||
// prevent module from importing itself | ||
if (mod[0] === id) return; | ||
var hash = keypath + ":" + (module[0]) + ":" + (module[1]); | ||
const hash = `${keypath}:${mod[0]}:${mod[1]}`; | ||
var importLocalName = | ||
name === keypath ? name : rollupPluginutils.makeLegalIdentifier(("$inject_" + keypath)); | ||
const importLocalName = | ||
name === keypath ? name : rollupPluginutils.makeLegalIdentifier(`$inject_${keypath}`); | ||
if (!newImports[hash]) { | ||
if (module[1] === '*') { | ||
newImports[hash] = "import * as " + importLocalName + " from '" + (module[0]) + "';"; | ||
} else { | ||
newImports[hash] = "import { " + (module[1]) + " as " + importLocalName + " } from '" + (module[0]) + "';"; | ||
} | ||
} | ||
if (!newImports.has(hash)) { | ||
if (mod[1] === "*") { | ||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`); | ||
} else { | ||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${ | ||
mod[0] | ||
}';`); | ||
} | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
estreeWalker.walk(ast, { | ||
enter: function enter (node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
estreeWalker.walk(ast, { | ||
enter(node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
if (node.scope) { scope = node.scope; } | ||
if (node.scope) scope = node.scope; | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === 'Property' && node.shorthand) { | ||
var name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === "Property" && node.shorthand) { | ||
const name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
if (isReference(node, parent)) { | ||
var ref = flatten(node); | ||
var name$1 = ref.name; | ||
var keypath = ref.keypath; | ||
var handled = handleReference(node, name$1, keypath); | ||
if (handled) { return this.skip(); } | ||
} | ||
}, | ||
leave: function leave (node) { | ||
if (node.scope) { scope = scope.parent; } | ||
} | ||
}); | ||
if (isReference(node, parent)) { | ||
const { name, keypath } = flatten(node); | ||
const handled = handleReference(node, name, keypath); | ||
if (handled) return this.skip(); | ||
} | ||
}, | ||
leave(node) { | ||
if (node.scope) scope = scope.parent; | ||
} | ||
}); | ||
var keys = Object.keys(newImports); | ||
if (!keys.length) { return null; } | ||
if (newImports.size === 0) { | ||
return { | ||
code, | ||
ast | ||
}; | ||
} | ||
const importBlock = Array.from(newImports.values()) | ||
.join("\n\n"); | ||
var importBlock = keys.map(function (hash) { return newImports[hash]; }).join('\n\n'); | ||
magicString.prepend(importBlock + '\n\n'); | ||
magicString.prepend(importBlock + "\n\n"); | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
} | ||
module.exports = inject; |
@@ -1,2 +0,2 @@ | ||
import { attachScopes, createFilter, makeLegalIdentifier } from 'rollup-pluginutils'; | ||
import { createFilter, attachScopes, makeLegalIdentifier } from 'rollup-pluginutils'; | ||
import { sep } from 'path'; | ||
@@ -6,190 +6,192 @@ import { walk } from 'estree-walker'; | ||
function escape (str) { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); | ||
} | ||
const escape = (str) => { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
}; | ||
function isReference (node, parent) { | ||
if (node.type === 'MemberExpression') { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
const isReference = (node, parent) => { | ||
if (node.type === "MemberExpression") { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
if (node.type === 'Identifier') { | ||
// TODO is this right? | ||
if (parent.type === 'MemberExpression') | ||
{ return parent.computed || node === parent.object; } | ||
if (node.type === "Identifier") { | ||
// TODO is this right? | ||
if (parent.type === "MemberExpression") return parent.computed || node === parent.object; | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === 'Property' && node !== parent.value) { return false; } | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === "Property" && node !== parent.value) return false; | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === 'MethodDefinition') { return false; } | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === "MethodDefinition") return false; | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === 'ExportSpecifier' && node !== parent.local) { return; } | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === "ExportSpecifier" && node !== parent.local) return; | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
function flatten (node) { | ||
var name; | ||
var parts = []; | ||
const flatten = (node) => { | ||
const parts = []; | ||
while (node.type === 'MemberExpression') { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
while (node.type === "MemberExpression") { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
name = node.name; | ||
parts.unshift(name); | ||
const name = node.name; | ||
parts.unshift(name); | ||
return { name: name, keypath: parts.join('.') }; | ||
} | ||
return { name, keypath: parts.join(".") }; | ||
}; | ||
function inject (options) { | ||
if (!options) { throw new Error('Missing options'); } | ||
function inject(options) { | ||
if (!options) throw new Error("Missing options"); | ||
var filter = createFilter(options.include, options.exclude); | ||
const filter = createFilter(options.include, options.exclude); | ||
var modules; | ||
let modules = options.modules; | ||
if (options.modules) { | ||
modules = options.modules; | ||
} else { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
if (!modules) { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
// Fix paths on Windows | ||
if (sep !== '/') { | ||
Object.keys(modules).forEach(function (key) { | ||
var module = modules[key]; | ||
const modulesMap = new Map(Object.entries(modules)); | ||
modules[key] = Array.isArray(module) | ||
? [module[0].split(sep).join('/'), module[1]] | ||
: module.split(sep).join('/'); | ||
}); | ||
} | ||
// Fix paths on Windows | ||
if (sep !== "/") { | ||
modulesMap.forEach((mod, key) => { | ||
modulesMap.set(key, Array.isArray(mod) | ||
? [mod[0].split(sep).join("/"), mod[1]] | ||
: mod.split(sep).join("/")); | ||
}); | ||
} | ||
var firstpass = new RegExp( | ||
("(?:" + (Object.keys(modules) | ||
.map(escape) | ||
.join('|')) + ")"), | ||
'g' | ||
); | ||
var sourceMap = options.sourceMap !== false; | ||
const firstpass = new RegExp( | ||
`(?:${Array.from(modulesMap.keys()) | ||
.map(escape) | ||
.join("|")})`, | ||
"g" | ||
); | ||
const sourceMap = options.sourceMap !== false; | ||
return { | ||
name: 'inject', | ||
return { | ||
name: "inject", | ||
transform: function transform (code, id) { | ||
if (!filter(id)) { return null; } | ||
if (code.search(firstpass) == -1) { return null; } | ||
transform(code, id) { | ||
if (!filter(id)) return null; | ||
if (code.search(firstpass) === -1) return null; | ||
if (sep !== '/') { id = id.split(sep).join('/'); } | ||
if (sep !== "/") id = id.split(sep).join("/"); | ||
var ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: 'PARSE_ERROR', | ||
message: | ||
("rollup-plugin-inject: failed to parse " + id + ". Consider restricting the plugin to particular files via options.include") | ||
}); | ||
} | ||
if (!ast) { return null; } | ||
let ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: "PARSE_ERROR", | ||
message: `rollup-plugin-inject: failed to parse ${id}. Consider restricting the plugin to particular files via options.include` | ||
}); | ||
} | ||
if (!ast) { | ||
return null; | ||
} | ||
// analyse scopes | ||
var scope = attachScopes(ast, 'scope'); | ||
// analyse scopes | ||
let scope = attachScopes(ast, "scope"); | ||
var imports = {}; | ||
ast.body.forEach(function (node) { | ||
if (node.type === 'ImportDeclaration') { | ||
node.specifiers.forEach(function (specifier) { | ||
imports[specifier.local.name] = true; | ||
}); | ||
} | ||
}); | ||
const imports = new Set(); | ||
ast.body.forEach(node => { | ||
if (node.type === "ImportDeclaration") { | ||
node.specifiers.forEach(specifier => { | ||
imports.add(specifier.local.name); | ||
}); | ||
} | ||
}); | ||
var magicString = new MagicString(code); | ||
const magicString = new MagicString(code); | ||
var newImports = {}; | ||
const newImports = new Map(); | ||
function handleReference (node, name, keypath) { | ||
if (keypath in modules && !scope.contains(name) && !imports[name]) { | ||
var module = modules[keypath]; | ||
if (typeof module === 'string') { module = [module, 'default']; } | ||
function handleReference(node, name, keypath) { | ||
let mod = modulesMap.get(keypath); | ||
if (mod && !imports.has(name) && !scope.contains(name)) { | ||
if (typeof mod === "string") mod = [mod, "default"]; | ||
// prevent module from importing itself | ||
if (module[0] === id) { return; } | ||
// prevent module from importing itself | ||
if (mod[0] === id) return; | ||
var hash = keypath + ":" + (module[0]) + ":" + (module[1]); | ||
const hash = `${keypath}:${mod[0]}:${mod[1]}`; | ||
var importLocalName = | ||
name === keypath ? name : makeLegalIdentifier(("$inject_" + keypath)); | ||
const importLocalName = | ||
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`); | ||
if (!newImports[hash]) { | ||
if (module[1] === '*') { | ||
newImports[hash] = "import * as " + importLocalName + " from '" + (module[0]) + "';"; | ||
} else { | ||
newImports[hash] = "import { " + (module[1]) + " as " + importLocalName + " } from '" + (module[0]) + "';"; | ||
} | ||
} | ||
if (!newImports.has(hash)) { | ||
if (mod[1] === "*") { | ||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`); | ||
} else { | ||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${ | ||
mod[0] | ||
}';`); | ||
} | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
walk(ast, { | ||
enter: function enter (node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
walk(ast, { | ||
enter(node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
if (node.scope) { scope = node.scope; } | ||
if (node.scope) scope = node.scope; | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === 'Property' && node.shorthand) { | ||
var name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === "Property" && node.shorthand) { | ||
const name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
if (isReference(node, parent)) { | ||
var ref = flatten(node); | ||
var name$1 = ref.name; | ||
var keypath = ref.keypath; | ||
var handled = handleReference(node, name$1, keypath); | ||
if (handled) { return this.skip(); } | ||
} | ||
}, | ||
leave: function leave (node) { | ||
if (node.scope) { scope = scope.parent; } | ||
} | ||
}); | ||
if (isReference(node, parent)) { | ||
const { name, keypath } = flatten(node); | ||
const handled = handleReference(node, name, keypath); | ||
if (handled) return this.skip(); | ||
} | ||
}, | ||
leave(node) { | ||
if (node.scope) scope = scope.parent; | ||
} | ||
}); | ||
var keys = Object.keys(newImports); | ||
if (!keys.length) { return null; } | ||
if (newImports.size === 0) { | ||
return { | ||
code, | ||
ast | ||
}; | ||
} | ||
const importBlock = Array.from(newImports.values()) | ||
.join("\n\n"); | ||
var importBlock = keys.map(function (hash) { return newImports[hash]; }).join('\n\n'); | ||
magicString.prepend(importBlock + '\n\n'); | ||
magicString.prepend(importBlock + "\n\n"); | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
} | ||
export default inject; |
{ | ||
"name": "rollup-plugin-inject", | ||
"description": "Scan modules for global variables and inject `import` statements where necessary", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"devDependencies": { | ||
"eslint": "^5.2.0", | ||
"mocha": "^5.2.0", | ||
"prettier": "^1.13.7", | ||
"rollup": "^0.63.4", | ||
"rollup-plugin-buble": "^0.19.2", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.0.0", | ||
"prettier": "^1.18.2", | ||
"rollup": "^1.15.1", | ||
"shx": "^0.3.2" | ||
@@ -31,5 +30,5 @@ }, | ||
"dependencies": { | ||
"estree-walker": "^0.5.0", | ||
"magic-string": "^0.25.0", | ||
"rollup-pluginutils": "^2.0.1" | ||
"estree-walker": "^0.6.1", | ||
"magic-string": "^0.25.2", | ||
"rollup-pluginutils": "^2.8.1" | ||
}, | ||
@@ -36,0 +35,0 @@ "repository": { |
310
src/index.js
@@ -1,199 +0,195 @@ | ||
import { | ||
attachScopes, | ||
createFilter, | ||
makeLegalIdentifier | ||
} from 'rollup-pluginutils'; | ||
import { sep } from 'path'; | ||
import { walk } from 'estree-walker'; | ||
import { attachScopes, createFilter, makeLegalIdentifier } from "rollup-pluginutils"; | ||
import { sep } from "path"; | ||
import { walk } from "estree-walker"; | ||
import MagicString from 'magic-string'; | ||
import MagicString from "magic-string"; | ||
function escape (str) { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); | ||
} | ||
const escape = (str) => { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
}; | ||
function isReference (node, parent) { | ||
if (node.type === 'MemberExpression') { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
const isReference = (node, parent) => { | ||
if (node.type === "MemberExpression") { | ||
return !node.computed && isReference(node.object, node); | ||
} | ||
if (node.type === 'Identifier') { | ||
// TODO is this right? | ||
if (parent.type === 'MemberExpression') | ||
return parent.computed || node === parent.object; | ||
if (node.type === "Identifier") { | ||
// TODO is this right? | ||
if (parent.type === "MemberExpression") return parent.computed || node === parent.object; | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === 'Property' && node !== parent.value) return false; | ||
// disregard the `bar` in { bar: foo } | ||
if (parent.type === "Property" && node !== parent.value) return false; | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === 'MethodDefinition') return false; | ||
// disregard the `bar` in `class Foo { bar () {...} }` | ||
if (parent.type === "MethodDefinition") return false; | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === 'ExportSpecifier' && node !== parent.local) return; | ||
// disregard the `bar` in `export { foo as bar }` | ||
if (parent.type === "ExportSpecifier" && node !== parent.local) return; | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
function flatten (node) { | ||
let name; | ||
let parts = []; | ||
const flatten = (node) => { | ||
const parts = []; | ||
while (node.type === 'MemberExpression') { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
while (node.type === "MemberExpression") { | ||
parts.unshift(node.property.name); | ||
node = node.object; | ||
} | ||
name = node.name; | ||
parts.unshift(name); | ||
const name = node.name; | ||
parts.unshift(name); | ||
return { name, keypath: parts.join('.') }; | ||
} | ||
return { name, keypath: parts.join(".") }; | ||
}; | ||
export default function inject (options) { | ||
if (!options) throw new Error('Missing options'); | ||
export default function inject(options) { | ||
if (!options) throw new Error("Missing options"); | ||
const filter = createFilter(options.include, options.exclude); | ||
const filter = createFilter(options.include, options.exclude); | ||
let modules; | ||
let modules = options.modules; | ||
if (options.modules) { | ||
modules = options.modules; | ||
} else { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
if (!modules) { | ||
modules = Object.assign({}, options); | ||
delete modules.include; | ||
delete modules.exclude; | ||
} | ||
// Fix paths on Windows | ||
if (sep !== '/') { | ||
Object.keys(modules).forEach(key => { | ||
const module = modules[key]; | ||
const modulesMap = new Map(Object.entries(modules)); | ||
modules[key] = Array.isArray(module) | ||
? [module[0].split(sep).join('/'), module[1]] | ||
: module.split(sep).join('/'); | ||
}); | ||
} | ||
// Fix paths on Windows | ||
if (sep !== "/") { | ||
modulesMap.forEach((mod, key) => { | ||
modulesMap.set(key, Array.isArray(mod) | ||
? [mod[0].split(sep).join("/"), mod[1]] | ||
: mod.split(sep).join("/")); | ||
}); | ||
} | ||
const firstpass = new RegExp( | ||
`(?:${Object.keys(modules) | ||
.map(escape) | ||
.join('|')})`, | ||
'g' | ||
); | ||
const sourceMap = options.sourceMap !== false; | ||
const firstpass = new RegExp( | ||
`(?:${Array.from(modulesMap.keys()) | ||
.map(escape) | ||
.join("|")})`, | ||
"g" | ||
); | ||
const sourceMap = options.sourceMap !== false; | ||
return { | ||
name: 'inject', | ||
return { | ||
name: "inject", | ||
transform (code, id) { | ||
if (!filter(id)) return null; | ||
if (code.search(firstpass) == -1) return null; | ||
transform(code, id) { | ||
if (!filter(id)) return null; | ||
if (code.search(firstpass) === -1) return null; | ||
if (sep !== '/') id = id.split(sep).join('/'); | ||
if (sep !== "/") id = id.split(sep).join("/"); | ||
let ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: 'PARSE_ERROR', | ||
message: | ||
`rollup-plugin-inject: failed to parse ${id}. Consider restricting the plugin to particular files via options.include` | ||
}); | ||
} | ||
if (!ast) return null; | ||
let ast = null; | ||
try { | ||
ast = this.parse(code); | ||
} catch (err) { | ||
this.warn({ | ||
code: "PARSE_ERROR", | ||
message: `rollup-plugin-inject: failed to parse ${id}. Consider restricting the plugin to particular files via options.include` | ||
}); | ||
} | ||
if (!ast) { | ||
return null; | ||
} | ||
// analyse scopes | ||
let scope = attachScopes(ast, 'scope'); | ||
// analyse scopes | ||
let scope = attachScopes(ast, "scope"); | ||
let imports = {}; | ||
ast.body.forEach(node => { | ||
if (node.type === 'ImportDeclaration') { | ||
node.specifiers.forEach(specifier => { | ||
imports[specifier.local.name] = true; | ||
}); | ||
} | ||
}); | ||
const imports = new Set(); | ||
ast.body.forEach(node => { | ||
if (node.type === "ImportDeclaration") { | ||
node.specifiers.forEach(specifier => { | ||
imports.add(specifier.local.name); | ||
}); | ||
} | ||
}); | ||
const magicString = new MagicString(code); | ||
const magicString = new MagicString(code); | ||
let newImports = {}; | ||
const newImports = new Map(); | ||
function handleReference (node, name, keypath) { | ||
if (keypath in modules && !scope.contains(name) && !imports[name]) { | ||
let module = modules[keypath]; | ||
if (typeof module === 'string') module = [module, 'default']; | ||
function handleReference(node, name, keypath) { | ||
let mod = modulesMap.get(keypath); | ||
if (mod && !imports.has(name) && !scope.contains(name)) { | ||
if (typeof mod === "string") mod = [mod, "default"]; | ||
// prevent module from importing itself | ||
if (module[0] === id) return; | ||
// prevent module from importing itself | ||
if (mod[0] === id) return; | ||
const hash = `${keypath}:${module[0]}:${module[1]}`; | ||
const hash = `${keypath}:${mod[0]}:${mod[1]}`; | ||
const importLocalName = | ||
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`); | ||
const importLocalName = | ||
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`); | ||
if (!newImports[hash]) { | ||
if (module[1] === '*') { | ||
newImports[hash] = `import * as ${importLocalName} from '${ | ||
module[0] | ||
}';`; | ||
} else { | ||
newImports[hash] = `import { ${ | ||
module[1] | ||
} as ${importLocalName} } from '${module[0]}';`; | ||
} | ||
} | ||
if (!newImports.has(hash)) { | ||
if (mod[1] === "*") { | ||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`); | ||
} else { | ||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${ | ||
mod[0] | ||
}';`); | ||
} | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
if (name !== keypath) { | ||
magicString.overwrite(node.start, node.end, importLocalName, { | ||
storeName: true | ||
}); | ||
} | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
walk(ast, { | ||
enter (node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
walk(ast, { | ||
enter(node, parent) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
magicString.addSourcemapLocation(node.end); | ||
} | ||
if (node.scope) scope = node.scope; | ||
if (node.scope) scope = node.scope; | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === 'Property' && node.shorthand) { | ||
const name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
// special case – shorthand properties. because node.key === node.value, | ||
// we can't differentiate once we've descended into the node | ||
if (node.type === "Property" && node.shorthand) { | ||
const name = node.key.name; | ||
handleReference(node, name, name); | ||
return this.skip(); | ||
} | ||
if (isReference(node, parent)) { | ||
const { name, keypath } = flatten(node); | ||
const handled = handleReference(node, name, keypath); | ||
if (handled) return this.skip(); | ||
} | ||
}, | ||
leave (node) { | ||
if (node.scope) scope = scope.parent; | ||
} | ||
}); | ||
if (isReference(node, parent)) { | ||
const { name, keypath } = flatten(node); | ||
const handled = handleReference(node, name, keypath); | ||
if (handled) return this.skip(); | ||
} | ||
}, | ||
leave(node) { | ||
if (node.scope) scope = scope.parent; | ||
} | ||
}); | ||
const keys = Object.keys(newImports); | ||
if (!keys.length) return null; | ||
if (newImports.size === 0) { | ||
return { | ||
code, | ||
ast | ||
}; | ||
} | ||
const importBlock = Array.from(newImports.values()) | ||
.join("\n\n"); | ||
const importBlock = keys.map(hash => newImports[hash]).join('\n\n'); | ||
magicString.prepend(importBlock + '\n\n'); | ||
magicString.prepend(importBlock + "\n\n"); | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
return { | ||
code: magicString.toString(), | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
}; | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19874
5
1
- Removedestree-walker@0.5.2(transitive)
Updatedestree-walker@^0.6.1
Updatedmagic-string@^0.25.2
Updatedrollup-pluginutils@^2.8.1