diff --git a/.eslintignore b/.eslintignore index dc01ae1b9e..7eaabe1d10 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,3 +7,9 @@ packages/*/lib packages/*/dist packages/*/test/fixtures packages/*/test/tmp +packages/babel-standalone/babel.js +packages/babel-standalone/babel.min.js + +# Prettier tries to insert trailing commas in function calls, which Node.js +# doesn't natively support. This causes an error when loading the Gulp tasks. +packages/babel-standalone/src/gulpTasks.js diff --git a/.gitignore b/.gitignore index f0052bad05..adf691453e 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ package-lock.json /packages/*/lib .nyc_output /babel.sublime-workspace +packages/babel-standalone/babel.js +packages/babel-standalone/babel.min.js diff --git a/.travis.yml b/.travis.yml index 61086501e5..adef236322 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,9 @@ before_install: - curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.28.1 script: + # Babel doesn't build correctly with npm v2 (which comes bundled with Node v4) + # so we manually upgrade to a known working npm version. + - npm install --global npm@4 - 'if [ "$JOB" = "test" ]; then make test-ci; fi' - 'if [ "$JOB" = "lint" ]; then make lint && make flow; fi' diff --git a/Gulpfile.js b/Gulpfile.js index 04535b387b..647fa4d9e0 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -9,6 +9,7 @@ const watch = require("gulp-watch"); const gutil = require("gulp-util"); const gulp = require("gulp"); const path = require("path"); +const registerBabelStandaloneTasks = require("./packages/babel-standalone/src/gulpTasks"); const base = path.join(__dirname, "packages"); const scripts = "./packages/*/src/**/*.js"; @@ -59,3 +60,5 @@ gulp.task("watch", ["build"], function() { gulp.start("build"); }); }); + +registerBabelStandaloneTasks(gulp); diff --git a/Makefile b/Makefile index 7223a0f777..c34c6d8374 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ export FORCE_COLOR = true build: clean rm -rf packages/*/lib ./node_modules/.bin/gulp build +ifneq ($(BABEL_ENV), "cov") + ./node_modules/.bin/gulp build-babel-standalone +endif build-dist: build cd packages/babel-polyfill; \ diff --git a/circle.yml b/circle.yml index 042b1eada4..31b3b4fddd 100644 --- a/circle.yml +++ b/circle.yml @@ -1,3 +1,8 @@ +general: + artifacts: + - "packages/babel-standalone/babel.js" + - "packages/babel-standalone/babel.min.js" + machine: node: version: @@ -14,3 +19,9 @@ dependencies: test: override: - make test-ci-coverage + # Builds babel-standalone with the regular Babel config + - make build + # test-ci-coverage doesn't test babel-standalone, as trying to gather coverage + # data for a JS file that's several megabytes large is bound to fail. Here, + # we just run the babel-standalone test separately. + - ./node_modules/mocha/bin/_mocha packages/babel-standalone/test/ --opts test/mocha.opts diff --git a/package.json b/package.json index 4d218cb845..6c831f6556 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,13 @@ "babel-cli": "7.0.0-alpha.18", "babel-core": "7.0.0-alpha.18", "babel-eslint": "8.0.0-alpha.15", + "babel-loader": "^7.1.1", "babel-plugin-istanbul": "^4.1.4", "babel-preset-env": "2.0.0-alpha.18", "babel-preset-flow": "7.0.0-alpha.18", "babel-preset-stage-0": "7.0.0-alpha.18", "babel-register": "7.0.0-alpha.18", + "babylon": "7.0.0-beta.18", "browserify": "^13.1.1", "bundle-collapser": "^1.2.1", "chai": "^4.1.0", @@ -58,7 +60,8 @@ "all": true, "exclude": [ "scripts/*.js", - "packages/*/test/**" + "packages/*/test/**", + "packages/babel-standalone/**" ], "sourceMap": false, "instrument": false diff --git a/packages/babel-standalone/README.md b/packages/babel-standalone/README.md new file mode 100644 index 0000000000..ced004a500 --- /dev/null +++ b/packages/babel-standalone/README.md @@ -0,0 +1,93 @@ +babel-standalone +================ + +babel-standalone is a standalone build of Babel for use in non-Node.js environments, including browsers. It's bundled with all the standard Babel plugins and presets, and [a build of babili (babel-minify)](http://dl.vc/babili-standalone) is optionally available too. + +But why?! +========= + +It's true that using Babel through Webpack, Browserify or Gulp should be sufficient for most use cases. However, there are some valid use cases for babel-standalone: + + - Sites like [JSFiddle](https://jsfiddle.net/), [JS Bin](https://jsbin.com/), the [REPL on the Babel site](http://babeljs.io/repl/), etc. These sites compile user-provided JavaScript in real-time. + - Apps that embed a JavaScript engine such as V8 directly, and want to use Babel for compilation + - Apps that want to use JavaScript as a scripting language for extending the app itself, including all the goodies that ES2015 provides. + - Integration of Babel into a non-Node.js environment ([ReactJS.NET](http://reactjs.net/), [ruby-babel-transpiler](https://github.com/babel/ruby-babel-transpiler), [php-babel-transpiler](https://github.com/talyssonoc/php-babel-transpiler), etc). + +Installation +============ + +There are several ways to get a copy of babel-standalone. Pick whichever one you like: + +- Use it via UNPKG: https://unpkg.com/babel-standalone@6/babel.min.js. This is a simple way to embed it on a webpage without having to do any other setup. +- Install via Bower: `bower install babel-standalone` +- Install via NPM: `npm install --save babel-standalone` +- Manually grab `babel.js` and/or `babel.min.js` from the [GitHub releases page](https://github.com/Daniel15/babel-standalone/releases). Every release includes these files. +- Install it via Git: You can use the repo at https://github.com/Daniel15/babel-standalone-bower to pull a prebuilt version from Git. Note that this is generally only advised for systems that *must* pull artifacts from Git, such as Bower. + +Usage +===== + +Load `babel.js` or `babel.min.js` in your environment. This will expose [Babel's API](http://babeljs.io/docs/usage/api/) in a `Babel` object: + +```js +var input = 'const getMessage = () => "Hello World";'; +var output = Babel.transform(input, { presets: ['es2015'] }).code; +``` + +When loaded in a browser, babel-standalone will automatically compile and execute all script tags with type `text/babel` or `text/jsx`: +```html +
+ + + + +``` + +You can use the `data-plugins` and `data-presets` attributes to specify the Babel plugins/presets to use: +```html + +``` + +Note that `.babelrc` doesn't work in babel-standalone, as no file system access is available. The presets and/or plugins to use **must** be specified in the options passed to `Babel.transform`. + +Customisation +============= +Custom plugins and presets can be added using the `registerPlugin` and `registerPreset` methods respectively: + +```js +// Simple plugin that converts every identifier to "LOL" +function lolizer() { + return { + visitor: { + Identifier(path) { + path.node.name = 'LOL'; + } + } + } +} +Babel.registerPlugin('lolizer', lolizer); +``` + +Once registered, just use the name of the plugin: + +```js +var output = Babel.transform( + 'function helloWorld() { alert(hello); }', + {plugins: ['lolizer']} +); +// Returns "function LOL() { LOL(LOL); }" +``` + +Custom plugins also work for inline ` + + + diff --git a/packages/babel-standalone/examples/example.js b/packages/babel-standalone/examples/example.js new file mode 100644 index 0000000000..f994d46444 --- /dev/null +++ b/packages/babel-standalone/examples/example.js @@ -0,0 +1,8 @@ +/* global Babel */ + +const doStuff = () => { + const name = "world"; + document.getElementById("output").innerHTML = `Hello ${name}`; + document.getElementById("version").innerHTML = Babel.version; +}; +doStuff(); diff --git a/packages/babel-standalone/examples/scriptTag-custom.htm b/packages/babel-standalone/examples/scriptTag-custom.htm new file mode 100644 index 0000000000..e2b8fbbf17 --- /dev/null +++ b/packages/babel-standalone/examples/scriptTag-custom.htm @@ -0,0 +1,30 @@ + + + + + babel-standalone example - Script tag + + + Using Babel : +
Loading...
+ + + + + diff --git a/packages/babel-standalone/examples/scriptTag-inline.htm b/packages/babel-standalone/examples/scriptTag-inline.htm new file mode 100644 index 0000000000..7ee66d4ec4 --- /dev/null +++ b/packages/babel-standalone/examples/scriptTag-inline.htm @@ -0,0 +1,21 @@ + + + + + babel-standalone example - Script tag + + + Using Babel : +
Loading...
+ + + + + diff --git a/packages/babel-standalone/examples/scriptTag-src.htm b/packages/babel-standalone/examples/scriptTag-src.htm new file mode 100644 index 0000000000..93cd20de9a --- /dev/null +++ b/packages/babel-standalone/examples/scriptTag-src.htm @@ -0,0 +1,14 @@ + + + + + babel-standalone example - External script + + + Using Babel : +
Loading...
+ + + + + diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json new file mode 100644 index 0000000000..3fddafefae --- /dev/null +++ b/packages/babel-standalone/package.json @@ -0,0 +1,122 @@ +{ + "name": "babel-standalone", + "version": "7.0.0-alpha.19", + "description": "Standalone build of Babel for use in non-Node.js environments.", + "main": "babel.js", + "files": [ + "babel.js", + "babel.min.js", + "src" + ], + "devDependencies": { + "babel-core": "7.0.0-alpha.19", + "babel-helper-builder-react-jsx": "7.0.0-alpha.19", + "babel-loader": "7.1.1", + "babel-plugin-check-es2015-constants": "7.0.0-alpha.19", + "babel-plugin-external-helpers": "7.0.0-alpha.19", + "babel-plugin-inline-replace-variables": "1.3.1", + "babel-plugin-syntax-async-functions": "7.0.0-alpha.19", + "babel-plugin-syntax-async-generators": "7.0.0-alpha.19", + "babel-plugin-syntax-class-properties": "7.0.0-alpha.19", + "babel-plugin-syntax-decorators": "7.0.0-alpha.19", + "babel-plugin-syntax-do-expressions": "7.0.0-alpha.19", + "babel-plugin-syntax-exponentiation-operator": "7.0.0-alpha.19", + "babel-plugin-syntax-export-extensions": "7.0.0-alpha.19", + "babel-plugin-syntax-flow": "7.0.0-alpha.19", + "babel-plugin-syntax-function-bind": "7.0.0-alpha.19", + "babel-plugin-syntax-function-sent": "7.0.0-alpha.19", + "babel-plugin-syntax-jsx": "7.0.0-alpha.19", + "babel-plugin-syntax-object-rest-spread": "7.0.0-alpha.19", + "babel-plugin-syntax-trailing-function-commas": "7.0.0-alpha.19", + "babel-plugin-transform-async-functions": "7.0.0-alpha.19", + "babel-plugin-transform-async-to-generator": "7.0.0-alpha.19", + "babel-plugin-transform-async-to-module-method": "7.0.0-alpha.19", + "babel-plugin-transform-class-properties": "7.0.0-alpha.19", + "babel-plugin-transform-decorators": "7.0.0-alpha.19", + "babel-plugin-transform-do-expressions": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-arrow-functions": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-block-scoped-functions": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-block-scoping": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-classes": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-computed-properties": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-destructuring": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-duplicate-keys": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-for-of": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-function-name": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-instanceof": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-literals": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-modules-amd": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-modules-commonjs": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-modules-systemjs": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-modules-umd": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-object-super": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-parameters": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-shorthand-properties": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-spread": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-sticky-regex": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-template-literals": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-typeof-symbol": "7.0.0-alpha.19", + "babel-plugin-transform-es2015-unicode-regex": "7.0.0-alpha.19", + "babel-plugin-transform-es3-member-expression-literals": "7.0.0-alpha.19", + "babel-plugin-transform-es3-property-literals": "7.0.0-alpha.19", + "babel-plugin-transform-es5-property-mutators": "7.0.0-alpha.19", + "babel-plugin-transform-eval": "7.0.0-alpha.19", + "babel-plugin-transform-exponentiation-operator": "7.0.0-alpha.19", + "babel-plugin-transform-export-extensions": "7.0.0-alpha.19", + "babel-plugin-transform-flow-comments": "7.0.0-alpha.19", + "babel-plugin-transform-flow-strip-types": "7.0.0-alpha.19", + "babel-plugin-transform-function-bind": "7.0.0-alpha.19", + "babel-plugin-transform-jscript": "7.0.0-alpha.19", + "babel-plugin-transform-object-assign": "7.0.0-alpha.19", + "babel-plugin-transform-object-rest-spread": "7.0.0-alpha.19", + "babel-plugin-transform-object-set-prototype-of-to-assign": "7.0.0-alpha.19", + "babel-plugin-transform-proto-to-assign": "7.0.0-alpha.19", + "babel-plugin-transform-react-constant-elements": "7.0.0-alpha.19", + "babel-plugin-transform-react-display-name": "7.0.0-alpha.19", + "babel-plugin-transform-react-inline-elements": "7.0.0-alpha.19", + "babel-plugin-transform-react-jsx": "7.0.0-alpha.19", + "babel-plugin-transform-react-jsx-compat": "7.0.0-alpha.19", + "babel-plugin-transform-react-jsx-self": "7.0.0-alpha.19", + "babel-plugin-transform-react-jsx-source": "7.0.0-alpha.19", + "babel-plugin-transform-regenerator": "7.0.0-alpha.19", + "babel-plugin-transform-runtime": "7.0.0-alpha.19", + "babel-plugin-transform-strict-mode": "7.0.0-alpha.19", + "babel-preset-env": "2.0.0-alpha.19", + "babel-preset-es2015": "7.0.0-alpha.19", + "babel-preset-es2016": "7.0.0-alpha.19", + "babel-preset-es2017": "7.0.0-alpha.19", + "babel-preset-react": "7.0.0-alpha.19", + "babel-preset-stage-0": "7.0.0-alpha.19", + "babel-preset-stage-1": "7.0.0-alpha.19", + "babel-preset-stage-2": "7.0.0-alpha.19", + "babel-preset-stage-3": "7.0.0-alpha.19", + "gulp-rename": "^1.2.2", + "gulp-uglify": "^3.0.0", + "json-loader": "^0.5.7", + "lazypipe": "^1.0.1", + "pump": "^1.0.2", + "react": "^16.0.0-beta.3", + "webpack": "^3.4.1", + "webpack-dependency-suite": "^2.4.4", + "webpack-stream": "^4.0.0" + }, + "keywords": [ + "babel", + "babeljs", + "6to5", + "transpile", + "transpiler" + ], + "author": "Daniel Lo Nigro (http://dan.cx/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/babel/babel-standalone/issues" + }, + "homepage": "https://github.com/babel/babel-standalone#readme", + "jest": { + "transformIgnorePatterns": [ + "/node_modules/", + "babel.js" + ] + } +} diff --git a/packages/babel-standalone/src/babel-package-shim.js b/packages/babel-standalone/src/babel-package-shim.js new file mode 100644 index 0000000000..a67c572f3e --- /dev/null +++ b/packages/babel-standalone/src/babel-package-shim.js @@ -0,0 +1,8 @@ +/** + * A shim that replaces Babel's require('package.json') statement. + * Babel requires the entire package.json file just to get the version number. + */ + +/* global BABEL_VERSION */ + +export const version = BABEL_VERSION; diff --git a/packages/babel-standalone/src/gulpTasks.js b/packages/babel-standalone/src/gulpTasks.js new file mode 100644 index 0000000000..7718809987 --- /dev/null +++ b/packages/babel-standalone/src/gulpTasks.js @@ -0,0 +1,123 @@ +/** + * This file contains the Gulp tasks for babel-standalone. Note that + * babel-standalone is compiled using Webpack, and performs its own Babel + * compilation of all the JavaScript files. This is because it targets web + * browsers, so more transforms are needed than the regular Babel builds that + * only target Node.js. + */ + +const pump = require("pump"); +const rename = require("gulp-rename"); +const RootMostResolvePlugin = require("webpack-dependency-suite").RootMostResolvePlugin; +const webpack = require("webpack"); +const webpackStream = require("webpack-stream"); +const uglify = require("gulp-uglify"); + +function webpackBuild(filename, libraryName, version) { + // If this build is part of a pull request, include the pull request number in + // the version number. + if (process.env.CIRCLE_PR_NUMBER) { + version += '+pr.' + process.env.CIRCLE_PR_NUMBER; + } + const typeofPlugin = require("babel-plugin-transform-es2015-typeof-symbol") + .default; + + // babel-plugin-transform-es2015-typeof-symbol is not idempotent, and something + // else is already running it, so we need to exclude it from the transform. + const preset2015 = require("babel-preset-es2015").default(); + const es2015WithoutTypeof = { + plugins: preset2015.plugins.filter(plugin => plugin !== typeofPlugin), + }; + + const config = { + module: { + rules: [ + { + //exclude: /node_modules/, + test: /\.js$/, + loader: "babel-loader", + options: { + // Some of the node_modules may have their own "babel" section in + // their project.json (or a ".babelrc" file). We need to ignore + // those as we're using our own Babel options. + babelrc: false, + presets: [es2015WithoutTypeof, require("babel-preset-stage-0")], + }, + }, + ], + }, + node: { + // Mock Node.js modules that Babel require()s but that we don't + // particularly care about. + fs: "empty", + module: "empty", + net: "empty", + }, + output: { + filename: filename, + library: libraryName, + libraryTarget: "umd", + }, + plugins: [ + new webpack.DefinePlugin({ + "process.env.NODE_ENV": '"production"', + BABEL_VERSION: + JSON.stringify(require("babel-core/package.json").version), + VERSION: JSON.stringify(version), + }), + // Use browser version of visionmedia-debug + new webpack.NormalModuleReplacementPlugin( + /debug\/node/, + "debug/src/browser" + ), + /*new webpack.NormalModuleReplacementPlugin( + /..\/..\/package/, + "../../../../src/babel-package-shim" + ),*/ + new webpack.optimize.ModuleConcatenationPlugin(), + ], + resolve: { + plugins: [ + // Dedupe packages that are used across multiple plugins. + // This replaces DedupePlugin from Webpack 1.x + new RootMostResolvePlugin(__dirname + '/../../../', true), + ], + }, + }; + + if (libraryName !== "Babel") { + // This is a secondary package (eg. Babili), we should expect that Babel + // was already loaded, rather than bundling it in here too. + config.externals = { + "babel-standalone": "Babel", + }; + } + return webpackStream(config, webpack); + // To write JSON for debugging: + /*return webpackStream(config, webpack, (err, stats) => { + require('gulp-util').log(stats.toString({colors: true})); + require('fs').writeFileSync('webpack-debug.json', JSON.stringify(stats.toJson())); + });*/ +} + +function registerGulpTasks(gulp) { + gulp.task("build-babel-standalone", ["build"], cb => { + pump( + [ + gulp.src(__dirname + "/index.js"), + webpackBuild( + "babel.js", + "Babel", + require(__dirname + "/../package.json").version + ), + gulp.dest(__dirname + "/../"), + uglify(), + rename({ extname: ".min.js" }), + gulp.dest(__dirname + "/../"), + ], + cb + ); + }); +} + +module.exports = registerGulpTasks; diff --git a/packages/babel-standalone/src/index.js b/packages/babel-standalone/src/index.js new file mode 100644 index 0000000000..0281596c2e --- /dev/null +++ b/packages/babel-standalone/src/index.js @@ -0,0 +1,317 @@ +/** + * Entry point for babel-standalone. This wraps Babel's API in a version that's + * friendlier for use in web browers. It removes the automagical detection of + * plugins, instead explicitly registering all the available plugins and + * presets, and requiring custom ones to be registered through `registerPlugin` + * and `registerPreset` respectively. + */ + +/* global VERSION */ +/* eslint-disable max-len */ + +import * as Babel from "babel-core"; + +import { runScripts } from "./transformScriptTags"; + +const isArray = + Array.isArray || + (arg => Object.prototype.toString.call(arg) === "[object Array]"); + +/** + * Loads the given name (or [name, options] pair) from the given table object + * holding the available presets or plugins. + * + * Returns undefined if the preset or plugin is not available; passes through + * name unmodified if it (or the first element of the pair) is not a string. + */ +function loadBuiltin(builtinTable, name) { + if (isArray(name) && typeof name[0] === "string") { + if (builtinTable.hasOwnProperty(name[0])) { + return [builtinTable[name[0]]].concat(name.slice(1)); + } + return; + } else if (typeof name === "string") { + return builtinTable[name]; + } + // Could be an actual preset/plugin module + return name; +} + +/** + * Parses plugin names and presets from the specified options. + */ +function processOptions(options) { + // Parse preset names + const presets = (options.presets || []).map(presetName => { + const preset = loadBuiltin(availablePresets, presetName); + + if (preset) { + // workaround for babel issue + // at some point, babel copies the preset, losing the non-enumerable + // buildPreset key; convert it into an enumerable key. + if ( + isArray(preset) && + typeof preset[0] === "object" && + preset[0].hasOwnProperty("buildPreset") + ) { + preset[0] = { ...preset[0], buildPreset: preset[0].buildPreset }; + } + } else { + throw new Error( + `Invalid preset specified in Babel options: "${presetName}"`, + ); + } + return preset; + }); + + // Parse plugin names + const plugins = (options.plugins || []).map(pluginName => { + const plugin = loadBuiltin(availablePlugins, pluginName); + + if (!plugin) { + throw new Error( + `Invalid plugin specified in Babel options: "${pluginName}"`, + ); + } + return plugin; + }); + + return { + babelrc: false, + ...options, + presets, + plugins, + }; +} + +export function transform(code, options) { + return Babel.transform(code, processOptions(options)); +} + +export function transformFromAst(ast, code, options) { + return Babel.transformFromAst(ast, code, processOptions(options)); +} +export const availablePlugins = {}; +export const availablePresets = {}; +export const buildExternalHelpers = Babel.buildExternalHelpers; +/** + * Registers a named plugin for use with Babel. + */ +export function registerPlugin(name, plugin) { + if (availablePlugins.hasOwnProperty(name)) { + console.warn( + `A plugin named "${name}" is already registered, it will be overridden`, + ); + } + availablePlugins[name] = plugin; +} +/** + * Registers multiple plugins for use with Babel. `newPlugins` should be an object where the key + * is the name of the plugin, and the value is the plugin itself. + */ +export function registerPlugins(newPlugins) { + Object.keys(newPlugins).forEach(name => + registerPlugin(name, newPlugins[name]), + ); +} + +/** + * Registers a named preset for use with Babel. + */ +export function registerPreset(name, preset) { + if (availablePresets.hasOwnProperty(name)) { + console.warn( + `A preset named "${name}" is already registered, it will be overridden`, + ); + } + availablePresets[name] = preset; +} +/** + * Registers multiple presets for use with Babel. `newPresets` should be an object where the key + * is the name of the preset, and the value is the preset itself. + */ +export function registerPresets(newPresets) { + Object.keys(newPresets).forEach(name => + registerPreset(name, newPresets[name]), + ); +} + +// All the plugins we should bundle +registerPlugins({ + "check-es2015-constants": require("babel-plugin-check-es2015-constants"), + "external-helpers": require("babel-plugin-external-helpers"), + "syntax-async-functions": require("babel-plugin-syntax-async-functions"), + "syntax-async-generators": require("babel-plugin-syntax-async-generators"), + "syntax-class-properties": require("babel-plugin-syntax-class-properties"), + "syntax-decorators": require("babel-plugin-syntax-decorators"), + "syntax-do-expressions": require("babel-plugin-syntax-do-expressions"), + "syntax-exponentiation-operator": require("babel-plugin-syntax-exponentiation-operator"), + "syntax-export-extensions": require("babel-plugin-syntax-export-extensions"), + "syntax-flow": require("babel-plugin-syntax-flow"), + "syntax-function-bind": require("babel-plugin-syntax-function-bind"), + "syntax-function-sent": require("babel-plugin-syntax-function-sent"), + "syntax-jsx": require("babel-plugin-syntax-jsx"), + "syntax-object-rest-spread": require("babel-plugin-syntax-object-rest-spread"), + "syntax-trailing-function-commas": require("babel-plugin-syntax-trailing-function-commas"), + "transform-async-functions": require("babel-plugin-transform-async-functions"), + "transform-async-to-generator": require("babel-plugin-transform-async-to-generator"), + "transform-async-to-module-method": require("babel-plugin-transform-async-to-module-method"), + "transform-class-properties": require("babel-plugin-transform-class-properties"), + "transform-decorators": require("babel-plugin-transform-decorators"), + "transform-do-expressions": require("babel-plugin-transform-do-expressions"), + "transform-es2015-arrow-functions": require("babel-plugin-transform-es2015-arrow-functions"), + "transform-es2015-block-scoped-functions": require("babel-plugin-transform-es2015-block-scoped-functions"), + "transform-es2015-block-scoping": require("babel-plugin-transform-es2015-block-scoping"), + "transform-es2015-classes": require("babel-plugin-transform-es2015-classes"), + "transform-es2015-computed-properties": require("babel-plugin-transform-es2015-computed-properties"), + "transform-es2015-destructuring": require("babel-plugin-transform-es2015-destructuring"), + "transform-es2015-duplicate-keys": require("babel-plugin-transform-es2015-duplicate-keys"), + "transform-es2015-for-of": require("babel-plugin-transform-es2015-for-of"), + "transform-es2015-function-name": require("babel-plugin-transform-es2015-function-name"), + "transform-es2015-instanceof": require("babel-plugin-transform-es2015-instanceof"), + "transform-es2015-literals": require("babel-plugin-transform-es2015-literals"), + "transform-es2015-modules-amd": require("babel-plugin-transform-es2015-modules-amd"), + "transform-es2015-modules-commonjs": require("babel-plugin-transform-es2015-modules-commonjs"), + "transform-es2015-modules-systemjs": require("babel-plugin-transform-es2015-modules-systemjs"), + "transform-es2015-modules-umd": require("babel-plugin-transform-es2015-modules-umd"), + "transform-es2015-object-super": require("babel-plugin-transform-es2015-object-super"), + "transform-es2015-parameters": require("babel-plugin-transform-es2015-parameters"), + "transform-es2015-shorthand-properties": require("babel-plugin-transform-es2015-shorthand-properties"), + "transform-es2015-spread": require("babel-plugin-transform-es2015-spread"), + "transform-es2015-sticky-regex": require("babel-plugin-transform-es2015-sticky-regex"), + "transform-es2015-template-literals": require("babel-plugin-transform-es2015-template-literals"), + "transform-es2015-typeof-symbol": require("babel-plugin-transform-es2015-typeof-symbol"), + "transform-es2015-unicode-regex": require("babel-plugin-transform-es2015-unicode-regex"), + "transform-es3-member-expression-literals": require("babel-plugin-transform-es3-member-expression-literals"), + "transform-es3-property-literals": require("babel-plugin-transform-es3-property-literals"), + "transform-es5-property-mutators": require("babel-plugin-transform-es5-property-mutators"), + "transform-eval": require("babel-plugin-transform-eval"), + "transform-exponentiation-operator": require("babel-plugin-transform-exponentiation-operator"), + "transform-export-extensions": require("babel-plugin-transform-export-extensions"), + "transform-flow-comments": require("babel-plugin-transform-flow-comments"), + "transform-flow-strip-types": require("babel-plugin-transform-flow-strip-types"), + "transform-function-bind": require("babel-plugin-transform-function-bind"), + "transform-jscript": require("babel-plugin-transform-jscript"), + "transform-object-assign": require("babel-plugin-transform-object-assign"), + "transform-object-rest-spread": require("babel-plugin-transform-object-rest-spread"), + "transform-object-set-prototype-of-to-assign": require("babel-plugin-transform-object-set-prototype-of-to-assign"), + "transform-proto-to-assign": require("babel-plugin-transform-proto-to-assign"), + "transform-react-constant-elements": require("babel-plugin-transform-react-constant-elements"), + "transform-react-display-name": require("babel-plugin-transform-react-display-name"), + "transform-react-inline-elements": require("babel-plugin-transform-react-inline-elements"), + "transform-react-jsx": require("babel-plugin-transform-react-jsx"), + "transform-react-jsx-compat": require("babel-plugin-transform-react-jsx-compat"), + "transform-react-jsx-self": require("babel-plugin-transform-react-jsx-self"), + "transform-react-jsx-source": require("babel-plugin-transform-react-jsx-source"), + "transform-regenerator": require("babel-plugin-transform-regenerator"), + "transform-runtime": require("babel-plugin-transform-runtime"), + "transform-strict-mode": require("babel-plugin-transform-strict-mode"), +}); + +// All the presets we should bundle +registerPresets({ + es2015: require("babel-preset-es2015"), + es2016: require("babel-preset-es2016"), + es2017: require("babel-preset-es2017"), + react: require("babel-preset-react"), + "stage-0": require("babel-preset-stage-0"), + "stage-1": require("babel-preset-stage-1"), + "stage-2": require("babel-preset-stage-2"), + "stage-3": require("babel-preset-stage-3"), + + // ES2015 preset with es2015-modules-commonjs removed + // Plugin list copied from babel-preset-es2015/index.js + "es2015-no-commonjs": { + plugins: [ + require("babel-plugin-transform-es2015-template-literals"), + require("babel-plugin-transform-es2015-literals"), + require("babel-plugin-transform-es2015-function-name"), + require("babel-plugin-transform-es2015-arrow-functions"), + require("babel-plugin-transform-es2015-block-scoped-functions"), + require("babel-plugin-transform-es2015-classes"), + require("babel-plugin-transform-es2015-object-super"), + require("babel-plugin-transform-es2015-shorthand-properties"), + require("babel-plugin-transform-es2015-computed-properties"), + require("babel-plugin-transform-es2015-for-of"), + require("babel-plugin-transform-es2015-sticky-regex"), + require("babel-plugin-transform-es2015-unicode-regex"), + require("babel-plugin-check-es2015-constants"), + require("babel-plugin-transform-es2015-spread"), + require("babel-plugin-transform-es2015-parameters"), + require("babel-plugin-transform-es2015-destructuring"), + require("babel-plugin-transform-es2015-block-scoping"), + require("babel-plugin-transform-es2015-typeof-symbol"), + [ + require("babel-plugin-transform-regenerator"), + { async: false, asyncGenerators: false }, + ], + ], + }, + + // ES2015 preset with plugins set to loose mode. + // Based off https://github.com/bkonkle/babel-preset-es2015-loose/blob/master/index.js + "es2015-loose": { + plugins: [ + [ + require("babel-plugin-transform-es2015-template-literals"), + { loose: true }, + ], + require("babel-plugin-transform-es2015-literals"), + require("babel-plugin-transform-es2015-function-name"), + require("babel-plugin-transform-es2015-arrow-functions"), + require("babel-plugin-transform-es2015-block-scoped-functions"), + [require("babel-plugin-transform-es2015-classes"), { loose: true }], + require("babel-plugin-transform-es2015-object-super"), + require("babel-plugin-transform-es2015-shorthand-properties"), + require("babel-plugin-transform-es2015-duplicate-keys"), + [ + require("babel-plugin-transform-es2015-computed-properties"), + { loose: true }, + ], + [require("babel-plugin-transform-es2015-for-of"), { loose: true }], + require("babel-plugin-transform-es2015-sticky-regex"), + require("babel-plugin-transform-es2015-unicode-regex"), + require("babel-plugin-check-es2015-constants"), + [require("babel-plugin-transform-es2015-spread"), { loose: true }], + require("babel-plugin-transform-es2015-parameters"), + [require("babel-plugin-transform-es2015-destructuring"), { loose: true }], + require("babel-plugin-transform-es2015-block-scoping"), + require("babel-plugin-transform-es2015-typeof-symbol"), + [ + require("babel-plugin-transform-es2015-modules-commonjs"), + { loose: true }, + ], + [ + require("babel-plugin-transform-regenerator"), + { async: false, asyncGenerators: false }, + ], + ], + }, +}); + +export const version = VERSION; + +// Listen for load event if we're in a browser and then kick off finding and +// running of scripts with "text/babel" type. +if (typeof window !== "undefined" && window && window.addEventListener) { + window.addEventListener( + "DOMContentLoaded", + () => transformScriptTags(), + false, + ); +} + +/** + * Transform