package/package.json000644 001750 001750 0000001060 12705142372013017 0ustar00000000 000000 { "name": "tariff", "version": "0.1.0", "description": "Crudely convert ES6 import/export declarations to CommonJS", "main": "index.js", "scripts": {"test": "node test.js"}, "repository": { "type": "git", "url": "git+ssh://git@github.com/marijnh/tariff.git" }, "author": "Marijn Haverbeke ", "license": "MIT", "bugs": { "url": "https://github.com/marijnh/tariff/issues" }, "homepage": "https://github.com/marijnh/tariff#readme", "dependencies": { "acorn": "^3.1.0", "source-map": "^0.5.3" } } package/.npmignore000644 001750 001750 0000000026 12705126275012535 0ustar00000000 000000 /node_modules .tern-* package/README.md000644 001750 001750 0000000600 12705142723012007 0ustar00000000 000000 # Tariff ``` var tariff = import("tariff") tariff('import {foo} from "./bar"\nexport let baz = foo(1)') // -> 'var foo = require("./bar").foo\nlet baz = exports.baz = foo(1)' ``` If you treat exports as values, not bindings, and don't do anything fancy, you can use tariff to convert ES6 `import` and `export` declarations into CommonJS equivalents. Released under an MIT license. package/LICENSE000644 001750 001750 0000002073 12705142735011546 0ustar00000000 000000 Copyright (C) 2016 by Marijn Haverbeke Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 001750 001750 0000007064 12705142006012202 0ustar00000000 000000 var acorn = require("acorn"), walk = require("acorn/dist/walk") /* base.ImportDeclaration = (node, st, c) => { for (let i = 0; i < node.specifiers.length; i++) c(node.specifiers[i], st) c(node.source, st, "Expression") } base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier */ function modVar(name) { return "m_" + name.replace(/\W+/g, "_") + "__" } module.exports = function(code) { var ast = acorn.parse(code, {sourceType: "module"}), patches = [] walk.simple(ast, { ImportDeclaration: function(node) { var req = "require(" + node.source.raw + ")", text if (node.specifiers.length == 0) { text = req } else if (node.specifiers.length > 1 || node.specifiers[0].type == "ImportDefaultSpecifier") { var name = modVar(node.source.value) text = "var " + name + " = " + req node.specifiers.forEach(function(spec) { if (spec.type == "ImportDefaultSpecifier") text += ", " + spec.local.name + " = " + name + ".default || " + name else if (name != null) text += ", " + spec.local.name + " = " + name + "." + spec.imported.name }) } else { text = "var " node.specifiers.forEach(function(spec) { if (spec.type == "ImportNamespaceSpecifier") text += spec.local.name + " = " + req else text += spec.local.name + " = " + req + "." + spec.imported.name }) } patches.push(node.start, node.end, text + ";") }, ExportNamedDeclaration: function(node) { if (node.source) { var name = modVar(node.source.value) var text = "var " + name + " = require(" + node.source.raw + ");" node.specifiers.forEach(function(spec) { text += " exports." + spec.exported.name + " = " + name + "." + spec.local.name + ";" }) patches.push(node.start, node.end, text) } else if (!node.declaration) { var text = "" node.specifiers.forEach(function(spec) { text += (text ? " " : "") + "exports." + spec.exported.name + " = " + spec.local.name + ";" }) patches.push(node.start, node.end, text) } else if (node.declaration.type == "VariableDeclaration") { patches.push(node.start, node.declaration.start, "") node.declaration.declarations.forEach(function(decl) { if (!decl.init) throw new RangeError("Exporting variable without a value") if (decl.id.type != "Identifier") throw new RangeError("Destructuring exports not supported") patches.push(decl.init.start, decl.init.start, "exports." + decl.id.name + " = ") }) } else { patches.push(node.start, node.declaration.start, "") patches.push(node.end, node.end, " exports." + node.declaration.id.name + " = " + node.declaration.id.name + ";") } }, ExportDefaultDeclaration: function(node) { if (/Declaration/.test(node.declaration.type)) { patches.push(node.start, node.declaration.start, "") patches.push(node.end, node.end, " module.exports = " + node.declaration.id.name + ";") } else { patches.push(node.start, node.declaration.start, " module.exports = ") } }, ExportAllDeclaration: function(node) { throw new RangeError("Tariff does not support `export *`") } }) var result = "", pos = code.length for (var i = patches.length - 3; i >= 0; i -= 3) { result = patches[i + 2] + code.slice(patches[i + 1], pos) + result pos = patches[i] } return code.slice(0, pos) + result } package/test.js000644 001750 001750 0000002446 12705136772012066 0ustar00000000 000000 var tariff = require("./index") var cases = { 'import foo from "bar"': 'var m_bar__ = require("bar"), foo = m_bar__.default || m_bar__;', 'import * as foo from "bar"': 'var foo = require("bar");', 'import {foo} from "bar"': 'var foo = require("bar").foo;', 'import {foo as baz} from "bar"': 'var baz = require("bar").foo;', 'import {foo, baz} from "bar"': 'var m_bar__ = require("bar"), foo = m_bar__.foo, baz = m_bar__.baz;', 'import {foo, baz as quux} from "bar"': 'var m_bar__ = require("bar"), foo = m_bar__.foo, quux = m_bar__.baz;', 'import * as foo from "bar"\n"more code"': 'var foo = require("bar");\n"more code"', 'export function bar() {}': 'function bar() {} exports.bar = bar;', 'export class baz {}': 'class baz {} exports.baz = baz;', 'export function bar() {} 1': 'function bar() {} exports.bar = bar; 1', 'export default function bar() {}': 'function bar() {} module.exports = bar;', 'export var x = 10': 'var x = exports.x = 10', 'export var x = 10, y = 20': 'var x = exports.x = 10, y = exports.y = 20', 'export {a, b}': 'exports.a = a; exports.b = b;', 'export {a, b as c}': 'exports.a = a; exports.c = b;' } for (var code in cases) { var result = tariff(code) if (cases[code] != result) console.log(" failed! wanted\n" + cases[code] + "\n got\n" + result) }