diff --git a/babel.config.js b/babel.config.js index 20a40063a2fb..82b13d097f49 100644 --- a/babel.config.js +++ b/babel.config.js @@ -33,7 +33,7 @@ module.exports = { }, ], ], - test: 'packages/jest-config/src/readConfigFileAndSetRootDir.ts', + test: 'packages/jest-config/src/importMjs.ts', }, ], plugins: [ diff --git a/packages/jest-config/src/importMjs.ts b/packages/jest-config/src/importMjs.ts new file mode 100644 index 000000000000..a2746b52ddc6 --- /dev/null +++ b/packages/jest-config/src/importMjs.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// this is in a separate file so that node 8 don't explode with a syntax error +export default (specifier: string) => import(specifier); diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index b0b2a8479ea1..96576e9c28e9 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -15,6 +15,7 @@ import { JEST_CONFIG_EXT_MJS, PACKAGE_JSON, } from './constants'; +import importMjs from './importMjs'; // Read the configuration and set its `rootDir` // 1. If it's a `package.json` file, we look into its "jest" property @@ -28,7 +29,7 @@ export default async function readConfigFileAndSetRootDir( if (isMjs) { try { - const importedConfig = await import(configPath); + const importedConfig = await importMjs(configPath); if (!importedConfig.default) { throw new Error( diff --git a/scripts/build.js b/scripts/build.js index 2930ee8804e2..06481ec36e33 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -149,7 +149,14 @@ function buildFile(file, silent) { Array.isArray(plugin) && plugin[0] === '@babel/plugin-transform-modules-commonjs' ) { - return [plugin[0], Object.assign({}, plugin[1], {lazy: true})]; + return [ + plugin[0], + Object.assign({}, plugin[1], { + lazy: string => + // we want to lazyload all non-local modules plus `importMjs` - the latter to avoid syntax errors + !string.startsWith('./') || string === './importMjs', + }), + ]; } return plugin;