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 dev scripts #12636

Merged
merged 20 commits into from
Apr 7, 2022
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module.exports = {
},
{
env: {node: true},
files: ['*.js', '*.jsx'],
files: ['*.js', '*.jsx', 'scripts/*'],
},
{
files: [
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
"scripts": {
"build-clean": "rimraf './packages/*/build' './packages/*/dist' './packages/*/tsconfig.tsbuildinfo' './packages/*/api-extractor.json' './api-extractor.json'",
"build": "yarn build:js && yarn build:ts && yarn bundle:ts",
"build:js": "node ./scripts/build.js",
"build:ts": "node ./scripts/buildTs.js",
"bundle:ts": "node ./scripts/bundleTs.js",
"build:js": "node ./scripts/build.mjs",
"build:ts": "node ./scripts/buildTs.mjs",
"bundle:ts": "node ./scripts/bundleTs.mjs",
"check-copyright-headers": "node ./scripts/checkCopyrightHeaders.js",
"clean-all": "yarn clean-e2e && yarn build-clean && rimraf './packages/*/node_modules' && rimraf './node_modules'",
"clean-e2e": "node ./scripts/cleanE2e.js",
Expand Down
42 changes: 25 additions & 17 deletions scripts/build.js → scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,41 @@
* Non-js files not matching IGNORE_PATTERN will be copied without transpiling.
*
* Example:
* node ./scripts/build.js
* node ./scripts/build.js /users/123/jest/packages/jest-111/src/111.js
*
* NOTE: this script is node@6 compatible
* node ./scripts/build.mjs
* node ./scripts/build.mjs /users/123/jest/packages/jest-111/src/111.js
*/

'use strict';

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');
const chalk = require('chalk');
const glob = require('glob');
const micromatch = require('micromatch');
const prettier = require('prettier');
const transformOptions = require('../babel.config.js');
const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils');
import assert from 'assert';
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import babel from '@babel/core';
import chalk from 'chalk';
import glob from 'glob';
import micromatch from 'micromatch';
import prettier from 'prettier';
import transformOptions from '../babel.config.js';
import {
OK,
PACKAGES_DIR,
adjustToTerminalWidth,
getPackages,
} from './buildUtils.mjs';

const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const JS_FILES_PATTERN = '**/*.js';
const TS_FILES_PATTERN = '**/*.ts';
const IGNORE_PATTERN = '**/__{tests,mocks}__/**';
const PACKAGES_DIR = path.resolve(__dirname, '../packages');

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

const prettierConfig = prettier.resolveConfig.sync(__filename);
const prettierConfig = prettier.resolveConfig.sync(
fileURLToPath(import.meta.url),
);
prettierConfig.trailingComma = 'none';
prettierConfig.parser = 'babel';

Expand Down Expand Up @@ -110,7 +115,10 @@ function buildFile(file, silent) {
// The excluded modules are injected into the user's sandbox
// We need to guard some globals there.
options.plugins.push(
require.resolve('./babel-plugin-jest-native-globals'),
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'babel-plugin-jest-native-globals.js',
),
);
} else {
options.plugins = options.plugins.map(plugin => {
Expand Down
28 changes: 14 additions & 14 deletions scripts/buildTs.js → scripts/buildTs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

'use strict';
F3n67u marked this conversation as resolved.
Show resolved Hide resolved

const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const execa = require('execa');
const globby = require('globby');
const stripJsonComments = require('strip-json-comments');
const throat = require('throat');
const {getPackages} = require('./buildUtils');

(async () => {
F3n67u marked this conversation as resolved.
Show resolved Hide resolved
import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import chalk from 'chalk';
import execa from 'execa';
import globby from 'globby';
import stripJsonComments from 'strip-json-comments';
import throat from 'throat';
import {getPackages} from './buildUtils.mjs';

try {
const packages = getPackages();

const packagesWithTs = packages.filter(p =>
Expand Down Expand Up @@ -196,7 +196,7 @@ const {getPackages} = require('./buildUtils');
console.log(
chalk.inverse.green(' Successfully validated TypeScript definition files '),
);
})().catch(error => {
} catch (error) {
console.error('Got error', error.stack);
process.exitCode = 1;
});
}
39 changes: 21 additions & 18 deletions scripts/buildUtils.js → scripts/buildUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@

'use strict';

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const {sync: readPkg} = require('read-pkg');
const stringLength = require('string-length');
const rootPackage = require('../package.json');

const PACKAGES_DIR = path.resolve(__dirname, '../packages');

const OK = chalk.reset.inverse.bold.green(' DONE ');
import assert from 'assert';
import fs from 'fs';
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';
import chalk from 'chalk';
import {sync as readPkg} from 'read-pkg';
import stringLength from 'string-length';

export const PACKAGES_DIR = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../packages',
);

export const OK = chalk.reset.inverse.bold.green(' DONE ');

// Get absolute paths of all directories under packages/*
module.exports.getPackages = function getPackages() {
export function getPackages() {
const packages = fs
.readdirSync(PACKAGES_DIR)
.map(file => path.resolve(PACKAGES_DIR, file))
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
const require = createRequire(import.meta.url);
const rootPackage = require('../package.json');

const nodeEngineRequirement = rootPackage.engines.node;

Expand Down Expand Up @@ -104,9 +110,9 @@ module.exports.getPackages = function getPackages() {

return {packageDir, pkg};
});
};
}

module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
export function adjustToTerminalWidth(str) {
const columns = process.stdout.columns || 80;
const WIDTH = columns - stringLength(OK) + 1;
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
Expand All @@ -115,7 +121,4 @@ module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
}
return strs.slice(0, -1).concat(lastString).join('\n');
};

module.exports.OK = OK;
module.exports.PACKAGES_DIR = PACKAGES_DIR;
}
34 changes: 20 additions & 14 deletions scripts/bundleTs.js → scripts/bundleTs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@

'use strict';

const fs = require('fs');
const path = require('path');
const {
import fs from 'fs';
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';
import {
CompilerState,
Extractor,
ExtractorConfig,
} = require('@microsoft/api-extractor');
const chalk = require('chalk');
const {sync: pkgDir} = require('pkg-dir');
const prettier = require('prettier');
const rimraf = require('rimraf');
const {getPackages} = require('./buildUtils');
} from '@microsoft/api-extractor';
import chalk from 'chalk';
import {sync as pkgDir} from 'pkg-dir';
import prettier from 'prettier';
import rimraf from 'rimraf';
import {getPackages} from './buildUtils.mjs';

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

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

const copyrightSnippet = `
Expand All @@ -35,7 +38,7 @@ const copyrightSnippet = `
*/
`.trim();

(async () => {
try {
const packages = getPackages();

const packagesWithTs = packages.filter(p =>
Expand Down Expand Up @@ -104,7 +107,10 @@ const copyrightSnippet = `
};

await fs.promises.writeFile(
path.resolve(__dirname, '../api-extractor.json'),
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../api-extractor.json',
),
JSON.stringify(sharedExtractorConfig, null, 2),
);

Expand Down Expand Up @@ -195,7 +201,7 @@ const copyrightSnippet = `
console.log(
chalk.inverse.green(' Successfully extracted TypeScript definition files '),
);
})().catch(error => {
} catch (error) {
console.error('Got error', error.stack);
process.exitCode = 1;
});
}