babel-plugin-name-amd-modules
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -16,24 +16,24 @@ 'use strict'; | ||
it('correctly names anonymous modules', function () { | ||
var source = '\n\tdefine([], function(){})\n\t'; | ||
var source = '\n\tdefine([], function(){})\n\t'; | ||
var _babel$transform = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform.code; | ||
var _babel$transform = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform.code; | ||
expect(code).toMatchSnapshot(); | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
it('correctly renames named modules', function () { | ||
var source = '\n\tdefine(\'my-invalid-name\', [], function(){})\n\t'; | ||
var source = '\n\tdefine(\'my-invalid-name\', [], function(){})\n\t'; | ||
var _babel$transform2 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform2.code; | ||
var _babel$transform2 = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [_index2.default] | ||
}), | ||
code = _babel$transform2.code; | ||
expect(code).toMatchSnapshot(); | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
188
lib/index.js
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
var t = _ref.types; | ||
var nameVisitor = { | ||
ExpressionStatement: function ExpressionStatement(path, _ref2) { | ||
var opts = _ref2.opts; | ||
var nameVisitor = { | ||
ExpressionStatement: function ExpressionStatement(path, _ref2) { | ||
var opts = _ref2.opts; | ||
var node = path.node; | ||
var expression = node.expression; | ||
var node = path.node; | ||
var expression = node.expression; | ||
if (t.isCallExpression(expression)) { | ||
var callee = expression.callee; | ||
if (t.isCallExpression(expression)) { | ||
var callee = expression.callee; | ||
if (t.isIdentifier(callee, { name: 'define' })) { | ||
var args = expression.arguments; | ||
if (t.isIdentifier(callee, { name: 'define' })) { | ||
var args = expression.arguments; | ||
var insertName = false; | ||
var unshiftName = true; | ||
var insertName = false; | ||
var unshiftName = true; | ||
switch (args.length) { | ||
case 1: | ||
insertName = t.isFunctionExpression(args[0]); | ||
break; | ||
switch (args.length) { | ||
case 1: | ||
insertName = t.isFunctionExpression(args[0]); | ||
break; | ||
case 2: | ||
insertName = t.isArrayExpression(args[0]) && t.isFunctionExpression(args[1]); | ||
break; | ||
case 2: | ||
insertName = t.isArrayExpression(args[0]) && t.isFunctionExpression(args[1]); | ||
break; | ||
case 3: | ||
unshiftName = false; | ||
insertName = t.isStringLiteral(args[0]) && t.isArrayExpression(args[1]) && t.isFunctionExpression(args[2]); | ||
break; | ||
} | ||
case 3: | ||
unshiftName = false; | ||
insertName = t.isStringLiteral(args[0]) && t.isArrayExpression(args[1]) && t.isFunctionExpression(args[2]); | ||
break; | ||
} | ||
if (insertName) { | ||
var packageName = getPackageName(this.opts.packageName, this.filenameRelative); | ||
if (insertName) { | ||
var packageName = getPackageName(this.opts.packageName, this.filenameRelative); | ||
var moduleName = getModuleName(this.filenameRelative, getSrcPrefixes(opts)); | ||
var moduleName = getModuleName(this.filenameRelative, getSrcPrefixes(opts)); | ||
if (unshiftName) { | ||
args.unshift(t.stringLiteral('' + packageName + moduleName)); | ||
} else { | ||
args[0].value = '' + packageName + moduleName; | ||
} | ||
if (unshiftName) { | ||
args.unshift(t.stringLiteral('' + packageName + moduleName)); | ||
} else { | ||
args[0].value = '' + packageName + moduleName; | ||
} | ||
path.stop(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
path.stop(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
return { | ||
visitor: { | ||
Program: { | ||
exit: function exit(path, state) { | ||
// We must traverse the AST again because the | ||
// transform-es2015-modules-amd plugin emits its define() | ||
// call after exiting Program node :-( | ||
path.traverse(nameVisitor, { | ||
filenameRelative: state.file.opts.filenameRelative, | ||
opts: state.opts | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
return { | ||
visitor: { | ||
Program: { | ||
exit: function exit(path, state) { | ||
// We must traverse the AST again because the | ||
// transform-es2015-modules-amd plugin emits its define() | ||
// call after exiting Program node :-( | ||
path.traverse(nameVisitor, { | ||
filenameRelative: state.file.opts.filenameRelative, | ||
opts: state.opts | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -96,13 +96,13 @@ | ||
* @param {object} opts the Babel plugin options | ||
* @return {Array} the normalized srcPrefixes array (with native path | ||
* @return {Array} the normalized srcPrefixes array (with native path | ||
* separators) | ||
*/ | ||
function getSrcPrefixes(opts) { | ||
var srcPrefixes = opts.srcPrefixes || ['src/main/resources/META-INF/resources']; | ||
var srcPrefixes = opts.srcPrefixes || ['src/main/resources/META-INF/resources']; | ||
return srcPrefixes.map(function (srcPrefix) { | ||
return _path2.default.normalize(srcPrefix); | ||
}).map(function (srcPrefix) { | ||
return srcPrefix.endsWith(_path2.default.sep) ? srcPrefix : srcPrefix + _path2.default.sep; | ||
}); | ||
return srcPrefixes.map(function (srcPrefix) { | ||
return _path2.default.normalize(srcPrefix); | ||
}).map(function (srcPrefix) { | ||
return srcPrefix.endsWith(_path2.default.sep) ? srcPrefix : srcPrefix + _path2.default.sep; | ||
}); | ||
} | ||
@@ -112,5 +112,5 @@ | ||
* Resolve the package name of a JS module file. | ||
* @param {String} packageName a forced package name or '<package.json>' to get | ||
* @param {String} packageName a forced package name or '<package.json>' to get | ||
* the package name from the nearest ancestor package.json file | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* compiler | ||
@@ -128,16 +128,16 @@ * @return {String} the package name (in 'pkg@version' format) ending with '/' | ||
function getPackageName(packageName, filenameRelative) { | ||
packageName = packageName || '<package.json>'; | ||
packageName = packageName || '<package.json>'; | ||
if (packageName === '<package.json>') { | ||
var pkgJsonPath = pkgs.getPackageJsonPath(filenameRelative); | ||
var pkgJson = (0, _readJsonSync2.default)(pkgJsonPath); | ||
if (packageName === '<package.json>') { | ||
var pkgJsonPath = pkgs.getPackageJsonPath(filenameRelative); | ||
var pkgJson = (0, _readJsonSync2.default)(pkgJsonPath); | ||
packageName = pkgJson.name + '@' + pkgJson.version + '/'; | ||
} | ||
packageName = pkgJson.name + '@' + pkgJson.version + '/'; | ||
} | ||
if (!packageName.endsWith('/')) { | ||
packageName += '/'; | ||
} | ||
if (!packageName.endsWith('/')) { | ||
packageName += '/'; | ||
} | ||
return packageName; | ||
return packageName; | ||
} | ||
@@ -147,35 +147,35 @@ | ||
* Get the module name of a JS module file given its path. | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* compiler | ||
* @param {Array} srcPrefixes an array of source directories where JS module | ||
* @param {Array} srcPrefixes an array of source directories where JS module | ||
* files may live | ||
* @return {String} the module name that must be assigned to the file with the | ||
* syntax: | ||
* @return {String} the module name that must be assigned to the file with the | ||
* syntax: | ||
* <package name>@<package version>/<relative path without trailing .js> | ||
*/ | ||
function getModuleName(filenameRelative, srcPrefixes) { | ||
var filenameAbsolute = _path2.default.resolve(filenameRelative); | ||
var pkgDir = pkgs.getPackageDir(filenameRelative); | ||
var filenameAbsolute = _path2.default.resolve(filenameRelative); | ||
var pkgDir = pkgs.getPackageDir(filenameRelative); | ||
var moduleName = filenameAbsolute.substring(pkgDir.length + 1); | ||
var moduleName = filenameAbsolute.substring(pkgDir.length + 1); | ||
if (moduleName.toLowerCase().endsWith('.js')) { | ||
moduleName = moduleName.substring(0, moduleName.length - 3); | ||
} | ||
if (moduleName.toLowerCase().endsWith('.js')) { | ||
moduleName = moduleName.substring(0, moduleName.length - 3); | ||
} | ||
for (var i = 0; i < srcPrefixes.length; i++) { | ||
var srcPrefix = _path2.default.normalize(srcPrefixes[i]); | ||
for (var i = 0; i < srcPrefixes.length; i++) { | ||
var srcPrefix = _path2.default.normalize(srcPrefixes[i]); | ||
if (moduleName.startsWith(srcPrefix)) { | ||
moduleName = moduleName.substring(srcPrefix.length); | ||
break; | ||
} | ||
} | ||
if (moduleName.startsWith(srcPrefix)) { | ||
moduleName = moduleName.substring(srcPrefix.length); | ||
break; | ||
} | ||
} | ||
if (_path2.default.sep == '\\') { | ||
moduleName = moduleName.replace(/\\/g, '/'); | ||
} | ||
if (_path2.default.sep == '\\') { | ||
moduleName = moduleName.replace(/\\/g, '/'); | ||
} | ||
return moduleName; | ||
return moduleName; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-plugin-name-amd-modules", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "A Babel plugin to give name to AMD modules based on their path and package.", | ||
@@ -15,5 +15,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"liferay-npm-build-tools-common": "^1.2.2", | ||
"liferay-npm-build-tools-common": "^1.2.3", | ||
"read-json-sync": "^1.1.1" | ||
} | ||
} |
@@ -5,25 +5,25 @@ import * as babel from 'babel-core'; | ||
it('correctly names anonymous modules', () => { | ||
const source = ` | ||
const source = ` | ||
define([], function(){}) | ||
`; | ||
const {code} = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [plugin], | ||
}); | ||
const {code} = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [plugin], | ||
}); | ||
expect(code).toMatchSnapshot(); | ||
expect(code).toMatchSnapshot(); | ||
}); | ||
it('correctly renames named modules', () => { | ||
const source = ` | ||
const source = ` | ||
define('my-invalid-name', [], function(){}) | ||
`; | ||
const {code} = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [plugin], | ||
}); | ||
const {code} = babel.transform(source, { | ||
filenameRelative: __filename, | ||
plugins: [plugin], | ||
}); | ||
expect(code).toMatchSnapshot(); | ||
expect(code).toMatchSnapshot(); | ||
}); |
211
src/index.js
@@ -12,74 +12,77 @@ import * as pkgs from 'liferay-npm-build-tools-common/lib/packages'; | ||
export default function({types: t}) { | ||
const nameVisitor = { | ||
ExpressionStatement(path, {opts}) { | ||
const node = path.node; | ||
const expression = node.expression; | ||
const nameVisitor = { | ||
ExpressionStatement(path, {opts}) { | ||
const node = path.node; | ||
const expression = node.expression; | ||
if (t.isCallExpression(expression)) { | ||
const callee = expression.callee; | ||
if (t.isCallExpression(expression)) { | ||
const callee = expression.callee; | ||
if (t.isIdentifier(callee, {name: 'define'})) { | ||
const args = expression.arguments; | ||
if (t.isIdentifier(callee, {name: 'define'})) { | ||
const args = expression.arguments; | ||
let insertName = false; | ||
let unshiftName = true; | ||
let insertName = false; | ||
let unshiftName = true; | ||
switch (args.length) { | ||
case 1: | ||
insertName = t.isFunctionExpression(args[0]); | ||
break; | ||
switch (args.length) { | ||
case 1: | ||
insertName = t.isFunctionExpression(args[0]); | ||
break; | ||
case 2: | ||
insertName = | ||
t.isArrayExpression(args[0]) && t.isFunctionExpression(args[1]); | ||
break; | ||
case 2: | ||
insertName = | ||
t.isArrayExpression(args[0]) && | ||
t.isFunctionExpression(args[1]); | ||
break; | ||
case 3: | ||
unshiftName = false; | ||
insertName = | ||
t.isStringLiteral(args[0]) && | ||
t.isArrayExpression(args[1]) && | ||
t.isFunctionExpression(args[2]); | ||
break; | ||
} | ||
case 3: | ||
unshiftName = false; | ||
insertName = | ||
t.isStringLiteral(args[0]) && | ||
t.isArrayExpression(args[1]) && | ||
t.isFunctionExpression(args[2]); | ||
break; | ||
} | ||
if (insertName) { | ||
const packageName = getPackageName( | ||
this.opts.packageName, | ||
this.filenameRelative | ||
); | ||
if (insertName) { | ||
const packageName = getPackageName( | ||
this.opts.packageName, | ||
this.filenameRelative | ||
); | ||
const moduleName = getModuleName( | ||
this.filenameRelative, | ||
getSrcPrefixes(opts) | ||
); | ||
const moduleName = getModuleName( | ||
this.filenameRelative, | ||
getSrcPrefixes(opts) | ||
); | ||
if (unshiftName) { | ||
args.unshift(t.stringLiteral(`${packageName}${moduleName}`)); | ||
} else { | ||
args[0].value = `${packageName}${moduleName}`; | ||
} | ||
if (unshiftName) { | ||
args.unshift( | ||
t.stringLiteral(`${packageName}${moduleName}`) | ||
); | ||
} else { | ||
args[0].value = `${packageName}${moduleName}`; | ||
} | ||
path.stop(); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
path.stop(); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
return { | ||
visitor: { | ||
Program: { | ||
exit(path, state) { | ||
// We must traverse the AST again because the | ||
// transform-es2015-modules-amd plugin emits its define() | ||
// call after exiting Program node :-( | ||
path.traverse(nameVisitor, { | ||
filenameRelative: state.file.opts.filenameRelative, | ||
opts: state.opts, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}; | ||
return { | ||
visitor: { | ||
Program: { | ||
exit(path, state) { | ||
// We must traverse the AST again because the | ||
// transform-es2015-modules-amd plugin emits its define() | ||
// call after exiting Program node :-( | ||
path.traverse(nameVisitor, { | ||
filenameRelative: state.file.opts.filenameRelative, | ||
opts: state.opts, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
@@ -91,16 +94,16 @@ | ||
* @param {object} opts the Babel plugin options | ||
* @return {Array} the normalized srcPrefixes array (with native path | ||
* @return {Array} the normalized srcPrefixes array (with native path | ||
* separators) | ||
*/ | ||
function getSrcPrefixes(opts) { | ||
let srcPrefixes = opts.srcPrefixes || [ | ||
'src/main/resources/META-INF/resources', | ||
]; | ||
let srcPrefixes = opts.srcPrefixes || [ | ||
'src/main/resources/META-INF/resources', | ||
]; | ||
return srcPrefixes | ||
.map(srcPrefix => path.normalize(srcPrefix)) | ||
.map( | ||
srcPrefix => | ||
srcPrefix.endsWith(path.sep) ? srcPrefix : srcPrefix + path.sep | ||
); | ||
return srcPrefixes | ||
.map(srcPrefix => path.normalize(srcPrefix)) | ||
.map( | ||
srcPrefix => | ||
srcPrefix.endsWith(path.sep) ? srcPrefix : srcPrefix + path.sep | ||
); | ||
} | ||
@@ -110,5 +113,5 @@ | ||
* Resolve the package name of a JS module file. | ||
* @param {String} packageName a forced package name or '<package.json>' to get | ||
* @param {String} packageName a forced package name or '<package.json>' to get | ||
* the package name from the nearest ancestor package.json file | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* compiler | ||
@@ -118,16 +121,16 @@ * @return {String} the package name (in 'pkg@version' format) ending with '/' | ||
function getPackageName(packageName, filenameRelative) { | ||
packageName = packageName || '<package.json>'; | ||
packageName = packageName || '<package.json>'; | ||
if (packageName === '<package.json>') { | ||
const pkgJsonPath = pkgs.getPackageJsonPath(filenameRelative); | ||
const pkgJson = readJsonSync(pkgJsonPath); | ||
if (packageName === '<package.json>') { | ||
const pkgJsonPath = pkgs.getPackageJsonPath(filenameRelative); | ||
const pkgJson = readJsonSync(pkgJsonPath); | ||
packageName = `${pkgJson.name}@${pkgJson.version}/`; | ||
} | ||
packageName = `${pkgJson.name}@${pkgJson.version}/`; | ||
} | ||
if (!packageName.endsWith('/')) { | ||
packageName += '/'; | ||
} | ||
if (!packageName.endsWith('/')) { | ||
packageName += '/'; | ||
} | ||
return packageName; | ||
return packageName; | ||
} | ||
@@ -137,34 +140,34 @@ | ||
* Get the module name of a JS module file given its path. | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* @param {String} filenameRelative the filenameRelative path as given by Babel | ||
* compiler | ||
* @param {Array} srcPrefixes an array of source directories where JS module | ||
* @param {Array} srcPrefixes an array of source directories where JS module | ||
* files may live | ||
* @return {String} the module name that must be assigned to the file with the | ||
* syntax: | ||
* @return {String} the module name that must be assigned to the file with the | ||
* syntax: | ||
* <package name>@<package version>/<relative path without trailing .js> | ||
*/ | ||
function getModuleName(filenameRelative, srcPrefixes) { | ||
const filenameAbsolute = path.resolve(filenameRelative); | ||
const pkgDir = pkgs.getPackageDir(filenameRelative); | ||
const filenameAbsolute = path.resolve(filenameRelative); | ||
const pkgDir = pkgs.getPackageDir(filenameRelative); | ||
let moduleName = filenameAbsolute.substring(pkgDir.length + 1); | ||
let moduleName = filenameAbsolute.substring(pkgDir.length + 1); | ||
if (moduleName.toLowerCase().endsWith('.js')) { | ||
moduleName = moduleName.substring(0, moduleName.length - 3); | ||
} | ||
if (moduleName.toLowerCase().endsWith('.js')) { | ||
moduleName = moduleName.substring(0, moduleName.length - 3); | ||
} | ||
for (let i = 0; i < srcPrefixes.length; i++) { | ||
const srcPrefix = path.normalize(srcPrefixes[i]); | ||
for (let i = 0; i < srcPrefixes.length; i++) { | ||
const srcPrefix = path.normalize(srcPrefixes[i]); | ||
if (moduleName.startsWith(srcPrefix)) { | ||
moduleName = moduleName.substring(srcPrefix.length); | ||
break; | ||
} | ||
} | ||
if (moduleName.startsWith(srcPrefix)) { | ||
moduleName = moduleName.substring(srcPrefix.length); | ||
break; | ||
} | ||
} | ||
if (path.sep == '\\') { | ||
moduleName = moduleName.replace(/\\/g, '/'); | ||
} | ||
if (path.sep == '\\') { | ||
moduleName = moduleName.replace(/\\/g, '/'); | ||
} | ||
return moduleName; | ||
return moduleName; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
326
24406