babel-plugin-system-import-transformer
Advanced tools
Comparing version 1.1.5 to 2.0.0
97
index.js
@@ -0,6 +1,6 @@ | ||
var path = require('path'); | ||
module.exports = function (babel) { | ||
var t = babel.types; | ||
var path = require('path'); | ||
function getImportPath(file, relativeImportPath) { | ||
@@ -17,17 +17,21 @@ var filename = file.opts.filename; | ||
// There should be a better way | ||
var importedModuleFile = t.cloneDeep(file); | ||
var importedModuleFile = t.clone(file); | ||
importedModuleFile.opts = t.cloneDeep(file.opts); | ||
importedModuleFile.opts.filename = importedModuleFile.opts.filenameRelative = importedModulePath + '.js'; | ||
var UmdFormatter = file.moduleFormatter.constructor; | ||
var result = new UmdFormatter(importedModuleFile).getModuleName(); | ||
importedModuleFile.opts.moduleIds = true; | ||
var result = importedModuleFile.getModuleName(); | ||
return result; | ||
} | ||
function SystemImportExpressionTransformer(file, params) { | ||
this.file = file; | ||
function SystemImportExpressionTransformer(state, params) { | ||
this.state = state; | ||
this.file = state.file; | ||
this.pluginOptions = this.state.opts; | ||
this.moduleType = this.pluginOptions.modules; | ||
var param = params[0]; | ||
this.importedModuleLiteral = t.literal(param.node.value); | ||
this.importedModuleLiteral = t.stringLiteral(param.node.value); | ||
var moduleName = getImportModuleName(this.file, this.importedModuleLiteral.value); | ||
this.moduleNameLiteral = t.literal(moduleName); | ||
this.moduleNameLiteral = t.stringLiteral(moduleName); | ||
} | ||
@@ -44,3 +48,3 @@ | ||
t.unaryExpression('typeof', t.identifier('window')), | ||
t.literal('undefined') | ||
t.stringLiteral('undefined') | ||
), | ||
@@ -91,3 +95,3 @@ t.identifier('window'), | ||
)), | ||
t.literal('function') | ||
t.stringLiteral('function') | ||
), | ||
@@ -129,3 +133,3 @@ t.memberExpression( | ||
t.unaryExpression('typeof', t.identifier('module')), | ||
t.literal('undefined') | ||
t.stringLiteral('undefined') | ||
), | ||
@@ -139,3 +143,3 @@ t.logicalExpression('&&', | ||
t.unaryExpression('typeof', t.identifier('require')), | ||
t.literal('undefined') | ||
t.stringLiteral('undefined') | ||
) | ||
@@ -153,3 +157,3 @@ ) | ||
t.unaryExpression('typeof', t.identifier('module')), | ||
t.literal('undefined') | ||
t.stringLiteral('undefined') | ||
), | ||
@@ -174,3 +178,3 @@ t.logicalExpression('&&', | ||
), | ||
t.literal('component') | ||
t.stringLiteral('component') | ||
) | ||
@@ -185,12 +189,10 @@ ) | ||
// resolve(require('./../utils/serializer')); | ||
var commonJSRequire = t.expressionStatement( | ||
t.callExpression( | ||
t.identifier('resolve'), [ | ||
t.callExpression( | ||
t.identifier('require'), | ||
[module] | ||
) | ||
] | ||
) | ||
var commonJSRequireExpression = t.callExpression( | ||
t.identifier('require'), | ||
// [module] // why this isn't working??? | ||
// [module, t.identifier('undefined')] // had to add extra undefined parameter or parenthesis !?!?!? | ||
[t.parenthesizedExpression(module)] | ||
); | ||
var commonJSRequire = this.createResolveExpressionStatement(commonJSRequireExpression); | ||
return commonJSRequire; | ||
@@ -201,13 +203,10 @@ }; | ||
var globalIdentifier = this.getGlobalIdentifier(); | ||
// resolve(global.localforageSerializer); | ||
var globalMemberExpression = t.memberExpression( | ||
globalIdentifier, | ||
module | ||
module, | ||
true // computed | ||
); | ||
globalMemberExpression.computed = true; | ||
var globalRequire = t.expressionStatement( | ||
t.callExpression( | ||
t.identifier('resolve'), [globalMemberExpression] | ||
) | ||
); | ||
var globalRequire = this.createResolveExpressionStatement(globalMemberExpression); | ||
return globalRequire; | ||
@@ -218,5 +217,5 @@ }; | ||
var moduleImportExpressions; | ||
if (this.file.opts.modules === 'amd') { | ||
if (this.moduleType === 'amd') { | ||
moduleImportExpressions = [this.getAmdRequire(this.moduleNameLiteral)]; | ||
} else if (this.file.opts.modules === 'common') { | ||
} else if (this.moduleType === 'common') { | ||
moduleImportExpressions = [this.getCommonJSRequire(this.importedModuleLiteral)]; | ||
@@ -248,11 +247,23 @@ } else { | ||
return new babel.Transformer('system-import-transformer', { | ||
CallExpression: function (node, parent, scope, file) { | ||
if (this.get('callee').matchesPattern('System.import')) { | ||
var params = this.get('arguments'); | ||
if (params.length && params[0].isLiteral()) { | ||
var transformer = new SystemImportExpressionTransformer(file, params); | ||
var transformedExpression = transformer.createTransformedExpression(); | ||
if (transformedExpression) { | ||
return t.expressionStatement(transformedExpression); | ||
SystemImportExpressionTransformer.prototype.createResolveExpressionStatement = function (parameter) { | ||
var result = t.expressionStatement( | ||
t.callExpression( | ||
t.identifier('resolve'), [parameter] | ||
) | ||
); | ||
return result; | ||
}; | ||
return { | ||
visitor: { | ||
CallExpression: function (path, state) { | ||
var callee = path.get('callee'); | ||
if (callee && callee.matchesPattern('System.import')) { | ||
var params = path.get('arguments'); | ||
if (params.length && params[0].isLiteral()) { | ||
var transformer = new SystemImportExpressionTransformer(state, params); | ||
var transformedExpression = transformer.createTransformedExpression(); | ||
if (transformedExpression) { | ||
path.replaceWith(transformedExpression); | ||
} | ||
} | ||
@@ -262,3 +273,3 @@ } | ||
} | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-system-import-transformer", | ||
"version": "1.1.5", | ||
"version": "2.0.0", | ||
"description": "Babel plugin that replaces System.import with the equivalent UMD pattern", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,2 +32,8 @@ # babel-plugin-system-import-transformer | ||
## Requirements | ||
- Babel v6.x.x | ||
**Note:** for babel v5 please use the [v1.x.x releases](https://github.com/thgreasi/babel-plugin-system-import-transformer/tree/v1.x.x-stable). | ||
## Installation | ||
@@ -40,2 +46,10 @@ | ||
```js | ||
// in .babelrc | ||
{ | ||
"plugins": [ | ||
"system-import-transformer" | ||
] | ||
} | ||
// in grunt.js | ||
babel: { | ||
@@ -72,7 +86,17 @@ options: { | ||
When babel is configured to use `AMD` or `CommonJS` modules | ||
When you are transforming to `AMD` or `CommonJS` modules you should set the respective plugin option: | ||
```js | ||
babel: { options: { modules: 'amd' } } | ||
// OR | ||
babel: { options: { modules: 'common' /* this is the default value for babel */ } } | ||
// AMD | ||
{ | ||
"plugins": [ | ||
["system-import-transformer", { "modules": "amd" }] | ||
] | ||
} | ||
// CommonJS | ||
{ | ||
"plugins": [ | ||
["system-import-transformer", { "modules": "common" }] | ||
] | ||
} | ||
``` | ||
@@ -92,1 +116,3 @@ `system-import-transformer` will omit the module type detection code and just insert the appropriate require statement wrapped with a `Promise`. | ||
``` | ||
**Note**: the default transpilation target is UMD |
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
13595
237
115