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

[v3.0] Convert build scripts to ESM, update dependencies #4558

Merged
merged 2 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
637 changes: 274 additions & 363 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -20,7 +20,6 @@
"lint:nofix": "eslint . && prettier --check \"**/*.md\"",
"lint:markdown": "prettier --write \"**/*.md\"",
"perf": "npm run build:cjs && node --expose-gc scripts/perf.js",
"perf:debug": "node --inspect-brk scripts/perf-debug.js",
"perf:init": "node scripts/perf-init.js",
"postpublish": "git push && git push --tags",
"prepare": "husky install && npm run build",
Expand Down Expand Up @@ -69,8 +68,8 @@
"@types/node": "^14.18.21",
"@types/signal-exit": "^3.0.1",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.3",
"@typescript-eslint/parser": "^5.30.3",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"acorn": "^8.7.1",
"acorn-jsx": "^5.3.2",
"acorn-walk": "^8.2.0",
Expand All @@ -85,7 +84,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"execa": "^5.1.1",
"execa": "^6.1.0",
"fixturify": "^2.1.1",
"fs-extra": "^10.1.0",
"hash.js": "^1.1.7",
Expand All @@ -97,7 +96,7 @@
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"pretty-bytes": "^5.6.0",
"pretty-bytes": "^6.0.0",
"pretty-ms": "^8.0.0",
"requirejs": "^2.3.6",
"rollup": "^2.75.7",
Expand Down
11 changes: 11 additions & 0 deletions scripts/find-config.js
@@ -0,0 +1,11 @@
import { promises as fs } from 'fs';
import { resolve } from 'path';

export async function findConfigFileName(targetDir) {
const filesInWorkingDir = new Set(await fs.readdir(targetDir));
for (const extension of ['mjs', 'cjs', 'ts', 'js']) {
const fileName = `rollup.config.${extension}`;
if (filesInWorkingDir.has(fileName)) return resolve(targetDir, fileName);
}
throw new Error('The repository needs to have a file "rollup.config.js" at the top level.');
}
39 changes: 0 additions & 39 deletions scripts/load-perf-config.js

This file was deleted.

3 changes: 3 additions & 0 deletions scripts/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
8 changes: 0 additions & 8 deletions scripts/perf-debug.js

This file was deleted.

22 changes: 10 additions & 12 deletions scripts/perf-init.js
@@ -1,13 +1,14 @@
/* eslint-disable no-console */

const { accessSync, constants } = require('fs');
const path = require('path');
const execa = require('execa');
const { removeSync } = require('fs-extra');
const repoWithBranch = process.argv[2];
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { execa } from 'execa';
import fs from 'fs-extra';
import { findConfigFileName } from './find-config.js';

const TARGET_DIR = path.resolve(__dirname, '..', 'perf');
const TARGET_DIR = path.resolve(dirname(fileURLToPath(import.meta.url)), '..', 'perf');
const VALID_REPO = /^([^/\s#]+\/[^/\s#]+)(#([^/\s#]+))?$/;
const repoWithBranch = process.argv[2];

if (process.argv.length !== 3 || !VALID_REPO.test(repoWithBranch)) {
console.error(
Expand All @@ -16,8 +17,9 @@ if (process.argv.length !== 3 || !VALID_REPO.test(repoWithBranch)) {
);
process.exit(1);
}

console.error(`Cleaning up '${TARGET_DIR}'...`);
removeSync(TARGET_DIR);
fs.removeSync(TARGET_DIR);

const [, repo, , branch] = VALID_REPO.exec(repoWithBranch);

Expand All @@ -42,11 +44,7 @@ async function setupNewRepo(repo, branch) {
}
gitArgs.push(`https://github.com/${repo}.git`, TARGET_DIR);
await execWithOutput('git', gitArgs);
try {
accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), constants.R_OK);
} catch (e) {
throw new Error('The repository needs to have a file "rollup.config.js" at the top level.');
}
await findConfigFileName(TARGET_DIR);
process.chdir(TARGET_DIR);
await execWithOutput('npm', ['install']);
}
72 changes: 37 additions & 35 deletions scripts/perf.js
@@ -1,14 +1,29 @@
/* eslint-disable import/no-unresolved */
/* global gc */

const { readFileSync, writeFileSync } = require('fs');
const path = require('path');
const colorette = require('colorette');
const prettyBytes = require('pretty-bytes');
const rollup = require('../dist/rollup.js');
const { loadPerfConfig, targetDir } = require('./load-perf-config');
import { readFileSync, writeFileSync } from 'fs';
import path, { dirname } from 'path';
import { cwd } from 'process';
import { fileURLToPath } from 'url';
import { createColors } from 'colorette';
import prettyBytes from 'pretty-bytes';
import loadConfigFile from '../dist/loadConfigFile.js';
import { rollup } from '../dist/rollup.js';
import { findConfigFileName } from './find-config.js';

