From 27da6de72303eda45e4074f4ef32befafba499a2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 1 Feb 2015 16:38:13 +1100 Subject: [PATCH] add back runtime - fixes #651 --- Makefile | 4 ++++ bin/6to5-runtime | 4 ++++ bin/6to5/index.js | 2 ++ lib/6to5/build-helpers.js | 13 +++++++++++++ lib/6to5/build-runtime.js | 26 ++++++++++++++++++++++++++ lib/6to5/file.js | 10 ++++++++-- lib/6to5/generation/index.js | 8 +++++--- lib/6to5/index.js | 2 ++ package.json | 3 ++- packages/build-runtime.js | 18 ++++++------------ 10 files changed, 72 insertions(+), 18 deletions(-) create mode 100755 bin/6to5-runtime create mode 100644 lib/6to5/build-helpers.js create mode 100644 lib/6to5/build-runtime.js diff --git a/Makefile b/Makefile index b597fd4faa..9a1cbf30a4 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ build: node $(BROWSERIFY_CMD) lib/6to5/browser.js -s to5 >dist/6to5.js node $(UGLIFY_CMD) dist/6to5.js >dist/6to5.min.js + node bin/6to5-runtime >dist/runtime.js + node $(UGLIFY_CMD) dist/runtime.js >dist/runtime.min.js + rm -rf templates.json clean: @@ -77,6 +80,7 @@ publish: make build cp dist/6to5.min.js browser.js cp dist/polyfill.min.js browser-polyfill.js + cp dist/runtime.min.js runtime.js node tools/cache-templates test -f templates.json diff --git a/bin/6to5-runtime b/bin/6to5-runtime new file mode 100755 index 0000000000..41b596c4de --- /dev/null +++ b/bin/6to5-runtime @@ -0,0 +1,4 @@ +#!/usr/bin/env node + +var runtime = require("../lib/6to5/build-runtime"); +console.log(runtime()); diff --git a/bin/6to5/index.js b/bin/6to5/index.js index a126afa14f..0dbd8f364e 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -11,6 +11,7 @@ commander.option("-t, --source-maps-inline", "Append sourceMappingURL comment to commander.option("-s, --source-maps", "Save source map alongside the compiled code"); commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin"); commander.option("-w, --watch", "Recompile files on changes"); +commander.option("-r, --runtime", "Replace 6to5 declarations with references to a runtime"); commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features"); commander.option("-p, --playground", "Enable playground support"); @@ -108,6 +109,7 @@ exports.opts = { sourceMap: commander.sourceMaps || commander.sourceMapsInline, optional: commander.optional, comments: !commander.removeComments, + runtime: commander.runtime, modules: commander.modules, loose: commander.loose }; diff --git a/lib/6to5/build-helpers.js b/lib/6to5/build-helpers.js new file mode 100644 index 0000000000..f9640abd27 --- /dev/null +++ b/lib/6to5/build-helpers.js @@ -0,0 +1,13 @@ +var File = require("./file"); +var util = require("./util"); +var each = require("lodash/collection/each"); +var t = require("./types"); + +module.exports = function (body, namespace) { + each(File.helpers, function (name) { + var key = t.identifier(t.toIdentifier(name)); + body.push(t.expressionStatement( + t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name)) + )); + }); +}; diff --git a/lib/6to5/build-runtime.js b/lib/6to5/build-runtime.js new file mode 100644 index 0000000000..bab3f1eb79 --- /dev/null +++ b/lib/6to5/build-runtime.js @@ -0,0 +1,26 @@ +"use strict"; + +var buildHelpers = require("./build-helpers"); +var generator = require("./generation"); +var util = require("./util"); +var t = require("./types"); +var _ = require("lodash"); + +module.exports = function () { + var namespace = t.identifier("to5Runtime"); + + var body = []; + var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body)); + var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("self-global")]))]); + + body.push(t.variableDeclaration("var", [ + t.variableDeclarator( + namespace, + t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([])) + ) + ])); + + buildHelpers(body, namespace); + + return generator(tree).code; +}; diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 54109ad026..20625f465c 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -24,7 +24,7 @@ function File(opts) { this.data = {}; this.lastStatements = []; - this.opts = File.normaliseOptions(opts); + this.opts = this.normaliseOptions(opts); this.ast = {}; this.buildTransformers(); @@ -76,6 +76,7 @@ File.validOptions = [ "playground", "experimental", "resolveModuleSource", + "runtime", // these are used by plugins "ignore", @@ -84,7 +85,7 @@ File.validOptions = [ "accept" ]; -File.normaliseOptions = function (opts) { +File.prototype.normaliseOptions = function (opts) { opts = clone(opts); for (var key in opts) { @@ -108,6 +109,7 @@ File.normaliseOptions = function (opts) { comments: true, filename: "unknown", modules: "common", + runtime: false, loose: [], code: true, ast: true @@ -146,6 +148,10 @@ File.normaliseOptions = function (opts) { opts.experimental = true; } + if (opts.runtime) { + this.set("runtimeIdentifier", t.identifier("to5Runtime")); + } + opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist); opts.whitelist = transform._ensureTransformerNames("whitelist", opts.whitelist); opts.optional = transform._ensureTransformerNames("optional", opts.optional); diff --git a/lib/6to5/generation/index.js b/lib/6to5/generation/index.js index 95113d2254..7311f0c440 100644 --- a/lib/6to5/generation/index.js +++ b/lib/6to5/generation/index.js @@ -40,9 +40,11 @@ each(Buffer.prototype, function (fn, key) { }); CodeGenerator.normaliseOptions = function (code, opts) { - var indent = detectIndent(code); - var style = indent.indent; - if (!style || style === " ") style = " "; + var style = " "; + if (code) { + var style = detectIndent(code).indent; + if (style && style !== " ") style = " "; + } return merge({ parentheses: true, diff --git a/lib/6to5/index.js b/lib/6to5/index.js index fb97f47c42..de4474c5d4 100644 --- a/lib/6to5/index.js +++ b/lib/6to5/index.js @@ -7,6 +7,8 @@ var isFunction = require("lodash/lang/isFunction"); exports.version = require("../../package").version; +exports.runtime = require("./build-runtime"); + exports.types = require("./types"); exports.register = function (opts) { diff --git a/package.json b/package.json index 52cf7cf0b2..2e93258a53 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "main": "lib/6to5/index.js", "bin": { "6to5": "./bin/6to5/index.js", - "6to5-node": "./bin/6to5-node" + "6to5-node": "./bin/6to5-node", + "6to5-runtime": "./bin/6to5-runtime" }, "browser": { "./lib/6to5/index.js": "./lib/6to5/browser.js", diff --git a/packages/build-runtime.js b/packages/build-runtime.js index 1970e593b6..46e8625ced 100644 --- a/packages/build-runtime.js +++ b/packages/build-runtime.js @@ -1,11 +1,10 @@ "use strict"; -var transform = require("../lib/6to5/transformation"); -var File = require("../lib/6to5/file"); -var util = require("../lib/6to5/util"); -var fs = require("fs"); -var t = require("../lib/6to5/types"); -var _ = require("lodash"); +var buildHelpers = require("../lib/6to5/build-helpers"); +var transform = require("../lib/6to5/transformation"); +var fs = require("fs"); +var t = require("../lib/6to5/types"); +var _ = require("lodash"); var relative = function (filename) { return __dirname + "/6to5-runtime/" + filename; @@ -35,12 +34,7 @@ var buildHelpers = function () { var body = []; var tree = t.program(body); - _.each(File.helpers, function (name) { - var key = t.identifier(t.toIdentifier(name)); - body.push(t.expressionStatement( - t.assignmentExpression("=", t.memberExpression(t.identifier("exports"), key), util.template(name)) - )); - }); + buildHelpers(body, t.identifier("exports")); return transform.fromAst(tree, null, { optional: ["selfContained"]