From e9dbdeca96bd7fa673f9c61c2599cd14fee61c16 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Fri, 4 May 2018 19:22:39 -0700 Subject: [PATCH] Target latest Node for backend applications (#213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Consequently, Babel won’t transform classes to their roughly equivalent ES5 counterparts, etc. Test Plan: Create `src/classy.js` with `class X {}; console.log(X);`. Then, add a build target for `classy: resolveApp("src/classy.js"),` in `paths.js`. Use `yarn backend` and inspect the contents of `bin/classy.js`; in particular, look at the definition of `X` (whatever the argument to `console.log` is). Before this commit, the result will be a big complicated mess. After this commit, it will be `class X {}`. Note also that `yarn travis --full` passes, indicating that the two manual tests, which call out to the utilities in `bin/`, still work. wchargin-branch: target-node --- config/babel.js | 46 ++++++++++++++++++++++++++++------------------ package.json | 1 + scripts/backend.js | 1 + 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/config/babel.js b/config/babel.js index 72685c1..f6ba6e1 100644 --- a/config/babel.js +++ b/config/babel.js @@ -49,6 +49,7 @@ const plugins = [ // https://github.com/facebookincubator/create-react-app/issues/720 // It’s also nice that we can enforce `NODE_ENV` being specified. var env = process.env.BABEL_ENV || process.env.NODE_ENV; +var backend = !!process.env.SOURCECRED_BACKEND; if (env !== "development" && env !== "test" && env !== "production") { throw new Error( "Using `babel-preset-react-app` requires that you specify `NODE_ENV` or " + @@ -101,13 +102,14 @@ if (env === "test") { [ require.resolve("babel-preset-env"), { - targets: { - // React parses on ie 9, so we should too - ie: 9, - // We currently minify with uglify - // Remove after https://github.com/mishoo/UglifyJS2/issues/448 - uglify: true, - }, + targets: backend + ? { + node: "current", + } + : { + ie: 9, + uglify: true, + }, // Disable polyfill transforms useBuiltIns: false, // Do not transform modules to CJS @@ -117,18 +119,26 @@ if (env === "test") { // JSX, Flow require.resolve("babel-preset-react"), ], - plugins: plugins.concat([ - // function* () { yield 42; yield 43; } + plugins: plugins.concat( + backend + ? [ + // Must come before `babel-plugin-transform-regenerator`. + require.resolve("babel-plugin-transform-es2015-for-of"), + ] + : [], [ - require.resolve("babel-plugin-transform-regenerator"), - { - // Async functions are converted to generators by babel-preset-env - async: false, - }, - ], - // Adds syntax support for import() - require.resolve("babel-plugin-syntax-dynamic-import"), - ]), + // function* () { yield 42; yield 43; } + [ + require.resolve("babel-plugin-transform-regenerator"), + { + // Async functions are converted to generators by babel-preset-env + async: false, + }, + ], + // Adds syntax support for import() + require.resolve("babel-plugin-syntax-dynamic-import"), + ] + ), }; if (env === "production") { diff --git a/package.json b/package.json index c1b5a86..d8fccb9 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "babel-eslint": "7.2.3", "babel-jest": "20.0.3", "babel-loader": "7.1.2", + "babel-plugin-transform-es2015-for-of": "^6.23.0", "babel-preset-react-app": "^3.1.1", "babel-runtime": "6.26.0", "case-sensitive-paths-webpack-plugin": "2.1.1", diff --git a/scripts/backend.js b/scripts/backend.js index 4b96d38..f9add28 100644 --- a/scripts/backend.js +++ b/scripts/backend.js @@ -3,6 +3,7 @@ // Do this as the first thing so that any code reading it knows the right env. process.env.NODE_ENV = process.env.NODE_ENV || "development"; process.env.BABEL_ENV = process.env.NODE_ENV; +process.env.SOURCECRED_BACKEND = true; // Makes the script crash on unhandled rejections instead of silently // ignoring them. In the future, promise rejections that are not handled will