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

Update module resolution to NodeNext #10620

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/bundlesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { readFileSync } from "fs";
import { join } from "path";
import { gzipSync } from "zlib";
import bytes from "bytes";
import path from "path";
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const gzipBundleByteLengthLimit = bytes("32.65KB");
const minFile = join("dist", "apollo-client.min.cjs");
Expand Down
2 changes: 1 addition & 1 deletion config/distSpotFixes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs";
import { EOL } from "os";
import { distDir } from './helpers';
import { distDir } from './helpers.js';

export function applyDistSpotFixes() {
sanitizeDEV();
Expand Down
43 changes: 26 additions & 17 deletions config/entryPoints.js → config/entryPoints.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { posix as path } from "path";

export interface EntryPoint {
dirs: string[],
bundleName?: string,
extensions?: string[],
sideEffects?: boolean,
}

const entryPoints = [
{ dirs: [], bundleName: "main" },
{ dirs: ['cache'] },
Expand Down Expand Up @@ -38,23 +47,25 @@ entryPoints.forEach(info => {
node.isEntry = true;
});

exports.forEach = function(callback, context) {
export function forEach(callback: (value: EntryPoint, index: number, array: EntryPoint[]) => void, context?: any) {
entryPoints.forEach(callback, context);
};
}

exports.map = function map(callback, context) {
export function map<U>(callback: (value: EntryPoint, index: number, array: EntryPoint[]) => U, context?: any) {
return entryPoints.map(callback, context);
};

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

exports.check = function (id, parentId) {
export function check(id: string, parentId: string) {
const resolved = path.resolve(path.dirname(parentId), id);
const importedParts = partsAfterDist(resolved);

if (importedParts) {
const entryPointIndex = lengthOfLongestEntryPoint(importedParts);
if (entryPointIndex === importedParts.length) {
// if the import includes an index file, don't count it towards the entry point length
const importPathLength = importedParts[importedParts.length - 1] === 'index.js'
? importedParts.length - 1
: importedParts.length
if (entryPointIndex === importPathLength) {
return true;
}

Expand All @@ -81,23 +92,21 @@ exports.check = function (id, parentId) {
}

return false;
};
}

function partsAfterDist(id) {
function partsAfterDist(id: string) {
const parts = id.split(path.sep);
const distIndex = parts.lastIndexOf("dist");
if (distIndex >= 0) {
return parts.slice(distIndex + 1);
}
return distIndex >= 0 ? parts.slice(distIndex + 1) : []
}

exports.getEntryPointDirectory = function (file) {
export function getEntryPointDirectory(file: string) {
const parts = partsAfterDist(file) || file.split(path.sep);
const len = lengthOfLongestEntryPoint(parts);
if (len >= 0) return parts.slice(0, len).join(path.sep);
};
}

function lengthOfLongestEntryPoint(parts) {
function lengthOfLongestEntryPoint(parts: string[]) {
let node = lookupTrie;
let longest = -1;
for (let i = 0; node && i < parts.length; ++i) {
Expand All @@ -110,7 +119,7 @@ function lengthOfLongestEntryPoint(parts) {
return longest;
}

function arraysEqualUpTo(a, b, end) {
function arraysEqualUpTo(a: string[], b: string[], end: number) {
for (let i = 0; i < end; ++i) {
if (a[i] !== b[i]) return false;
}
Expand Down
5 changes: 4 additions & 1 deletion config/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as path from "path";
import * as recast from "recast";
import * as parser from "recast/parsers/babel";
import * as parser from "recast/parsers/babel.js";
import glob = require("glob");
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

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

Expand Down
77 changes: 0 additions & 77 deletions config/jest.config.js

This file was deleted.

89 changes: 89 additions & 0 deletions config/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const defaults = {
rootDir: "src",
preset: "ts-jest/presets/default-esm",
testEnvironment: "jsdom",
testEnvironmentOptions: {
url: "http://localhost",
},
snapshotFormat: {
escapeString: true,
printBasicPrototype: true,
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: 'tsconfig.build.json',
useESM: true,
diagnostics: {
warnOnly: process.env.TEST_ENV !== "ci",
},
},
],
},
moduleFileExtensions: [
"js",
"mjs",
"cjs",
"jsx",
"ts",
"tsx",
"json",
"node",
],
moduleNameMapper: {
// Ignore '.js' at the end of imports; part of ESM support.
"^(\\.{1,2}/.*)\\.m?js$": "$1",
},
};

const ignoreTSFiles = ".ts$";
const ignoreTSXFiles = ".tsx$";

const react18TestFileIgnoreList = [
// ignore core tests (.ts files) as they are run separately
// to avoid running them twice with both react versions
// since they do not import react
ignoreTSFiles,
// failing hoc tests (8)
"src/react/hoc/__tests__/mutations/queries.test.tsx",
"src/react/hoc/__tests__/mutations/recycled-queries.test.tsx",
"src/react/hoc/__tests__/queries/errors.test.tsx",
"src/react/hoc/__tests__/queries/lifecycle.test.tsx",
"src/react/hoc/__tests__/queries/loading.test.tsx",
"src/react/hoc/__tests__/queries/observableQuery.test.tsx",
"src/react/hoc/__tests__/queries/skip.test.tsx",
"src/react/hoc/__tests__/subscriptions/subscriptions.test.tsx",
// failing components tests (1)
"src/react/components/__tests__/client/Query.test.tsx",
];

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

const standardReact18Config = {
...defaults,
displayName: "ReactDOM 18",
testPathIgnorePatterns: react18TestFileIgnoreList,
};

const standardReact17Config = {
...defaults,
displayName: "ReactDOM 17",
testPathIgnorePatterns: [ignoreTSFiles],
moduleNameMapper: {
...defaults.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",
},
};

export default {
projects: [tsStandardConfig, standardReact17Config, standardReact18Config],
};
4 changes: 2 additions & 2 deletions config/postprocessDist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from "fs";
import * as path from "path";
import resolve from "resolve";
import { distDir, eachFile, reparse, reprint } from './helpers';
import { distDir, eachFile, reparse, reprint } from './helpers.js';

import { applyDistSpotFixes } from "./distSpotFixes";
import { applyDistSpotFixes } from "./distSpotFixes.js";
applyDistSpotFixes();

// The primary goal of the 'npm run resolve' script is to make ECMAScript
Expand Down
5 changes: 2 additions & 3 deletions config/precheck.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const {
lockfileVersion,
} = require("../package-lock.json");
import packageJson from "../package-lock.json" assert { type: 'json' };

const lockfileVersion = packageJson.lockfileVersion;
const expectedVersion = 2;

if (typeof lockfileVersion !== "number" ||
Expand Down
8 changes: 5 additions & 3 deletions config/prepareChangesetsRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
// - 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");
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const distRoot = `${__dirname}/../dist`;
const srcDir = `${__dirname}/..`;
const destDir = `${srcDir}/dist`;

Expand Down
Loading