rollup-plugin-inject
Advanced tools
Comparing version 1.1.1 to 1.2.0
# rollup-plugin-inject | ||
## 1.2.0 | ||
* Generate sourcemaps by default | ||
## 1.1.1 | ||
@@ -4,0 +8,0 @@ |
@@ -11,213 +11,2 @@ 'use strict'; | ||
var babelHelpers = {}; | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var extractors = { | ||
Identifier: function Identifier(names, param) { | ||
names.push(param.name); | ||
}, | ||
ObjectPattern: function ObjectPattern(names, param) { | ||
param.properties.forEach(function (prop) { | ||
extractors[prop.key.type](names, prop.key); | ||
}); | ||
}, | ||
ArrayPattern: function ArrayPattern(names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function RestElement(names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function AssignmentPattern(names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
var Declaration = (function () { | ||
function Declaration() { | ||
babelHelpers.classCallCheck(this, Declaration); | ||
this.statement = null; | ||
this.name = null; | ||
this.isReassigned = false; | ||
this.aliases = []; | ||
} | ||
Declaration.prototype.addAlias = function addAlias(declaration) { | ||
this.aliases.push(declaration); | ||
}; | ||
Declaration.prototype.addReference = function addReference(reference) { | ||
reference.declaration = this; | ||
this.name = reference.name; // TODO handle differences of opinion | ||
if (reference.isReassignment) this.isReassigned = true; | ||
}; | ||
Declaration.prototype.render = function render(es6) { | ||
if (es6) return this.name; | ||
if (!this.isReassigned || !this.isExported) return this.name; | ||
return "exports." + this.name; | ||
}; | ||
Declaration.prototype.use = function use() { | ||
this.isUsed = true; | ||
if (this.statement) this.statement.mark(); | ||
this.aliases.forEach(function (alias) { | ||
return alias.use(); | ||
}); | ||
}; | ||
return Declaration; | ||
})(); | ||
var Scope = (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
babelHelpers.classCallCheck(this, Scope); | ||
options = options || {}; | ||
this.parent = options.parent; | ||
this.isBlockScope = !!options.block; | ||
this.declarations = Object.create(null); | ||
if (options.params) { | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
_this.declarations[name] = new Declaration(name); | ||
}); | ||
}); | ||
} | ||
} | ||
Scope.prototype.addDeclaration = function addDeclaration(node, isBlockDeclaration, isVar) { | ||
var _this2 = this; | ||
if (!isBlockDeclaration && this.isBlockScope) { | ||
// it's a `var` or function node, and this | ||
// is a block scope, so we need to go up | ||
this.parent.addDeclaration(node, isBlockDeclaration, isVar); | ||
} else { | ||
extractNames(node.id).forEach(function (name) { | ||
_this2.declarations[name] = new Declaration(name); | ||
}); | ||
} | ||
}; | ||
Scope.prototype.contains = function contains(name) { | ||
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false); | ||
}; | ||
Scope.prototype.eachDeclaration = function eachDeclaration(fn) { | ||
var _this3 = this; | ||
Object.keys(this.declarations).forEach(function (key) { | ||
fn(key, _this3.declarations[key]); | ||
}); | ||
}; | ||
Scope.prototype.findDeclaration = function findDeclaration(name) { | ||
return this.declarations[name] || this.parent && this.parent.findDeclaration(name); | ||
}; | ||
return Scope; | ||
})(); | ||
var blockDeclarations = { | ||
'const': true, | ||
'let': true | ||
}; | ||
function attachScopes(ast) { | ||
var scope = new Scope(); | ||
estreeWalker.walk(ast, { | ||
enter: function enter(node, parent) { | ||
// function foo () {...} | ||
// class Foo {...} | ||
if (/(Function|Class)Declaration/.test(node.type)) { | ||
scope.addDeclaration(node, false, false); | ||
} | ||
// var foo = 1 | ||
if (node.type === 'VariableDeclaration') { | ||
var isBlockDeclaration = blockDeclarations[node.kind]; | ||
// only one declarator per block, because we split them up already | ||
scope.addDeclaration(node.declarations[0], isBlockDeclaration, true); | ||
} | ||
var newScope = undefined; | ||
// create new function scope | ||
if (/Function/.test(node.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: false, | ||
params: node.params | ||
}); | ||
// named function expressions - the name is considered | ||
// part of the function's scope | ||
if (node.type === 'FunctionExpression' && node.id) { | ||
newScope.addDeclaration(node, false, false); | ||
} | ||
} | ||
// create new block scope | ||
if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: true | ||
}); | ||
} | ||
// catch clause has its own block scope | ||
if (node.type === 'CatchClause') { | ||
newScope = new Scope({ | ||
parent: scope, | ||
params: [node.param], | ||
block: true | ||
}); | ||
} | ||
if (newScope) { | ||
Object.defineProperty(node, '_scope', { | ||
value: newScope, | ||
configurable: true | ||
}); | ||
scope = newScope; | ||
} | ||
}, | ||
leave: function leave(node) { | ||
if (node._scope) { | ||
scope = scope.parent; | ||
} | ||
} | ||
}); | ||
return scope; | ||
} | ||
var reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split(' '); | ||
@@ -313,2 +102,3 @@ var builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split(' '); | ||
var firstpass = new RegExp('(?:' + Object.keys(modules).map(escape).join('|') + ')', 'g'); | ||
var sourceMap = options.sourceMap !== false; | ||
@@ -334,3 +124,3 @@ return { | ||
// analyse scopes | ||
var scope = attachScopes(ast); | ||
var scope = rollupPluginutils.attachScopes(ast, 'scope'); | ||
@@ -370,3 +160,3 @@ var imports = {}; | ||
enter: function enter(node, parent) { | ||
if (options.sourceMap) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
@@ -376,3 +166,3 @@ magicString.addSourcemapLocation(node.end); | ||
if (node._scope) scope = node._scope; | ||
if (node.scope) scope = node.scope; | ||
@@ -397,3 +187,3 @@ // special case – shorthand properties. because node.key === node.value, | ||
leave: function leave(node) { | ||
if (node._scope) scope = scope.parent; | ||
if (node.scope) scope = scope.parent; | ||
} | ||
@@ -412,3 +202,3 @@ }); | ||
code: magicString.toString(), | ||
map: options.sourceMap ? magicString.generateMap() : null | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
@@ -415,0 +205,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { createFilter } from 'rollup-pluginutils'; | ||
import { attachScopes, createFilter } from 'rollup-pluginutils'; | ||
import { extname } from 'path'; | ||
@@ -7,213 +7,2 @@ import { walk } from 'estree-walker'; | ||
var babelHelpers = {}; | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var extractors = { | ||
Identifier: function Identifier(names, param) { | ||
names.push(param.name); | ||
}, | ||
ObjectPattern: function ObjectPattern(names, param) { | ||
param.properties.forEach(function (prop) { | ||
extractors[prop.key.type](names, prop.key); | ||
}); | ||
}, | ||
ArrayPattern: function ArrayPattern(names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function RestElement(names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function AssignmentPattern(names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
var Declaration = (function () { | ||
function Declaration() { | ||
babelHelpers.classCallCheck(this, Declaration); | ||
this.statement = null; | ||
this.name = null; | ||
this.isReassigned = false; | ||
this.aliases = []; | ||
} | ||
Declaration.prototype.addAlias = function addAlias(declaration) { | ||
this.aliases.push(declaration); | ||
}; | ||
Declaration.prototype.addReference = function addReference(reference) { | ||
reference.declaration = this; | ||
this.name = reference.name; // TODO handle differences of opinion | ||
if (reference.isReassignment) this.isReassigned = true; | ||
}; | ||
Declaration.prototype.render = function render(es6) { | ||
if (es6) return this.name; | ||
if (!this.isReassigned || !this.isExported) return this.name; | ||
return "exports." + this.name; | ||
}; | ||
Declaration.prototype.use = function use() { | ||
this.isUsed = true; | ||
if (this.statement) this.statement.mark(); | ||
this.aliases.forEach(function (alias) { | ||
return alias.use(); | ||
}); | ||
}; | ||
return Declaration; | ||
})(); | ||
var Scope = (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
babelHelpers.classCallCheck(this, Scope); | ||
options = options || {}; | ||
this.parent = options.parent; | ||
this.isBlockScope = !!options.block; | ||
this.declarations = Object.create(null); | ||
if (options.params) { | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
_this.declarations[name] = new Declaration(name); | ||
}); | ||
}); | ||
} | ||
} | ||
Scope.prototype.addDeclaration = function addDeclaration(node, isBlockDeclaration, isVar) { | ||
var _this2 = this; | ||
if (!isBlockDeclaration && this.isBlockScope) { | ||
// it's a `var` or function node, and this | ||
// is a block scope, so we need to go up | ||
this.parent.addDeclaration(node, isBlockDeclaration, isVar); | ||
} else { | ||
extractNames(node.id).forEach(function (name) { | ||
_this2.declarations[name] = new Declaration(name); | ||
}); | ||
} | ||
}; | ||
Scope.prototype.contains = function contains(name) { | ||
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false); | ||
}; | ||
Scope.prototype.eachDeclaration = function eachDeclaration(fn) { | ||
var _this3 = this; | ||
Object.keys(this.declarations).forEach(function (key) { | ||
fn(key, _this3.declarations[key]); | ||
}); | ||
}; | ||
Scope.prototype.findDeclaration = function findDeclaration(name) { | ||
return this.declarations[name] || this.parent && this.parent.findDeclaration(name); | ||
}; | ||
return Scope; | ||
})(); | ||
var blockDeclarations = { | ||
'const': true, | ||
'let': true | ||
}; | ||
function attachScopes(ast) { | ||
var scope = new Scope(); | ||
walk(ast, { | ||
enter: function enter(node, parent) { | ||
// function foo () {...} | ||
// class Foo {...} | ||
if (/(Function|Class)Declaration/.test(node.type)) { | ||
scope.addDeclaration(node, false, false); | ||
} | ||
// var foo = 1 | ||
if (node.type === 'VariableDeclaration') { | ||
var isBlockDeclaration = blockDeclarations[node.kind]; | ||
// only one declarator per block, because we split them up already | ||
scope.addDeclaration(node.declarations[0], isBlockDeclaration, true); | ||
} | ||
var newScope = undefined; | ||
// create new function scope | ||
if (/Function/.test(node.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: false, | ||
params: node.params | ||
}); | ||
// named function expressions - the name is considered | ||
// part of the function's scope | ||
if (node.type === 'FunctionExpression' && node.id) { | ||
newScope.addDeclaration(node, false, false); | ||
} | ||
} | ||
// create new block scope | ||
if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: true | ||
}); | ||
} | ||
// catch clause has its own block scope | ||
if (node.type === 'CatchClause') { | ||
newScope = new Scope({ | ||
parent: scope, | ||
params: [node.param], | ||
block: true | ||
}); | ||
} | ||
if (newScope) { | ||
Object.defineProperty(node, '_scope', { | ||
value: newScope, | ||
configurable: true | ||
}); | ||
scope = newScope; | ||
} | ||
}, | ||
leave: function leave(node) { | ||
if (node._scope) { | ||
scope = scope.parent; | ||
} | ||
} | ||
}); | ||
return scope; | ||
} | ||
var reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split(' '); | ||
@@ -309,2 +98,3 @@ var builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split(' '); | ||
var firstpass = new RegExp('(?:' + Object.keys(modules).map(escape).join('|') + ')', 'g'); | ||
var sourceMap = options.sourceMap !== false; | ||
@@ -330,3 +120,3 @@ return { | ||
// analyse scopes | ||
var scope = attachScopes(ast); | ||
var scope = attachScopes(ast, 'scope'); | ||
@@ -366,3 +156,3 @@ var imports = {}; | ||
enter: function enter(node, parent) { | ||
if (options.sourceMap) { | ||
if (sourceMap) { | ||
magicString.addSourcemapLocation(node.start); | ||
@@ -372,3 +162,3 @@ magicString.addSourcemapLocation(node.end); | ||
if (node._scope) scope = node._scope; | ||
if (node.scope) scope = node.scope; | ||
@@ -393,3 +183,3 @@ // special case – shorthand properties. because node.key === node.value, | ||
leave: function leave(node) { | ||
if (node._scope) scope = scope.parent; | ||
if (node.scope) scope = scope.parent; | ||
} | ||
@@ -408,3 +198,3 @@ }); | ||
code: magicString.toString(), | ||
map: options.sourceMap ? magicString.generateMap() : null | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
@@ -411,0 +201,0 @@ } |
{ | ||
"name": "rollup-plugin-inject", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"devDependencies": { | ||
"eslint": "^1.7.3", | ||
"gobble": "^0.10.2", | ||
"gobble-cli": "^0.6.0", | ||
"gobble-rollup": "^0.11.0", | ||
"mocha": "^2.3.3", | ||
"rollup": "^0.20.3", | ||
"rollup-plugin-babel": "^1.0.0" | ||
@@ -17,3 +15,4 @@ }, | ||
"pretest": "npm run build", | ||
"build": "gobble build -f dist", | ||
"build": "rollup -c -f cjs -o dist/rollup-plugin-inject.cjs.js && rollup -c -f es6 -o dist/rollup-plugin-inject.es6.js", | ||
"prebuild": "rm -rf dist/*", | ||
"prepublish": "npm test" | ||
@@ -30,4 +29,4 @@ }, | ||
"magic-string": "^0.8.0", | ||
"rollup-pluginutils": "^1.1.0" | ||
"rollup-pluginutils": "^1.2.0" | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
import { createFilter } from 'rollup-pluginutils'; | ||
import { attachScopes, createFilter } from 'rollup-pluginutils'; | ||
import { extname } from 'path'; | ||
import { walk } from 'estree-walker'; | ||
import acorn from 'acorn'; | ||
import attachScopes from './attachScopes'; | ||
import makeLegalIdentifier from './makeLegalIdentifier'; | ||
@@ -66,2 +65,3 @@ import MagicString from 'magic-string'; | ||
const firstpass = new RegExp( `(?:${Object.keys( modules ).map( escape ).join( '|' )})`, 'g' ); | ||
const sourceMap = options.sourceMap !== false; | ||
@@ -87,3 +87,3 @@ return { | ||
// analyse scopes | ||
let scope = attachScopes( ast ); | ||
let scope = attachScopes( ast, 'scope' ); | ||
@@ -123,3 +123,3 @@ let imports = {}; | ||
enter ( node, parent ) { | ||
if ( options.sourceMap ) { | ||
if ( sourceMap ) { | ||
magicString.addSourcemapLocation( node.start ); | ||
@@ -129,3 +129,3 @@ magicString.addSourcemapLocation( node.end ); | ||
if ( node._scope ) scope = node._scope; | ||
if ( node.scope ) scope = node.scope; | ||
@@ -146,3 +146,3 @@ // special case – shorthand properties. because node.key === node.value, | ||
leave ( node ) { | ||
if ( node._scope ) scope = scope.parent; | ||
if ( node.scope ) scope = scope.parent; | ||
} | ||
@@ -159,6 +159,6 @@ }); | ||
code: magicString.toString(), | ||
map: options.sourceMap ? magicString.generateMap() : null | ||
map: sourceMap ? magicString.generateMap() : null | ||
}; | ||
} | ||
} | ||
}; | ||
} |
4
19327
7
450
Updatedrollup-pluginutils@^1.2.0