UNPKG

babel-plugin-transform-es2015-modules-simple-desctructure-amd

Version:
101 lines (81 loc) 4.62 kB
"use strict"; require("better-log/install"); var _babelTemplate = require("babel-template"); var _babelTemplate2 = _interopRequireDefault(_babelTemplate); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var buildModule = (0, _babelTemplate2.default)("\ndefine([IMPORT_PATHS], function(IMPORT_VARS) {\n NAMED_IMPORTS;\n BODY;\n});\n"); module.exports = function (_ref) { var t = _ref.types; return { visitor: { Program: { exit: function exit(path, file) { var body = path.get("body"), sources = [], anonymousSources = [], vars = [], namedImports = [], isModular = false, middleDefaultExportID = false; for (var i = 0; i < body.length; i++) { var _path = body[i], isLast = i == body.length - 1; if (_path.isExportDefaultDeclaration()) { var declaration = _path.get("declaration"); if (isLast) { _path.replaceWith(t.returnStatement(declaration.node)); } else { middleDefaultExportID = _path.scope.generateUidIdentifier("export_default"); _path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(middleDefaultExportID, declaration.node)])); } isModular = true; } if (_path.isImportDeclaration()) { (function () { var specifiers = _path.node.specifiers; if (specifiers.length == 0) { anonymousSources.push(_path.node.source); } else if (specifiers.length == 1 && specifiers[0].type == 'ImportDefaultSpecifier') { sources.push(_path.node.source); vars.push(specifiers[0]); } else if (specifiers.length > 1 && specifiers[0].type == 'ImportDefaultSpecifier') { sources.push(_path.node.source); vars.push(specifiers[0]); specifiers.forEach(function (_ref2, index) { var imported = _ref2.imported, local = _ref2.local; if (index != 0) { namedImports.push(t.variableDeclaration("var", [t.variableDeclarator(t.identifier(local.name), t.identifier(specifiers[0].local.name + '.' + imported.name))])); } }); } else { var importedID = _path.scope.generateUidIdentifier(_path.node.source.value); sources.push(_path.node.source); vars.push(importedID); specifiers.forEach(function (_ref3) { var imported = _ref3.imported, local = _ref3.local; namedImports.push(t.variableDeclaration("var", [t.variableDeclarator(t.identifier(local.name), t.identifier(importedID.name + '.' + imported.name))])); }); } _path.remove(); isModular = true; })(); } if (isLast && middleDefaultExportID) { _path.insertAfter(t.returnStatement(middleDefaultExportID)); } } if (isModular) { path.node.body = [buildModule({ IMPORT_PATHS: sources.concat(anonymousSources), IMPORT_VARS: vars, BODY: path.node.body, NAMED_IMPORTS: namedImports })]; } } } } }; };