Skip to content

Commit a93def0

Browse files
authored
Use native ESM for dev scripts (#12636)
1 parent c6902a0 commit a93def0

File tree

17 files changed

+187
-159
lines changed

17 files changed

+187
-159
lines changed

.eslintrc.js renamed to .eslintrc.cjs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
const {getPackages} = require('./scripts/buildUtils');
8+
const fs = require('fs');
9+
const path = require('path');
10+
const {sync: readPkg} = require('read-pkg');
911

10-
const internalPackages = getPackages()
11-
.map(({pkg}) => pkg.name)
12-
.sort();
12+
function getPackages() {
13+
const PACKAGES_DIR = path.resolve(__dirname, 'packages');
14+
const packages = fs
15+
.readdirSync(PACKAGES_DIR)
16+
.map(file => path.resolve(PACKAGES_DIR, file))
17+
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
18+
return packages.map(packageDir => {
19+
const pkg = readPkg({cwd: packageDir});
20+
return pkg.name;
21+
});
22+
}
1323

1424
module.exports = {
1525
env: {
@@ -254,7 +264,7 @@ module.exports = {
254264
},
255265
{
256266
env: {node: true},
257-
files: ['*.js', '*.jsx'],
267+
files: ['*.js', '*.jsx', '*.mjs', '*.cjs'],
258268
},
259269
{
260270
files: [
@@ -325,7 +335,7 @@ module.exports = {
325335
'scripts/**',
326336
'babel.config.js',
327337
'testSetupFile.js',
328-
'.eslintrc.js',
338+
'.eslintrc.cjs',
329339
],
330340
},
331341
],
@@ -507,7 +517,9 @@ module.exports = {
507517
'import/ignore': ['react-native'],
508518
// using `new RegExp` makes sure to escape `/`
509519
'import/internal-regex': new RegExp(
510-
internalPackages.map(pkg => `^${pkg}$`).join('|'),
520+
getPackages()
521+
.map(pkg => `^${pkg}$`)
522+
.join('|'),
511523
).source,
512524
'import/resolver': {
513525
typescript: {},

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ jobs:
177177
yarn jest-coverage --color --config jest.config.ci.js --max-workers ${{ steps.cpu-cores.outputs.count }} --shard=${{ matrix.shard }}
178178
yarn test-leak
179179
- name: map coverage
180-
run: node ./scripts/mapCoverage.js
180+
run: node ./scripts/mapCoverage.mjs
181181
if: always()
182182
- uses: codecov/codecov-action@v3
183183
if: always()

e2e/watch-plugins/cjs/my-watch-plugin.cjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
*/
77
class MyWatchPlugin {
88
// Add hooks to Jest lifecycle events
9-
apply(jestHooks) {
10-
}
9+
apply(jestHooks) {}
1110

1211
// Get the prompt information for interactive plugins
1312
getUsageInfo(globalConfig) {
1413
console.log('getUsageInfo');
1514
}
1615

1716
// Executed when the key from `getUsageInfo` is input
18-
run(globalConfig, updateConfigAndRun) {
19-
}
17+
run(globalConfig, updateConfigAndRun) {}
2018
}
2119

2220
module.exports = MyWatchPlugin;

e2e/watch-plugins/mjs/my-watch-plugin.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
*/
77
class MyWatchPlugin {
88
// Add hooks to Jest lifecycle events
9-
apply(jestHooks) {
10-
}
9+
apply(jestHooks) {}
1110

1211
// Get the prompt information for interactive plugins
1312
getUsageInfo(globalConfig) {
1413
console.log('getUsageInfo');
1514
}
1615

1716
// Executed when the key from `getUsageInfo` is input
18-
run(globalConfig, updateConfigAndRun) {
19-
}
17+
run(globalConfig, updateConfigAndRun) {}
2018
}
2119

2220
export default MyWatchPlugin;

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,31 @@
8787
"scripts": {
8888
"build-clean": "rimraf './packages/*/build' './packages/*/dist' './packages/*/tsconfig.tsbuildinfo' './packages/*/api-extractor.json' './api-extractor.json'",
8989
"build": "yarn build:js && yarn build:ts && yarn bundle:ts",
90-
"build:js": "node ./scripts/build.js",
91-
"build:ts": "node ./scripts/buildTs.js",
92-
"bundle:ts": "node ./scripts/bundleTs.js",
93-
"check-copyright-headers": "node ./scripts/checkCopyrightHeaders.js",
90+
"build:js": "node ./scripts/build.mjs",
91+
"build:ts": "node ./scripts/buildTs.mjs",
92+
"bundle:ts": "node ./scripts/bundleTs.mjs",
93+
"check-copyright-headers": "node ./scripts/checkCopyrightHeaders.mjs",
9494
"clean-all": "yarn clean-e2e && yarn build-clean && rimraf './packages/*/node_modules' && rimraf './node_modules'",
95-
"clean-e2e": "node ./scripts/cleanE2e.js",
95+
"clean-e2e": "node ./scripts/cleanE2e.mjs",
9696
"crowdin:upload": "echo 'Uploading sources to Crowdin' && crowdin upload sources --config ./crowdin.yaml",
9797
"crowdin:download": "echo 'Downloading translations from Crowdin' && crowdin download --config ./crowdin.yaml",
9898
"jest": "node ./packages/jest-cli/bin/jest.js",
9999
"jest-jasmine": "JEST_JASMINE=1 yarn jest",
100100
"jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.js",
101101
"jest-coverage": "yarn jest --coverage",
102-
"lint": "eslint . --cache --ext js,jsx,ts,tsx,md",
102+
"lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md",
103103
"lint:prettier": "prettier '**/*.{json,md,yml,yaml}' 'website/**/*.{css,js}' --write",
104104
"lint:prettier:ci": "prettier '**/*.{json,md,yml,yaml}' 'website/**/*.{css,js}' --check",
105-
"remove-examples": "node ./scripts/remove-examples.js",
105+
"remove-examples": "node ./scripts/remove-examples.mjs",
106106
"test-types": "yarn jest --config jest.config.tsd.js",
107107
"test-ci-partial": "yarn test-ci-partial:parallel -i",
108108
"test-ci-partial:parallel": "yarn jest --color --config jest.config.ci.js",
109109
"test-pretty-format-perf": "node packages/pretty-format/perf/test.js",
110110
"test-leak": "yarn jest -i --detectLeaks --color jest-mock jest-diff jest-repl pretty-format",
111111
"test": "yarn lint && yarn jest",
112-
"verify-old-ts": "node ./scripts/verifyOldTs.js",
113-
"verify-pnp": "node ./scripts/verifyPnP.js",
114-
"watch": "yarn build:js && node ./scripts/watch.js",
112+
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
113+
"verify-pnp": "node ./scripts/verifyPnP.mjs",
114+
"watch": "yarn build:js && node ./scripts/watch.mjs",
115115
"watch:ts": "yarn build:ts --watch"
116116
},
117117
"workspaces": [

scripts/build.js renamed to scripts/build.mjs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,39 @@
1212
* Non-js files not matching IGNORE_PATTERN will be copied without transpiling.
1313
*
1414
* Example:
15-
* node ./scripts/build.js
16-
* node ./scripts/build.js /users/123/jest/packages/jest-111/src/111.js
17-
*
18-
* NOTE: this script is node@6 compatible
15+
* node ./scripts/build.mjs
16+
* node ./scripts/build.mjs /users/123/jest/packages/jest-111/src/111.js
1917
*/
2018

21-
'use strict';
22-
23-
const assert = require('assert');
24-
const fs = require('fs');
25-
const path = require('path');
26-
const babel = require('@babel/core');
27-
const chalk = require('chalk');
28-
const glob = require('glob');
29-
const micromatch = require('micromatch');
30-
const prettier = require('prettier');
31-
const transformOptions = require('../babel.config.js');
32-
const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils');
19+
import assert from 'assert';
20+
import path from 'path';
21+
import {fileURLToPath} from 'url';
22+
import babel from '@babel/core';
23+
import chalk from 'chalk';
24+
import glob from 'glob';
25+
import fs from 'graceful-fs';
26+
import micromatch from 'micromatch';
27+
import prettier from 'prettier';
28+
import transformOptions from '../babel.config.js';
29+
import {
30+
OK,
31+
PACKAGES_DIR,
32+
adjustToTerminalWidth,
33+
getPackages,
34+
} from './buildUtils.mjs';
3335

3436
const SRC_DIR = 'src';
3537
const BUILD_DIR = 'build';
3638
const JS_FILES_PATTERN = '**/*.js';
3739
const TS_FILES_PATTERN = '**/*.ts';
3840
const IGNORE_PATTERN = '**/__{tests,mocks}__/**';
39-
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
4041

4142
const INLINE_REQUIRE_EXCLUDE_LIST =
4243
/packages\/expect|(jest-(circus|diff|get-type|jasmine2|matcher-utils|message-util|regex-util|snapshot))|pretty-format\//;
4344

44-
const prettierConfig = prettier.resolveConfig.sync(__filename);
45+
const prettierConfig = prettier.resolveConfig.sync(
46+
fileURLToPath(import.meta.url),
47+
);
4548
prettierConfig.trailingComma = 'none';
4649
prettierConfig.parser = 'babel';
4750

@@ -110,7 +113,10 @@ function buildFile(file, silent) {
110113
// The excluded modules are injected into the user's sandbox
111114
// We need to guard some globals there.
112115
options.plugins.push(
113-
require.resolve('./babel-plugin-jest-native-globals'),
116+
path.resolve(
117+
path.dirname(fileURLToPath(import.meta.url)),
118+
'babel-plugin-jest-native-globals.js',
119+
),
114120
);
115121
} else {
116122
options.plugins = options.plugins.map(plugin => {

scripts/buildTs.js renamed to scripts/buildTs.mjs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
10-
const assert = require('assert');
11-
const fs = require('fs');
12-
const os = require('os');
13-
const path = require('path');
14-
const chalk = require('chalk');
15-
const execa = require('execa');
16-
const globby = require('globby');
17-
const stripJsonComments = require('strip-json-comments');
18-
const throat = require('throat');
19-
const {getPackages} = require('./buildUtils');
8+
import assert from 'assert';
9+
import os from 'os';
10+
import path from 'path';
11+
import chalk from 'chalk';
12+
import execa from 'execa';
13+
import globby from 'globby';
14+
import fs from 'graceful-fs';
15+
import stripJsonComments from 'strip-json-comments';
16+
import throat from 'throat';
17+
import {getPackages} from './buildUtils.mjs';
2018

2119
(async () => {
2220
const packages = getPackages();

scripts/buildUtils.js renamed to scripts/buildUtils.mjs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
10-
const assert = require('assert');
11-
const fs = require('fs');
12-
const path = require('path');
13-
const chalk = require('chalk');
14-
const {sync: readPkg} = require('read-pkg');
15-
const stringLength = require('string-length');
16-
const rootPackage = require('../package.json');
17-
18-
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
19-
20-
const OK = chalk.reset.inverse.bold.green(' DONE ');
8+
import assert from 'assert';
9+
import {createRequire} from 'module';
10+
import path from 'path';
11+
import {fileURLToPath} from 'url';
12+
import chalk from 'chalk';
13+
import fs from 'graceful-fs';
14+
import {sync as readPkg} from 'read-pkg';
15+
import stringLength from 'string-length';
16+
17+
export const PACKAGES_DIR = path.resolve(
18+
path.dirname(fileURLToPath(import.meta.url)),
19+
'../packages',
20+
);
21+
22+
export const OK = chalk.reset.inverse.bold.green(' DONE ');
2123

2224
// Get absolute paths of all directories under packages/*
23-
module.exports.getPackages = function getPackages() {
25+
export function getPackages() {
2426
const packages = fs
2527
.readdirSync(PACKAGES_DIR)
2628
.map(file => path.resolve(PACKAGES_DIR, file))
2729
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
30+
const require = createRequire(import.meta.url);
31+
const rootPackage = require('../package.json');
2832

2933
const nodeEngineRequirement = rootPackage.engines.node;
3034

@@ -104,9 +108,9 @@ module.exports.getPackages = function getPackages() {
104108

105109
return {packageDir, pkg};
106110
});
107-
};
111+
}
108112

109-
module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
113+
export function adjustToTerminalWidth(str) {
110114
const columns = process.stdout.columns || 80;
111115
const WIDTH = columns - stringLength(OK) + 1;
112116
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
@@ -115,7 +119,4 @@ module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
115119
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
116120
}
117121
return strs.slice(0, -1).concat(lastString).join('\n');
118-
};
119-
120-
module.exports.OK = OK;
121-
module.exports.PACKAGES_DIR = PACKAGES_DIR;
122+
}

scripts/bundleTs.js renamed to scripts/bundleTs.mjs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
10-
const fs = require('fs');
11-
const path = require('path');
12-
const {
8+
import {createRequire} from 'module';
9+
import path from 'path';
10+
import {fileURLToPath} from 'url';
11+
import {
1312
CompilerState,
1413
Extractor,
1514
ExtractorConfig,
16-
} = require('@microsoft/api-extractor');
17-
const chalk = require('chalk');
18-
const {sync: pkgDir} = require('pkg-dir');
19-
const prettier = require('prettier');
20-
const rimraf = require('rimraf');
21-
const {getPackages} = require('./buildUtils');
15+
} from '@microsoft/api-extractor';
16+
import chalk from 'chalk';
17+
import fs from 'graceful-fs';
18+
import {sync as pkgDir} from 'pkg-dir';
19+
import prettier from 'prettier';
20+
import rimraf from 'rimraf';
21+
import {getPackages} from './buildUtils.mjs';
2222

2323
const prettierConfig = prettier.resolveConfig.sync(
24-
__filename.replace(/\.js$/, '.d.ts'),
24+
fileURLToPath(import.meta.url).replace(/\.js$/, '.d.ts'),
2525
);
2626

27+
const require = createRequire(import.meta.url);
2728
const typescriptCompilerFolder = pkgDir(require.resolve('typescript'));
2829

2930
const copyrightSnippet = `
@@ -104,7 +105,10 @@ const copyrightSnippet = `
104105
};
105106

106107
await fs.promises.writeFile(
107-
path.resolve(__dirname, '../api-extractor.json'),
108+
path.resolve(
109+
path.dirname(fileURLToPath(import.meta.url)),
110+
'../api-extractor.json',
111+
),
108112
JSON.stringify(sharedExtractorConfig, null, 2),
109113
);
110114

scripts/checkCopyrightHeaders.js renamed to scripts/checkCopyrightHeaders.mjs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
10-
const {execSync} = require('child_process');
11-
const fs = require('fs');
12-
const {isBinaryFileSync} = require('isbinaryfile');
8+
import {execSync} from 'child_process';
9+
import fs from 'graceful-fs';
10+
import {isBinaryFileSync} from 'isbinaryfile';
1311

1412
const getFileContents = path => fs.readFileSync(path, {encoding: 'utf-8'});
1513
const isDirectory = path => fs.lstatSync(path).isDirectory();
@@ -149,7 +147,7 @@ function check() {
149147
150148
${invalidFiles.join('\n ')}
151149
152-
Please include the header or exclude the files in \`scripts/checkCopyrightHeaders.js\``);
150+
Please include the header or exclude the files in \`scripts/checkCopyrightHeaders.mjs\``);
153151
process.exit(1);
154152
}
155153
}

0 commit comments

Comments
 (0)