Skip to content

Commit

Permalink
format config scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jul 31, 2023
1 parent 20fe0c6 commit d987931
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 215 deletions.
14 changes: 0 additions & 14 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,3 @@ node_modules/
/src/react/hoc/__tests__/ssr/server.test.tsx
/src/react/hoc/__tests__/subscriptions/subscriptions.test.tsx
/src/react/hooks/internal/__tests__/useDeepMemo.test.ts
/config/bundlesize.ts
/config/compare-build-output-to.sh
/config/entryPoints.js
/config/helpers.ts
/config/jest.config.js
/config/postprocessDist.ts
/config/precheck.js
/config/prepareChangesetsRelease.ts
/config/prepareDist.js
/config/processInvariants.ts
/config/rewriteSourceMaps.ts
/config/rollup.config.js
/config/tsconfig.json
/config/version.js
30 changes: 14 additions & 16 deletions config/bundlesize.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { readFileSync } from "fs";
import { join } from "path";
import { gzipSync } from "zlib";
import bytes from "bytes";
import { readFileSync } from 'fs';
import { join } from 'path';
import { gzipSync } from 'zlib';
import bytes from 'bytes';

const gzipBundleByteLengthLimit = bytes("35.25KB");
const minFile = join("dist", "apollo-client.min.cjs");
const minPath = join(__dirname, "..", minFile);
const gzipBundleByteLengthLimit = bytes('35.25KB');
const minFile = join('dist', 'apollo-client.min.cjs');
const minPath = join(__dirname, '..', minFile);
const gzipByteLen = gzipSync(readFileSync(minPath)).byteLength;
const overLimit = gzipByteLen > gzipBundleByteLengthLimit;

const message = `Minified + GZIP-encoded bundle size for ${
minFile
} = ${
bytes(gzipByteLen, { unit: "KB" })
}, ${
overLimit ? "exceeding" : "under"
} limit ${
bytes(gzipBundleByteLengthLimit, { unit: "KB" })
}`;
const message = `Minified + GZIP-encoded bundle size for ${minFile} = ${bytes(
gzipByteLen,
{ unit: 'KB' }
)}, ${overLimit ? 'exceeding' : 'under'} limit ${bytes(
gzipBundleByteLengthLimit,
{ unit: 'KB' }
)}`;

