babel-plugin-transform-omodule-namespace
Advanced tools
Comparing version
@@ -14,4 +14,11 @@ 'use strict'; | ||
return { | ||
visitor: { | ||
Program: { | ||
enter: function enter() { | ||
done__onamespace = false; | ||
done__ofilepath = false; | ||
} | ||
}, | ||
Identifier: function Identifier(babelPath, state) { | ||
@@ -39,9 +46,25 @@ var _state$opts = state.opts, | ||
if (absoluteFilename.indexOf(absoluteRootPath) > -1) { | ||
if (babelPath.node.name === '__onamespace') { | ||
if (babelPath.node.name === '__onamespace' && !done__onamespace) { | ||
var s = getONamesapce(matchRootPath, absoluteFilename, namespacePrefix); | ||
babelPath.replaceWith(t.stringLiteral(s)); | ||
var prog = babelPath.find(function (path) { | ||
return path.isProgram(); | ||
}); | ||
var ident = t.identifier('__onamespace'); | ||
var string = t.stringLiteral(s); | ||
var declarator = t.variableDeclarator(ident, string); | ||
var declaration = t.variableDeclaration('var', [declarator]); | ||
prog.scope.block.body.unshift(declaration); | ||
done__onamespace = true; | ||
} | ||
if (babelPath.node.name === '__ofilepath') { | ||
if (babelPath.node.name === '__ofilepath' && !done__ofilepath) { | ||
var _s = getOFilePath(matchRootPath, absoluteFilename); | ||
babelPath.replaceWith(t.stringLiteral(_s)); | ||
var _prog = babelPath.find(function (path) { | ||
return path.isProgram(); | ||
}); | ||
var _ident = t.identifier('__ofilepath'); | ||
var _string = t.stringLiteral(_s); | ||
var _declarator = t.variableDeclarator(_ident, _string); | ||
var _declaration = t.variableDeclaration('var', [_declarator]); | ||
_prog.scope.block.body.unshift(_declaration); | ||
done__ofilepath = true; | ||
} | ||
@@ -66,4 +89,2 @@ } | ||
var _estreeWalker = require('estree-walker'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -75,32 +96,2 @@ | ||
// function `uses` and `inject` learnd from 'babel-plugin-transform-dirname-filename' | ||
// ^_^ | ||
// https://github.com/TooTallNate/babel-plugin-transform-dirname-filename/blob/master/index.js | ||
// function uses(prog, variables) { | ||
// const results = variables.reduce(function(o, name) { | ||
// o[name] = false | ||
// return o | ||
// }, {}) | ||
// | ||
// walk(prog.scope.block, { | ||
// enter: function(node) { | ||
// if ('Identifier' === node.type) { | ||
// if (node.name in results) { | ||
// results[node.name] = true | ||
// } | ||
// } | ||
// } | ||
// }) | ||
// | ||
// return results | ||
// } | ||
// function inject(t, prog, variable, value) { | ||
// const ident = t.identifier(variable) | ||
// const string = t.stringLiteral(value) | ||
// const declarator = t.variableDeclarator(ident, string) | ||
// const declaration = t.variableDeclaration('var', [declarator]) | ||
// prog.scope.block.body.unshift(declaration) | ||
// } | ||
function isAbsolutePath(p) { | ||
@@ -156,2 +147,5 @@ return _path2.default.isAbsolute(p); | ||
return absoluteFilename.replace(matchRootPath + _path2.default.sep, ''); | ||
} | ||
} | ||
var done__onamespace = false; | ||
var done__ofilepath = false; |
{ | ||
"name": "babel-plugin-transform-omodule-namespace", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "", | ||
@@ -33,4 +33,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"app-root-path": "^2.0.1", | ||
"estree-walker": "^0.5.0" | ||
"app-root-path": "^2.0.1" | ||
}, | ||
@@ -37,0 +36,0 @@ "files": [ |
9119
2.44%1
-50%121
-3.97%- Removed
- Removed