const initialDir = process.cwd();
const initialDir = cwd();
const targetDir = path.resolve(dirname(fileURLToPath(import.meta.url)), '..', 'perf');
const perfFile = path.resolve(targetDir, 'rollup.perf.json');
const { bold, underline, cyan, red, green } = createColors();
const MIN_ABSOLUTE_TIME_DEVIATION = 10;
const RELATIVE_DEVIATION_FOR_COLORING = 5;

process.chdir(targetDir);
const configFile = await findConfigFileName(targetDir);
const configs = await loadConfigFile(
configFile,
configFile.endsWith('.ts') ? { configPlugin: 'typescript' } : {}
);

let numberOfRunsToAverage = 6;
let numberOfDiscardedResults = 3;
Expand All @@ -27,13 +42,15 @@ if (!(numberOfDiscardedResults >= 0) || !(numberOfDiscardedResults < numberOfRun
process.exit(1);
}
console.info(
colorette.bold(
`Calculating the average of ${colorette.cyan(
numberOfRunsToAverage
)} runs discarding the ${colorette.cyan(numberOfDiscardedResults)} largest results.\n`
bold(
`Calculating the average of ${cyan(numberOfRunsToAverage)} runs discarding the ${cyan(
numberOfDiscardedResults
)} largest results.\n`
) + 'Run "npm run perf <number of runs> <number of discarded results>" to change that.'
);

await calculatePrintAndPersistTimings(configs.options[0], await getExistingTimings());

function getSingleAverage(times, runs, discarded) {
const actualDiscarded = Math.min(discarded, runs - 1);
return (
Expand Down Expand Up @@ -99,7 +116,7 @@ async function buildAndGetTimings(config) {
}
gc();
process.chdir(targetDir);
const bundle = await rollup.rollup(config);
const bundle = await rollup(config);
process.chdir(initialDir);
await bundle.generate(config.output);
return bundle.getTimings();
Expand All @@ -111,9 +128,9 @@ function printMeasurements(average, existingAverage, filter = /.*/) {
printedLabels.forEach(label => {
let color = text => text;
if (label[0] === '#') {
color = colorette.bold;
color = bold;
if (label[1] !== '#') {
color = colorette.underline;
color = underline;
}
}
console.info(
Expand All @@ -132,16 +149,14 @@ function printMeasurements(average, existingAverage, filter = /.*/) {
}

function clearLines(numberOfLines) {
console.info('\33[A' + '\33[2K\33[A'.repeat(numberOfLines));
// console.info('\33[A' + '\33[2K\33[A'.repeat(numberOfLines));
}

function getExistingTimings() {
try {
const timings = JSON.parse(readFileSync(perfFile, 'utf8'));
console.info(
colorette.bold(
`Comparing with ${colorette.cyan(perfFile)}. Delete this file to create a new base line.`
)
bold(`Comparing with ${cyan(perfFile)}. Delete this file to create a new base line.`)
);
return timings;
} catch (e) {
Expand All @@ -152,22 +167,13 @@ function getExistingTimings() {
function persistTimings(timings) {
try {
writeFileSync(perfFile, JSON.stringify(timings, null, 2), 'utf8');
console.info(
colorette.bold(
`Saving performance information to new reference file ${colorette.cyan(perfFile)}.`
)
);
console.info(bold(`Saving performance information to new reference file ${cyan(perfFile)}.`));
} catch (e) {
console.error(
colorette.bold(`Could not persist performance information in ${colorette.cyan(perfFile)}.`)
);
console.error(bold(`Could not persist performance information in ${cyan(perfFile)}.`));
process.exit(1);
}
}

const MIN_ABSOLUTE_TIME_DEVIATION = 10;
const RELATIVE_DEVIATION_FOR_COLORING = 5;

function getFormattedTime(currentTime, persistedTime = currentTime) {
let color = text => text,
formattedTime = `${currentTime.toFixed(0)}ms`;
Expand All @@ -179,7 +185,7 @@ function getFormattedTime(currentTime, persistedTime = currentTime) {
0
)}ms, ${sign}${relativeDeviation.toFixed(1)}%)`;
if (relativeDeviation > RELATIVE_DEVIATION_FOR_COLORING) {
color = currentTime >= persistedTime ? colorette.red : colorette.green;
color = currentTime >= persistedTime ? red : green;
}
}
return color(formattedTime);
Expand All @@ -193,11 +199,7 @@ function getFormattedMemory(currentMemory, persistedMemory = currentMemory) {
const relativeDeviation = 100 * (absoluteDeviation / persistedMemory);
if (relativeDeviation > RELATIVE_DEVIATION_FOR_COLORING) {
formattedMemory += ` (${sign}${relativeDeviation.toFixed(0)}%)`;
color = currentMemory >= persistedMemory ? colorette.red : colorette.green;
color = currentMemory >= persistedMemory ? red : green;
}
return color(formattedMemory);
}

loadPerfConfig().then(async config =>
calculatePrintAndPersistTimings(config, await getExistingTimings())
);
26 changes: 12 additions & 14 deletions scripts/test-package.js
@@ -1,16 +1,14 @@
const pkg = require('../package.json');
import { readFile } from 'fs/promises';

function checkChokidar() {
const chokidarPkg = require('../node_modules/chokidar/package.json');
const chokidarFsevents =
chokidarPkg.dependencies.fsevents || chokidarPkg.optionalDependencies.fsevents;
if (!chokidarFsevents) return;
const pkgFsevents = pkg.optionalDependencies.fsevents;
if (chokidarFsevents !== pkgFsevents) {
throw new Error(
`The dependency "fsevents" should exist with the same version range "${chokidarFsevents}" as it has for chokidar but it has "${pkgFsevents}".`
);
}
const pkg = JSON.parse(await readFile(new URL('../package.json', import.meta.url), 'utf8'));
const chokidarPkg = JSON.parse(
await readFile(new URL('../node_modules/chokidar/package.json', import.meta.url), 'utf8')
);
const chokidarFsevents =
chokidarPkg.dependencies.fsevents || chokidarPkg.optionalDependencies.fsevents;
const pkgFsevents = pkg.optionalDependencies.fsevents;
if (chokidarFsevents !== pkgFsevents) {
throw new Error(
`The dependency "fsevents" should exist with the same version range "${chokidarFsevents}" as it has for chokidar but it has "${pkgFsevents}".`
);
}

checkChokidar();
9 changes: 5 additions & 4 deletions scripts/update-git-commit.js
@@ -1,6 +1,7 @@
const { execSync } = require('child_process');
const { writeFileSync } = require('fs');
const { join } = require('path');
import { execSync } from 'child_process';
import { writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

let revision;
try {
Expand All @@ -9,4 +10,4 @@ try {
console.warn('Could not determine git commit when building Rollup.');
revision = '(could not be determined)';
}
writeFileSync(join(__dirname, '../.commithash'), revision);
writeFileSync(join(dirname(fileURLToPath(import.meta.url)), '../.commithash'), revision);
19 changes: 10 additions & 9 deletions scripts/update-snapshots.js
@@ -1,10 +1,11 @@
#!/usr/bin/env node

const { readdirSync } = require('fs');
const { resolve, join } = require('path');
const { copySync, removeSync } = require('fs-extra');
import { readdirSync } from 'fs';
import { dirname, join, resolve } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs-extra';

const basePath = resolve(__dirname, '../test');
const basePath = resolve(dirname(fileURLToPath(import.meta.url)), '../test');

const formPath = join(basePath, 'form/samples');
const formDirsToHandle = readdirSync(formPath);
Expand All @@ -15,10 +16,10 @@ for (const dir of formDirsToHandle) {
formDirsToHandle.push(...testFiles.map(filename => join(dir, filename)));
} else if (testFiles.includes('_actual')) {
const expectedPath = join(testPath, '_expected');
removeSync(expectedPath);
copySync(join(testPath, '_actual'), expectedPath);
fs.removeSync(expectedPath);
fs.copySync(join(testPath, '_actual'), expectedPath);
} else if (testFiles.includes('_actual.js')) {
copySync(join(testPath, '_actual.js'), join(testPath, '_expected.js'));
fs.copySync(join(testPath, '_actual.js'), join(testPath, '_expected.js'));
} else {
throw new Error(`Could not find test output in ${testPath}`);
}
Expand All @@ -33,8 +34,8 @@ for (const dir of chunkingDirsToHandle) {
chunkingDirsToHandle.push(...testFiles.map(filename => join(dir, filename)));
} else if (testFiles.includes('_actual')) {
const expectedPath = join(testPath, '_expected');
removeSync(expectedPath);
copySync(join(testPath, '_actual'), expectedPath);
fs.removeSync(expectedPath);
fs.copySync(join(testPath, '_actual'), expectedPath);
} else {
throw new Error(`Could not find test output in ${testPath}`);
}
Expand Down