if (overLimit) {
throw new Error(message);
Expand Down
22 changes: 12 additions & 10 deletions config/entryPoints.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const entryPoints = [
{ dirs: [], bundleName: "main" },
{ dirs: [], bundleName: 'main' },
{ dirs: ['cache'] },
{ dirs: ['core'] },
{ dirs: ['dev'] },
Expand All @@ -24,31 +24,31 @@ const entryPoints = [
{ dirs: ['react', 'hooks'] },
{ dirs: ['react', 'parser'] },
{ dirs: ['react', 'ssr'] },
{ dirs: ['testing'], extensions: [".js", ".jsx"] },
{ dirs: ['testing'], extensions: ['.js', '.jsx'] },
{ dirs: ['testing', 'core'] },
{ dirs: ['utilities'] },
{ dirs: ['utilities', 'globals'], sideEffects: true },
];

const lookupTrie = Object.create(null);
entryPoints.forEach(info => {
entryPoints.forEach((info) => {
let node = lookupTrie;
info.dirs.forEach(dir => {
info.dirs.forEach((dir) => {
const dirs = node.dirs || (node.dirs = Object.create(null));
node = dirs[dir] || (dirs[dir] = { isEntry: false });
});
node.isEntry = true;
});

exports.forEach = function(callback, context) {
exports.forEach = function (callback, context) {
entryPoints.forEach(callback, context);
};

exports.map = function map(callback, context) {
return entryPoints.map(callback, context);
};

const path = require("path").posix;
const path = require('path').posix;

exports.check = function (id, parentId) {
const resolved = path.resolve(path.dirname(parentId), id);
Expand Down Expand Up @@ -76,9 +76,11 @@ exports.check = function (id, parentId) {
return false;
}

console.warn(`Risky cross-entry-point nested import of ${id} in ${
partsAfterDist(parentId).join("/")
}`);
console.warn(
`Risky cross-entry-point nested import of ${id} in ${partsAfterDist(
parentId
).join('/')}`
);
}
}

Expand All @@ -87,7 +89,7 @@ exports.check = function (id, parentId) {

function partsAfterDist(id) {
const parts = id.split(path.sep);
const distIndex = parts.lastIndexOf("dist");
const distIndex = parts.lastIndexOf('dist');
if (/^index.jsx?$/.test(parts[parts.length - 1])) {
parts.pop();
}
Expand Down
40 changes: 21 additions & 19 deletions config/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import * as path from "path";
import * as recast from "recast";
import * as parser from "recast/parsers/babel";
import glob = require("glob");
import * as path from 'path';
import * as recast from 'recast';
import * as parser from 'recast/parsers/babel';
import glob = require('glob');

export const distDir = path.resolve(__dirname, "..", "dist");
export const distDir = path.resolve(__dirname, '..', 'dist');

export function eachFile(dir: string, callback: (
absPath: string,
relPath: string,
) => any) {
export function eachFile(
dir: string,
callback: (absPath: string, relPath: string) => any
) {
const promises: Promise<any>[] = [];

return new Promise<void>((resolve, reject) => {
glob(`${dir.replace(/\\/g, '/')}/**/*.js`, (error, files) => {
if (error) return reject(error);

files.sort().forEach(file => {
files.sort().forEach((file) => {
const relPath = path.relative(dir, file);

// Outside the distDir, somehow.
if (relPath.startsWith("../")) return;
if (relPath.startsWith('../')) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".cjs")) return;
if (relPath.endsWith(".cjs.js")) return;
if (relPath.endsWith(".cjs.native.js")) return;
if (relPath.endsWith('.cjs')) return;
if (relPath.endsWith('.cjs.js')) return;
if (relPath.endsWith('.cjs.native.js')) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".min.js")) return;
if (relPath.endsWith('.min.js')) return;

// This file is not meant to be imported or processed.
if (relPath.endsWith("invariantErrorCodes.js")) return;
if (relPath.endsWith('invariantErrorCodes.js')) return;

promises.push(new Promise(resolve => {
resolve(callback(file, relPath));
}));
promises.push(
new Promise((resolve) => {
resolve(callback(file, relPath));
})
);
});

resolve();
Expand Down
42 changes: 19 additions & 23 deletions config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
const defaults = {
rootDir: "src",
preset: "ts-jest",
testEnvironment: "jsdom",
setupFilesAfterEnv: ["<rootDir>/config/jest/setup.ts"],
rootDir: 'src',
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/config/jest/setup.ts'],
globals: {
__DEV__: true,
},
testEnvironmentOptions: {
url: "http://localhost",
url: 'http://localhost',
},
snapshotFormat: {
escapeString: true,
printBasicPrototype: true
printBasicPrototype: true,
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: {
warnOnly: process.env.TEST_ENV !== 'ci'
warnOnly: process.env.TEST_ENV !== 'ci',
},
},
],
},
resolver: "ts-jest-resolver",
resolver: 'ts-jest-resolver',
};

const ignoreTSFiles = '.ts$';
Expand All @@ -34,40 +34,36 @@ const react17TestFileIgnoreList = [
// For now, we only support useSuspenseQuery with React 18, so no need to test
// it with React 17
'src/react/hooks/__tests__/useSuspenseQuery.test.tsx',
'src/react/hooks/__tests__/useBackgroundQuery.test.tsx'
]
'src/react/hooks/__tests__/useBackgroundQuery.test.tsx',
];

const tsStandardConfig = {
...defaults,
displayName: 'Core Tests',
testPathIgnorePatterns: [ignoreTSXFiles],
}
};

// For both React (Jest) "projects", ignore core tests (.ts files) as they
// do not import React, to avoid running them twice.
const standardReact18Config = {
...defaults,
displayName: "ReactDOM 18",
displayName: 'ReactDOM 18',
testPathIgnorePatterns: [ignoreTSFiles],
};

const standardReact17Config = {
...defaults,
displayName: "ReactDOM 17",
displayName: 'ReactDOM 17',
testPathIgnorePatterns: react17TestFileIgnoreList,
moduleNameMapper: {
"^react$": "react-17",
"^react-dom$": "react-dom-17",
"^react-dom/server$": "react-dom-17/server",
"^react-dom/test-utils$": "react-dom-17/test-utils",
"^@testing-library/react$": "@testing-library/react-12",
'^react$': 'react-17',
'^react-dom$': 'react-dom-17',
'^react-dom/server$': 'react-dom-17/server',
'^react-dom/test-utils$': 'react-dom-17/test-utils',
'^@testing-library/react$': '@testing-library/react-12',
},
};

module.exports = {
projects: [
tsStandardConfig,
standardReact17Config,
standardReact18Config,
],
projects: [tsStandardConfig, standardReact17Config, standardReact18Config],
};
18 changes: 10 additions & 8 deletions config/postprocessDist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { distDir } from './helpers.ts';
import fs from 'node:fs';
import path from 'node:path';

const globalTypesFile = path.resolve(distDir, "utilities/globals/global.d.ts");
fs.writeFileSync(globalTypesFile,
fs.readFileSync(globalTypesFile, "utf8")
.split("\n")
.filter(line => line.trim() !== 'const __DEV__: boolean;')
.join("\n"),
"utf8"
);
const globalTypesFile = path.resolve(distDir, 'utilities/globals/global.d.ts');
fs.writeFileSync(
globalTypesFile,
fs
.readFileSync(globalTypesFile, 'utf8')
.split('\n')
.filter((line) => line.trim() !== 'const __DEV__: boolean;')
.join('\n'),
'utf8'
);
15 changes: 4 additions & 11 deletions config/precheck.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
const {
lockfileVersion,
} = require("../package-lock.json");
const { lockfileVersion } = require('../package-lock.json');

const expectedVersion = 2;

if (typeof lockfileVersion !== "number" ||
lockfileVersion < expectedVersion) {
if (typeof lockfileVersion !== 'number' || lockfileVersion < expectedVersion) {
throw new Error(
`Old lockfileVersion (${
lockfileVersion
}) found in package-lock.json (expected ${
expectedVersion
} or later)`
`Old lockfileVersion (${lockfileVersion}) found in package-lock.json (expected ${expectedVersion} or later)`
);
}

console.log("ok", {
console.log('ok', {
lockfileVersion,
expectedVersion,
});
4 changes: 2 additions & 2 deletions config/prepareChangesetsRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// - Add both .changeset and CHANGELOG.md to an .npmignore so they are not
// included in the published package.

const fs = require("fs");
const path = require("path");
const fs = require('fs');
const path = require('path');

const distRoot = `${__dirname}/../dist`;
const srcDir = `${__dirname}/..`;
Expand Down
Loading

0 comments on commit d987931

Please sign in to comment.