From cb33014ffa5d33df4e6620fb58b5261fbfdfd610 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Aug 2017 13:35:12 +0200 Subject: [PATCH 1/4] Use babel transform to inline all requires --- .babelrc | 15 ++++++++++++++- package.json | 2 +- packages/jest-cli/src/cli/get_jest.js | 3 +-- packages/jest-runtime/src/index.js | 4 ++-- yarn.lock | 15 ++++++++------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/.babelrc b/.babelrc index d3202c6557ea..946713b35274 100644 --- a/.babelrc +++ b/.babelrc @@ -6,7 +6,20 @@ "transform-es2015-parameters", "transform-async-to-generator", "transform-strict-mode", - ["transform-es2015-modules-commonjs", {"allowTopLevelThis": true}] + ["transform-inline-imports-commonjs", {"allowTopLevelThis": true, "excludeModules": [ + "jest-circus", + "jest-diff", + "jest-get-type", + "jest-jasmine2", + "jest-matcher-utils", + "jest-matchers", + "jest-message-util", + "jest-regex-util", + "jest-snapshot", + "jest-util", + "jest-validate", + "pretty-format" + ]}] ], "retainLines": true } diff --git a/package.json b/package.json index ce8ee45bc659..fae51ff438ca 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "babel-plugin-syntax-trailing-function-commas": "^6.13.0", "babel-plugin-transform-async-to-generator": "^6.16.0", "babel-plugin-transform-es2015-destructuring": "^6.23.0", - "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", "babel-plugin-transform-es2015-parameters": "^6.23.0", "babel-plugin-transform-flow-strip-types": "^6.18.0", + "babel-plugin-transform-inline-imports-commonjs": "^1.2.0", "babel-plugin-transform-runtime": "^6.23.0", "babel-plugin-transform-strict-mode": "^6.24.1", "babel-preset-env": "^1.4.0", diff --git a/packages/jest-cli/src/cli/get_jest.js b/packages/jest-cli/src/cli/get_jest.js index 830ed090a7da..9f47395d59fe 100644 --- a/packages/jest-cli/src/cli/get_jest.js +++ b/packages/jest-cli/src/cli/get_jest.js @@ -13,6 +13,7 @@ import type {Path} from 'types/Config'; import path from 'path'; import chalk from 'chalk'; import fs from 'graceful-fs'; +import jest from '../jest'; function getJest(packageRoot: Path) { const packageJSONPath = path.join(packageRoot, 'package.json'); @@ -21,8 +22,6 @@ function getJest(packageRoot: Path) { /* $FlowFixMe */ return require(binPath); } else { - // TODO: Because of a dependency cycle, this can only be inlined once the babel plugin for inlining is merged - const jest = require('../jest'); // Check if Jest is specified in `package.json` but not installed. if (fs.existsSync(packageJSONPath)) { /* $FlowFixMe */ diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index d3ef2d661155..51b54ece9f4d 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -26,6 +26,7 @@ import fs from 'graceful-fs'; import stripBOM from 'strip-bom'; import ScriptTransformer from './script_transformer'; import shouldInstrument from './should_instrument'; +import cli from './cli'; import cliArgs from './cli/args'; type Module = {| @@ -262,8 +263,7 @@ class Runtime { } static runCLI(args?: Argv, info?: Array) { - // TODO: If this is not inline, the repl test fails - return require('./cli').run(args, info); + return cli.run(args, info); } static getCLIOptions() { diff --git a/yarn.lock b/yarn.lock index e55e9088654a..6c1233ba66b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -759,6 +759,13 @@ babel-plugin-transform-flow-strip-types@^6.18.0, babel-plugin-transform-flow-str babel-plugin-syntax-flow "^6.18.0" babel-runtime "^6.22.0" +babel-plugin-transform-inline-imports-commonjs@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-inline-imports-commonjs/-/babel-plugin-transform-inline-imports-commonjs-1.2.0.tgz#20c7d192bafc54c8727386e3387d8ee4ef19e6a5" + dependencies: + babel-plugin-transform-strict-mode "^6.8.0" + builtin-modules "^1.1.1" + babel-plugin-transform-object-assign@^6.5.0: version "6.22.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-assign/-/babel-plugin-transform-object-assign-6.22.0.tgz#f99d2f66f1a0b0d498e346c5359684740caa20ba" @@ -812,7 +819,7 @@ babel-plugin-transform-runtime@^6.23.0: dependencies: babel-runtime "^6.22.0" -babel-plugin-transform-strict-mode@^6.24.1: +babel-plugin-transform-strict-mode@^6.24.1, babel-plugin-transform-strict-mode@^6.8.0: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758" dependencies: @@ -5208,12 +5215,6 @@ resolve@^1.1.3, resolve@^1.1.4, resolve@^1.1.6, resolve@^1.1.7: dependencies: path-parse "^1.0.5" -resolve@^1.3.2: - version "1.3.3" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5" - dependencies: - path-parse "^1.0.5" - resolve@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86" From 440be57e7a359206ff4638388e26c5ff321362ee Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Aug 2017 15:02:44 +0200 Subject: [PATCH 2/4] Remove jest-util and jest-validate from blacklist --- .babelrc | 2 -- 1 file changed, 2 deletions(-) diff --git a/.babelrc b/.babelrc index 946713b35274..5ef398a3f49c 100644 --- a/.babelrc +++ b/.babelrc @@ -16,8 +16,6 @@ "jest-message-util", "jest-regex-util", "jest-snapshot", - "jest-util", - "jest-validate", "pretty-format" ]}] ], From 4124e7ea26bef24e1b7e44ff3b3e824a42c76042 Mon Sep 17 00:00:00 2001 From: cpojer Date: Thu, 24 Aug 2017 15:25:55 +0100 Subject: [PATCH 3/4] Do not apply the inline-require transform to blacklisted modules. --- .babelrc | 14 +-------- package.json | 4 +-- scripts/build.js | 22 ++++++++++++-- yarn.lock | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 18 deletions(-) diff --git a/.babelrc b/.babelrc index 5ef398a3f49c..ae49879f0aab 100644 --- a/.babelrc +++ b/.babelrc @@ -5,19 +5,7 @@ "transform-es2015-destructuring", "transform-es2015-parameters", "transform-async-to-generator", - "transform-strict-mode", - ["transform-inline-imports-commonjs", {"allowTopLevelThis": true, "excludeModules": [ - "jest-circus", - "jest-diff", - "jest-get-type", - "jest-jasmine2", - "jest-matcher-utils", - "jest-matchers", - "jest-message-util", - "jest-regex-util", - "jest-snapshot", - "pretty-format" - ]}] + "transform-strict-mode" ], "retainLines": true } diff --git a/package.json b/package.json index fae51ff438ca..eb7518c712ba 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "babel-plugin-syntax-trailing-function-commas": "^6.13.0", "babel-plugin-transform-async-to-generator": "^6.16.0", "babel-plugin-transform-es2015-destructuring": "^6.23.0", + "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", "babel-plugin-transform-es2015-parameters": "^6.23.0", "babel-plugin-transform-flow-strip-types": "^6.18.0", "babel-plugin-transform-inline-imports-commonjs": "^1.2.0", @@ -139,6 +140,5 @@ "testMatch": [ "**/*.test.js" ] - }, - "dependencies": {} + } } diff --git a/scripts/build.js b/scripts/build.js index c4a40d2bf681..a5bd07b36a6d 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -41,10 +41,12 @@ const JS_FILES_PATTERN = '**/*.js'; const IGNORE_PATTERN = '**/__tests__/**'; const PACKAGES_DIR = path.resolve(__dirname, '../packages'); -const babelNodeOptions = JSON.parse( +const INLINE_REQUIRE_BLACKLIST = /packages\/(jest-(circus|diff|get-type|jasmine2|matcher-utils|matchers|message-util|regex-util|snapshot))|pretty-format\//; + +const transformOptions = JSON.parse( fs.readFileSync(path.resolve(__dirname, '..', '.babelrc'), 'utf8') ); -babelNodeOptions.babelrc = false; +transformOptions.babelrc = false; const adjustToTerminalWidth = str => { const columns = process.stdout.columns || 80; @@ -136,7 +138,21 @@ function buildFile(file, silent) { '\n' ); } else { - const transformed = babel.transformFileSync(file, babelNodeOptions).code; + const options = Object.assign({}, transformOptions); + options.plugins = options.plugins.slice(); + + if (INLINE_REQUIRE_BLACKLIST.test(file)) { + options.plugins.push('transform-es2015-modules-commonjs'); + } else { + options.plugins.push([ + 'transform-inline-imports-commonjs', + { + allowTopLevelThis: true, + }, + ]); + } + + const transformed = babel.transformFileSync(file, options).code; fs.writeFileSync(destPath, transformed); silent || process.stdout.write( diff --git a/yarn.lock b/yarn.lock index 6c1233ba66b9..a45a4817eca7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -307,6 +307,14 @@ babel-code-frame@^6.22.0: esutils "^2.0.2" js-tokens "^3.0.0" +babel-code-frame@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" + dependencies: + chalk "^1.1.3" + esutils "^2.0.2" + js-tokens "^3.0.2" + babel-core@6, babel-core@^6.23.1, babel-core@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.24.1.tgz#8c428564dce1e1f41fb337ec34f4c3b022b5ad83" @@ -669,6 +677,15 @@ babel-plugin-transform-es2015-modules-commonjs@^6.23.0, babel-plugin-transform-e babel-template "^6.24.1" babel-types "^6.24.1" +babel-plugin-transform-es2015-modules-commonjs@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.0.tgz#0d8394029b7dc6abe1a97ef181e00758dd2e5d8a" + dependencies: + babel-plugin-transform-strict-mode "^6.24.1" + babel-runtime "^6.26.0" + babel-template "^6.26.0" + babel-types "^6.26.0" + babel-plugin-transform-es2015-modules-systemjs@^6.23.0: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-systemjs/-/babel-plugin-transform-es2015-modules-systemjs-6.24.1.tgz#ff89a142b9119a906195f5f106ecf305d9407d23" @@ -930,6 +947,13 @@ babel-runtime@^6.18.0, babel-runtime@^6.22.0: core-js "^2.4.0" regenerator-runtime "^0.10.0" +babel-runtime@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" + dependencies: + core-js "^2.4.0" + regenerator-runtime "^0.11.0" + babel-template@^6.16.0, babel-template@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.24.1.tgz#04ae514f1f93b3a2537f2a0f60a5a45fb8308333" @@ -950,6 +974,16 @@ babel-template@^6.25.0: babylon "^6.17.2" lodash "^4.2.0" +babel-template@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02" + dependencies: + babel-runtime "^6.26.0" + babel-traverse "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + lodash "^4.17.4" + babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.24.1.tgz#ab36673fd356f9a0948659e7b338d5feadb31695" @@ -978,6 +1012,20 @@ babel-traverse@^6.25.0: invariant "^2.2.0" lodash "^4.2.0" +babel-traverse@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" + dependencies: + babel-code-frame "^6.26.0" + babel-messages "^6.23.0" + babel-runtime "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + debug "^2.6.8" + globals "^9.18.0" + invariant "^2.2.2" + lodash "^4.17.4" + babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.23.0, babel-types@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.24.1.tgz#a136879dc15b3606bda0d90c1fc74304c2ff0975" @@ -996,10 +1044,23 @@ babel-types@^6.25.0: lodash "^4.2.0" to-fast-properties "^1.0.1" +babel-types@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497" + dependencies: + babel-runtime "^6.26.0" + esutils "^2.0.2" + lodash "^4.17.4" + to-fast-properties "^1.0.3" + babylon@^6.11.0, babylon@^6.14.1, babylon@^6.15.0, babylon@^6.17.0, babylon@^6.17.2, babylon@^6.17.4: version "6.17.4" resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.17.4.tgz#3e8b7402b88d22c3423e137a1577883b15ff869a" +babylon@^6.18.0: + version "6.18.0" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" + backo2@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947" @@ -2935,6 +2996,10 @@ globals@^9.0.0, globals@^9.17.0: version "9.17.0" resolved "https://registry.yarnpkg.com/globals/-/globals-9.17.0.tgz#0c0ca696d9b9bb694d2e5470bd37777caad50286" +globals@^9.18.0: + version "9.18.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a" + globby@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/globby/-/globby-5.0.0.tgz#ebd84667ca0dbb330b99bcfc68eac2bc54370e0d" @@ -3619,6 +3684,10 @@ js-tokens@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7" +js-tokens@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" + js-yaml@^3.7.0, js-yaml@^3.8.4: version "3.8.4" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.4.tgz#520b4564f86573ba96662af85a8cafa7b4b5a6f6" @@ -5071,6 +5140,10 @@ regenerator-runtime@^0.10.0, regenerator-runtime@^0.10.3: version "0.10.3" resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.3.tgz#8c4367a904b51ea62a908ac310bf99ff90a82a3e" +regenerator-runtime@^0.11.0: + version "0.11.0" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz#7e54fe5b5ccd5d6624ea6255c3473be090b802e1" + regenerator-transform@0.9.11: version "0.9.11" resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.9.11.tgz#3a7d067520cb7b7176769eb5ff868691befe1283" @@ -5860,6 +5933,10 @@ to-fast-properties@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.2.tgz#f3f5c0c3ba7299a7ef99427e44633257ade43320" +to-fast-properties@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" + tough-cookie@^2.3.2, tough-cookie@~2.3.0: version "2.3.2" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a" From 0c1859666017af5935ac23bbdce99d676f3bfc03 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Aug 2017 17:15:57 +0200 Subject: [PATCH 4/4] Always have commonjs plugin, but remove for non-blacklisted deps --- .babelrc | 3 ++- scripts/build.js | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.babelrc b/.babelrc index ae49879f0aab..d3202c6557ea 100644 --- a/.babelrc +++ b/.babelrc @@ -5,7 +5,8 @@ "transform-es2015-destructuring", "transform-es2015-parameters", "transform-async-to-generator", - "transform-strict-mode" + "transform-strict-mode", + ["transform-es2015-modules-commonjs", {"allowTopLevelThis": true}] ], "retainLines": true } diff --git a/scripts/build.js b/scripts/build.js index a5bd07b36a6d..25f112dc17ef 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -141,9 +141,15 @@ function buildFile(file, silent) { const options = Object.assign({}, transformOptions); options.plugins = options.plugins.slice(); - if (INLINE_REQUIRE_BLACKLIST.test(file)) { - options.plugins.push('transform-es2015-modules-commonjs'); - } else { + if (!INLINE_REQUIRE_BLACKLIST.test(file)) { + // Remove normal plugin. + options.plugins = options.plugins.filter( + plugin => + !( + Array.isArray(plugin) && + plugin[0] === 'transform-es2015-modules-commonjs' + ) + ); options.plugins.push([ 'transform-inline-imports-commonjs', {