babel-plugin-cortex-module
Advanced tools
Comparing version 1.0.0-beta.0 to 1.0.0-beta.1
84
index.js
'use strict'; | ||
var nodePath = require('path'); | ||
var fs = require('fs'); | ||
var fse = require('fs-extra'); | ||
var semver = require('semver'); | ||
@@ -9,2 +8,3 @@ var slash = require('slash'); | ||
var neuronLiteJS = fs.readFileSync(nodePath.join(__dirname, 'lib/neuron-lite.js'), {encoding: 'utf-8'}); | ||
var project = null; | ||
@@ -17,3 +17,3 @@ var cortexModules = {}; | ||
visitor: { | ||
Program: function () { | ||
Program: function (path) { | ||
if (!project) { | ||
@@ -25,2 +25,62 @@ project = { | ||
} | ||
// calculate current file path | ||
var filePath = this.file.opts.filenameRelative; | ||
var pathRelativeToRoot; | ||
if (nodePath.isAbsolute(filePath)) { | ||
pathRelativeToRoot = nodePath.relative(project.root, filePath); | ||
} else { | ||
pathRelativeToRoot = filePath; | ||
} | ||
// alter cortex built file | ||
if (/^neurons[/\\]/.test(pathRelativeToRoot)) { | ||
var pathNames = pathRelativeToRoot.split(/[/\\]/); | ||
var modName = pathNames[1]; | ||
var modVer = pathNames[2]; | ||
if (pathNames[3] === modName + '.js') { // is cortex module main file | ||
// parse inner module map | ||
var nodes = path.node.body[0].expression.callee.body.body; | ||
var deps = []; | ||
for (var i = 1; i < nodes.length; i++) { | ||
var node = nodes[i]; | ||
if (t.isVariableDeclaration(node)) { | ||
var declarator = node.declarations[0]; | ||
if (/^_\d+$/.test(declarator.id.name)) { | ||
deps.push(declarator.init.value); | ||
} | ||
} else break; | ||
} | ||
var internalDepPrefix = modName + '@' + modVer + '/'; | ||
var depStr = deps.filter(function (depName) { | ||
return depName.indexOf(internalDepPrefix) !== 0; | ||
}).reduce(function (depStr, depName) { | ||
var atPos = depName.indexOf('@'); | ||
var slashPos = depName.indexOf('/'); | ||
if (atPos <= 0) throw new Error('Illegal format: ' + depName); | ||
var requiredName = depName.slice(0, atPos); | ||
var versionRule = depName.slice(atPos + 1); | ||
if (slashPos > 0) { | ||
requiredName += depName.slice(slashPos); | ||
} | ||
var actualPath = getActualPath(requiredName, versionRule); | ||
depStr.push(JSON.stringify(requiredName) + ':{id:' + JSON.stringify(depName) + ',mod:require(' + JSON.stringify(actualPath) + ')}'); | ||
return depStr; | ||
}, []); | ||
var replaceStr = '{' + depStr.join(',') + '}'; | ||
var startupJS = neuronLiteJS.replace('$EXTDEPS$', replaceStr); | ||
var startupJSNodes = babel.transform(startupJS).ast.program.body; | ||
startupJSNodes.forEach(function (node) { | ||
babel.traverse.removeProperties(node); | ||
}); | ||
path.traverse({ | ||
FunctionDeclaration: function (path) { | ||
if (startupJSNodes && t.isIdentifier(path.node.id, {name: 'mix'})) { | ||
path.insertBefore(startupJSNodes); | ||
startupJSNodes = null; | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}, | ||
@@ -35,9 +95,2 @@ CallExpression: function(path) { | ||
var requireParam = path.node.arguments[0].value; | ||
var filePath = this.file.opts.filenameRelative; | ||
var pathRelativeToRoot; | ||
if (nodePath.isAbsolute(filePath)) { | ||
pathRelativeToRoot = nodePath.relative(project.root, filePath); | ||
} else { | ||
pathRelativeToRoot = filePath; | ||
} | ||
@@ -48,8 +101,3 @@ var actualFilePath = null; | ||
actualFilePath = getActualPath(requiredName); | ||
} else if (/^neurons[/\\]/.test(pathRelativeToRoot) && requireParam[0] !== '.') { | ||
// file itself is a cortex module | ||
actualFilePath = getActualPath(requireParam); | ||
} | ||
if (actualFilePath) { | ||
@@ -70,3 +118,3 @@ path.traverse({ | ||
function getActualPath(requiredName) { | ||
function getActualPath(requiredName, versionRule) { | ||
var depName = requiredName; | ||
@@ -103,4 +151,6 @@ var isReqSpecFile = requiredName.indexOf('/') >= 0; // require('@cortex/dpapp/some/source') | ||
// get matched dependency version | ||
var versionRule = project.cortex.dependencies[depName]; | ||
if (!versionRule) throw new Error('cortex package ' + depName + ' required but not defined in cortex.json'); | ||
if (!versionRule) { | ||
versionRule = project.cortex.dependencies[depName]; | ||
if (!versionRule) throw new Error('cortex package ' + depName + ' required but not defined in cortex.json'); | ||
} | ||
var matchedVersion = null; | ||
@@ -107,0 +157,0 @@ depInfo.some(function (item) { |
'use strict'; | ||
var fs = require('fs'); | ||
var fse = require('fs-extra'); | ||
@@ -4,0 +3,0 @@ var path = require('path'); |
{ | ||
"name": "babel-plugin-cortex-module", | ||
"version": "1.0.0-beta.0", | ||
"version": "1.0.0-beta.1", | ||
"description": "babel plugin for cortex modules", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
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
52740
14
232
2