Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native esm for babel config and babel custom plugin #12684

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Expand Up @@ -324,7 +324,7 @@ module.exports = {
'**/__typetests__/**',
'**/?(*.)(spec|test).js?(x)',
'scripts/**',
'babel.config.js',
'babel.config.mjs',
'testSetupFile.js',
'.eslintrc.cjs',
],
Expand Down
16 changes: 11 additions & 5 deletions babel.config.js → babel.config.mjs
Expand Up @@ -5,20 +5,26 @@
* LICENSE file in the root directory of this source tree.
*/

const semver = require('semver');
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';
import semver from 'semver';
const require = createRequire(import.meta.url);
const pkg = require('./package.json');

const supportedNodeVersion = semver.minVersion(pkg.engines.node).version;
const dirname = path.dirname(fileURLToPath(import.meta.url));

module.exports = {
export default {
babelrcRoots: ['examples/*'],
// we don't wanna run the transforms in this file over react native
exclude: /react-native/,
overrides: [
{
plugins: [
require.resolve(
'./scripts/babel-plugin-jest-replace-ts-require-assignment.js',
path.resolve(
dirname,
'scripts/babel-plugin-jest-replace-ts-require-assignment.mjs',
),
],
presets: [
Expand All @@ -37,7 +43,7 @@ module.exports = {
],
plugins: [
['@babel/plugin-transform-modules-commonjs', {allowTopLevelThis: true}],
require.resolve('./scripts/babel-plugin-jest-require-outside-vm'),
path.resolve(dirname, 'scripts/babel-plugin-jest-require-outside-vm.mjs'),
],
presets: [
[
Expand Down
4 changes: 4 additions & 0 deletions examples/mongodb/jest.config.js
Expand Up @@ -11,5 +11,9 @@ module.exports = {
// The test environment that will be used for testing
testEnvironment: './mongo-environment.js',

transform: {
'\\.js$': [require.resolve('babel-jest'), {cwd: __dirname}],
},

transformIgnorePatterns: ['/node_modules/', '/packages/'],
};
10 changes: 10 additions & 0 deletions examples/typescript/jest.config.js
@@ -0,0 +1,10 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/configuration

module.exports = {
testEnvironment: 'jsdom',

transform: {
'\\.js$': [require.resolve('babel-jest'), {cwd: __dirname}],
},
};
3 changes: 0 additions & 3 deletions examples/typescript/package.json
Expand Up @@ -18,8 +18,5 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
2 changes: 1 addition & 1 deletion jest.config.mjs
Expand Up @@ -70,7 +70,7 @@ export default {
],
testTimeout: 70000,
transform: {
'\\.[jt]sx?$': require.resolve('babel-jest'),
'\\.[jt]sx?$': '<rootDir>/transformer.mjs',
},
watchPathIgnorePatterns: ['coverage'],
watchPlugins: [
Expand Down
Expand Up @@ -5,12 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// This plugin exists to make sure that we use a `Promise` that has not been messed with by user code.
// Might consider extending this to other globals as well in the future

module.exports = ({template}) => {
export default ({template}) => {
const promiseDeclaration = template(`
var Promise = globalThis[Symbol.for('jest-native-promise')] || globalThis.Promise;
`);
Expand Down
Expand Up @@ -5,11 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// Replace `import thing = require('thing')` with `const thing = require('thing')` which allows us to keep CJS semantics

module.exports = ({template}) => {
export default ({template}) => {
const moduleExportsDeclaration = template(`
import NAME from 'IMPORT';
`);
Expand Down
Expand Up @@ -5,9 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const assert = require('assert');
import assert from 'assert';

/*
Replace
Expand All @@ -23,7 +21,7 @@ require(require.resolve('package', {

const REQUIRE_OUTSIDE_FUNCTION_NAME = 'requireOutside';

module.exports = ({template, types: t}) => {
export default ({template, types: t}) => {
const replacement = template(`
require(require.resolve(IMPORT_PATH, {
[(globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol).for('jest-resolve-outside-vm-option')]: true,
Expand Down
40 changes: 25 additions & 15 deletions scripts/build.mjs
Expand Up @@ -25,7 +25,7 @@ import glob from 'glob';
import fs from 'graceful-fs';
import micromatch from 'micromatch';
import prettier from 'prettier';
import transformOptions from '../babel.config.js';
import transformOptions from '../babel.config.mjs';
import {
OK,
PACKAGES_DIR,
Expand Down Expand Up @@ -60,14 +60,16 @@ function getBuildPath(file, buildFolder) {
return path.resolve(pkgBuildPath, relativeToSrcPath).replace(/\.ts$/, '.js');
}

function buildNodePackage({packageDir, pkg}) {
async function buildNodePackage({packageDir, pkg}) {
const srcDir = path.resolve(packageDir, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});

process.stdout.write(adjustToTerminalWidth(`${pkg.name}\n`));

files.forEach(file => buildFile(file, true));
for (const file of files) {
await buildFile(file, true);
}

assert.ok(
fs.existsSync(path.resolve(packageDir, pkg.main)),
Expand All @@ -77,7 +79,7 @@ function buildNodePackage({packageDir, pkg}) {
process.stdout.write(`${OK}\n`);
}

function buildFile(file, silent) {
async function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);

if (micromatch.isMatch(file, IGNORE_PATTERN)) {
Expand Down Expand Up @@ -115,7 +117,7 @@ function buildFile(file, silent) {
options.plugins.push(
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'babel-plugin-jest-native-globals.js',
'babel-plugin-jest-native-globals.mjs',
),
);
} else {
Expand All @@ -131,7 +133,7 @@ function buildFile(file, silent) {
});
}

const transformed = babel.transformFileSync(file, options).code;
const {code: transformed} = await babel.transformFileAsync(file, options);
const prettyCode = prettier.format(transformed, prettierConfig);

fs.writeFileSync(destPath, prettyCode);
Expand All @@ -148,12 +150,20 @@ function buildFile(file, silent) {
}
}

const files = process.argv.slice(2);

if (files.length) {
files.forEach(file => buildFile(file));
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Building packages \n'));
packages.forEach(buildNodePackage);
}
(async () => {
const files = process.argv.slice(2);
if (files.length) {
for (const file of files) {
await buildFile(file, true);
}
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Building packages \n'));
for (const pkg of packages) {
await buildNodePackage(pkg);
}
}
})().catch(error => {
console.error('Got error', error.stack);
process.exitCode = 1;
});
17 changes: 17 additions & 0 deletions transformer.mjs
@@ -0,0 +1,17 @@
/**
* 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.
*/
import {fileURLToPath} from 'url';
import {createTransformer} from 'babel-jest';

export default {
...createTransformer({
root: fileURLToPath(import.meta.url),
}),
// remove the synchronous functions
getCacheKey: undefined,
process: undefined,
};