From 93cf26abca5e6aa4e681ed987f3a27a91fcc9c8d Mon Sep 17 00:00:00 2001 From: Daniel Lo Nigro Date: Tue, 22 Aug 2017 13:46:30 -0700 Subject: [PATCH] Fix babel-standalone for realz (#6137) * Fix babel-standalone * Fix infinite loop in Makefile (oops) * Override Node.js module resolution to handle babel-core --- Gulpfile.js | 3 --- Makefile | 5 +++- package.json | 1 - packages/babel-standalone/Gulpfile.js | 6 +++++ packages/babel-standalone/src/gulpTasks.js | 27 ++++++++++++------- .../src/overrideModuleResolution.js | 24 +++++++++++++++++ yarn.lock | 2 +- 7 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 packages/babel-standalone/Gulpfile.js create mode 100644 packages/babel-standalone/src/overrideModuleResolution.js diff --git a/Gulpfile.js b/Gulpfile.js index 647fa4d9e0..04535b387b 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -9,7 +9,6 @@ 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"; @@ -60,5 +59,3 @@ gulp.task("watch", ["build"], function() { gulp.start("build"); }); }); - -registerBabelStandaloneTasks(gulp); diff --git a/Makefile b/Makefile index c34c6d8374..a2730fb101 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,12 @@ build: clean rm -rf packages/*/lib ./node_modules/.bin/gulp build ifneq ($(BABEL_ENV), "cov") - ./node_modules/.bin/gulp build-babel-standalone + make build-standalone endif +build-standalone: + ./node_modules/.bin/gulp build-babel-standalone --cwd=packages/babel-standalone/ + build-dist: build cd packages/babel-polyfill; \ scripts/build-dist.sh diff --git a/package.json b/package.json index e821482718..d0244ee38a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "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", diff --git a/packages/babel-standalone/Gulpfile.js b/packages/babel-standalone/Gulpfile.js new file mode 100644 index 0000000000..ab778ace06 --- /dev/null +++ b/packages/babel-standalone/Gulpfile.js @@ -0,0 +1,6 @@ +const registerBabelStandaloneTask = require("./src/gulpTasks") + .registerBabelStandaloneTask; +const gulp = require("gulp"); + +const version = require("./package.json").version; +registerBabelStandaloneTask(gulp, "babel", "Babel", __dirname, version); diff --git a/packages/babel-standalone/src/gulpTasks.js b/packages/babel-standalone/src/gulpTasks.js index 7718809987..bb11f65529 100644 --- a/packages/babel-standalone/src/gulpTasks.js +++ b/packages/babel-standalone/src/gulpTasks.js @@ -4,8 +4,14 @@ * 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. + * + * The tasks in this file are designed to be reusable, so that they can be used + * to make standalone builds of other Babel plugins/presets (such as babel-minify) */ +// Must run first +require("./overrideModuleResolution")(); + const pump = require("pump"); const rename = require("gulp-rename"); const RootMostResolvePlugin = require("webpack-dependency-suite").RootMostResolvePlugin; @@ -100,24 +106,27 @@ function webpackBuild(filename, libraryName, version) { });*/ } -function registerGulpTasks(gulp) { - gulp.task("build-babel-standalone", ["build"], cb => { +function registerBabelStandaloneTask(gulp, name, exportName, path, version) { + gulp.task("build-" + name + "-standalone", cb => { pump( [ - gulp.src(__dirname + "/index.js"), + gulp.src(path + "/src/index.js"), webpackBuild( - "babel.js", - "Babel", - require(__dirname + "/../package.json").version + name + ".js", + exportName, + version ), - gulp.dest(__dirname + "/../"), + gulp.dest(path), uglify(), rename({ extname: ".min.js" }), - gulp.dest(__dirname + "/../"), + gulp.dest(path), ], cb ); }); } -module.exports = registerGulpTasks; +module.exports = { + webpackBuild: webpackBuild, + registerBabelStandaloneTask: registerBabelStandaloneTask, +} diff --git a/packages/babel-standalone/src/overrideModuleResolution.js b/packages/babel-standalone/src/overrideModuleResolution.js new file mode 100644 index 0000000000..ce32d6d1c0 --- /dev/null +++ b/packages/babel-standalone/src/overrideModuleResolution.js @@ -0,0 +1,24 @@ +/** + * babel-loader causes problems as it's not part of the monorepo. It pulls in + * an older version of babel-core (the version referenced by the root + * package.json), rather than the version of babel-core that's in the repo. The + * only way to solve this without moving babel-loader into the monorepo is to + * override Node's module resolution algorithm to specify a custom resolver for + * babel-core to *force* it to use our version. + * + * Here be dragons. + */ + +const Module = require("module"); + +module.exports = function overrideModuleResolution() { + const originalLoader = Module._load.bind(Module); + + Module._load = function babelStandaloneLoader(request, parent, isMain) { + // Redirect babel-core to version in the repo. + if (request === "babel-core") { + request = __dirname + "/../../babel-core"; + } + return originalLoader(request, parent, isMain); + }; +}; diff --git a/yarn.lock b/yarn.lock index 5f500cbce7..569ecccd1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -563,7 +563,7 @@ babel-helpers@7.0.0-alpha.18: dependencies: babel-template "7.0.0-alpha.18" -babel-loader@7.1.1, babel-loader@^7.1.1: +babel-loader@7.1.1: version "7.1.1" resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-7.1.1.tgz#b87134c8b12e3e4c2a94e0546085bc680a2b8488" dependencies: