Skip to content

Commit

Permalink
fix support for node 8
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 19, 2020
1 parent 2e0d77e commit b218ce1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion babel.config.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
},
],
],
test: 'packages/jest-config/src/readConfigFileAndSetRootDir.ts',
test: 'packages/jest-config/src/importMjs.ts',
},
],
plugins: [
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-config/src/importMjs.ts
@@ -0,0 +1,10 @@
/**
* 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.
// Remove this file when we drop support for Node 8
export default (specifier: string) => import(specifier);
3 changes: 2 additions & 1 deletion packages/jest-config/src/readConfigFileAndSetRootDir.ts
Expand Up @@ -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
Expand All @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion scripts/build.js
Expand Up @@ -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. Set to just `true` when we drop support for node 8
!string.startsWith('./') || string === './importMjs',
}),
];
}

return plugin;
Expand Down

0 comments on commit b218ce1

Please sign in to comment.