Skip to content

Commit

Permalink
Import @apollo/client/utilities/globals wherever __DEV__ is used (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored Aug 27, 2021
1 parent 120c174 commit 212b1e6
Show file tree
Hide file tree
Showing 56 changed files with 175 additions and 182 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

### Bug Fixes

- Fix ObservableQuery.getCurrentResult() returning cached data with certain fetch policies. <br/>
- Fix `ObservableQuery.getCurrentResult()` returning cached `data` with certain fetch policies. <br/>
[@brainkim](https://github.com/brainkim) in [#8718](https://github.com/apollographql/apollo-client/pull/8718)

- Prevent `ssrMode`/`ssrForceFetchDelay` from causing queries to hang. <br/>
[@brainkim](https://github.com/brainkim) in [#8709](https://github.com/apollographql/apollo-client/pull/8709)

- Import `@apollo/client/utilities/globals` internally wherever `__DEV__` is used, not just in `@apollo/client/**/index.js` entry points. <br/>
[@benjamn](https://github.com/benjamn) in [#8720](https://github.com/apollographql/apollo-client/pull/8720)

## Apollo Client 3.4.9

Expand Down
54 changes: 0 additions & 54 deletions config/checkDEV.ts

This file was deleted.

6 changes: 2 additions & 4 deletions config/entryPoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ const entryPoints = [
{ dirs: ['react', 'hooks'] },
{ dirs: ['react', 'parser'] },
{ dirs: ['react', 'ssr'] },
{ dirs: ['utilities'],
sideEffects: [
"./globals/**"
]},
{ dirs: ['utilities'] },
{ dirs: ['utilities', 'globals'], sideEffects: true },
{ dirs: ['testing'], extensions: [".js", ".jsx"] },
];

Expand Down
135 changes: 83 additions & 52 deletions config/resolveModuleIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,32 @@ import { distDir, eachFile, reparse, reprint } from './helpers';
eachFile(distDir, (file, relPath) => new Promise((resolve, reject) => {
fs.readFile(file, "utf8", (error, source) => {
if (error) return reject(error);
const output = transform(source, file);

const tr = new Transformer;
const output = tr.transform(source, file);

if (
/\b__DEV__\b/.test(source) &&
// Ignore modules that reside within @apollo/client/utilities/globals.
!relPath.startsWith("utilities/globals/")
) {
let importsUtilitiesGlobals = false;

tr.absolutePaths.forEach(absPath => {
const distRelativePath =
path.relative(distDir, absPath).split(path.sep).join("/");
if (distRelativePath === "utilities/globals/index.js") {
importsUtilitiesGlobals = true;
}
});

if (!importsUtilitiesGlobals) {
reject(new Error(`Module ${
relPath
} uses __DEV__ but does not import @apollo/client/utilities/globals`));
}
}

if (source === output) {
resolve(file);
} else {
Expand All @@ -21,61 +46,67 @@ import * as recast from "recast";
const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;

function transform(code: string, file: string) {
const ast = reparse(code);

recast.visit(ast, {
visitImportDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitImportExpression(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitExportAllDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitExportNamedDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},
});
class Transformer {
absolutePaths = new Set<string>();

return reprint(ast);
}
transform(code: string, file: string) {
const ast = reparse(code);
const transformer = this;

function isRelative(id: string) {
return id.startsWith("./") || id.startsWith("../");
}
recast.visit(ast, {
visitImportDeclaration(path) {
this.traverse(path);
transformer.normalizeSourceString(file, path.node.source);
},

function normalizeSourceString(file: string, source?: Node | null) {
if (source && n.StringLiteral.check(source) && isRelative(source.value)) {
try {
source.value = normalizeId(source.value, file);
} catch (error) {
console.error(`Failed to resolve ${source.value} in ${file}`);
process.exit(1);
visitImportExpression(path) {
this.traverse(path);
transformer.normalizeSourceString(file, path.node.source);
},

visitExportAllDeclaration(path) {
this.traverse(path);
transformer.normalizeSourceString(file, path.node.source);
},

visitExportNamedDeclaration(path) {
this.traverse(path);
transformer.normalizeSourceString(file, path.node.source);
},
});

return reprint(ast);
}

isRelative(id: string) {
return id.startsWith("./") || id.startsWith("../");
}

normalizeSourceString(file: string, source: Node | null | undefined) {
if (source && n.StringLiteral.check(source) && this.isRelative(source.value)) {
try {
source.value = this.normalizeId(source.value, file);
} catch (error) {
console.error(`Failed to resolve ${source.value} in ${file}`);
process.exit(1);
}
}
}
}

function normalizeId(id: string, file: string) {
const basedir = path.dirname(file);
const absPath = resolve.sync(id, {
basedir,
packageFilter(pkg) {
return pkg.module ? {
...pkg,
main: pkg.module,
} : pkg;
},
});
const relPath = path.relative(basedir, absPath);
const relId = relPath.split(path.sep).join('/');
return isRelative(relId) ? relId : "./" + relId;
normalizeId(id: string, file: string) {
const basedir = path.dirname(file);
const absPath = resolve.sync(id, {
basedir,
packageFilter(pkg) {
return pkg.module ? {
...pkg,
main: pkg.module,
} : pkg;
},
});
this.absolutePaths.add(absPath);
const relPath = path.relative(basedir, absPath);
const relId = relPath.split(path.sep).join('/');
return this.isRelative(relId) ? relId : "./" + relId;
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "npm run update-version && npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve && npm run check-DEV && npm run verify-version",
"postbuild": "npm run update-version && npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve && npm run verify-version",
"update-version": "node config/version.js update",
"verify-version": "node config/version.js verify",
"invariants": "ts-node-script config/processInvariants.ts",
"sourcemaps": "ts-node-script config/rewriteSourceMaps.ts",
"rollup": "rollup -c ./config/rollup.config.js",
"prepdist": "node ./config/prepareDist.js",
"resolve": "ts-node-script config/resolveModuleIds.ts",
"check-DEV": "ts-node-script config/checkDEV.ts",
"clean": "rimraf -r dist coverage lib",
"test": "jest --config ./config/jest.config.js",
"test:debug": "BABEL_ENV=server node --inspect-brk node_modules/.bin/jest --config ./config/jest.config.js --runInBand",
Expand Down
12 changes: 11 additions & 1 deletion src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ Array [
"buildQueryFromSelectionSet",
"canUseWeakMap",
"canUseWeakSet",
"checkDEV",
"checkDocument",
"cloneDeep",
"compact",
Expand Down Expand Up @@ -381,3 +380,14 @@ Array [
"valueToObjectRepresentation",
]
`;

exports[`exports of public entry points @apollo/client/utilities/globals 1`] = `
Array [
"DEV",
"InvariantError",
"checkDEV",
"global",
"invariant",
"maybe",
]
`;
2 changes: 2 additions & 0 deletions src/__tests__/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as reactParser from "../react/parser";
import * as reactSSR from "../react/ssr";
import * as testing from "../testing";
import * as utilities from "../utilities";
import * as utilitiesGlobals from "../utilities/globals";

const entryPoints = require("../../config/entryPoints.js");

Expand Down Expand Up @@ -63,6 +64,7 @@ describe('exports of public entry points', () => {
check("@apollo/client/react/ssr", reactSSR);
check("@apollo/client/testing", testing);
check("@apollo/client/utilities", utilities);
check("@apollo/client/utilities/globals", utilitiesGlobals);

it("completeness", () => {
const { join } = require("path").posix;
Expand Down
3 changes: 1 addition & 2 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { checkDEV } from "../utilities";
checkDEV();
import '../utilities/globals';

export { Transaction, ApolloCache } from './core/cache';
export { Cache } from './core/types/Cache';
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/entityStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invariant } from '../../utilities/globals';
import { dep, OptimisticDependencyFunction } from 'optimism';
import { invariant } from 'ts-invariant';
import { equal } from '@wry/equality';
import { Trie } from '@wry/trie';

Expand Down
2 changes: 2 additions & 0 deletions src/cache/inmemory/object-canon.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "../../utilities/globals";

import { Trie } from "@wry/trie";
import {
canUseWeakMap,
Expand Down
3 changes: 2 additions & 1 deletion src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { invariant, InvariantError } from '../../utilities/globals';

import {
InlineFragmentNode,
FragmentDefinitionNode,
Expand All @@ -6,7 +8,6 @@ import {
} from 'graphql';

import { Trie } from '@wry/trie';
import { invariant, InvariantError } from 'ts-invariant';

import {
FragmentMap,
Expand Down
3 changes: 2 additions & 1 deletion src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { invariant, InvariantError } from '../../utilities/globals';

import {
DocumentNode,
FieldNode,
SelectionSetNode,
} from 'graphql';
import { wrap, OptimisticWrapperFunction } from 'optimism';
import { invariant, InvariantError } from 'ts-invariant';

import {
isField,
Expand Down
3 changes: 2 additions & 1 deletion src/cache/inmemory/writeToStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invariant, InvariantError } from '../../utilities/globals';

import { SelectionSetNode, FieldNode, SelectionNode } from 'graphql';
import { invariant, InvariantError } from 'ts-invariant';
import { equal } from '@wry/equality';

import {
Expand Down
3 changes: 2 additions & 1 deletion src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invariant, InvariantError } from '../utilities/globals';

import { ExecutionResult, DocumentNode } from 'graphql';
import { invariant, InvariantError } from 'ts-invariant';

import { ApolloLink, FetchResult, GraphQLRequest, execute } from '../link/core';
import { ApolloCache, DataProxy } from '../cache';
Expand Down
3 changes: 2 additions & 1 deletion src/core/LocalState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { invariant } from '../utilities/globals';

import {
DocumentNode,
OperationDefinitionNode,
Expand All @@ -10,7 +12,6 @@ import {
visit,
BREAK,
} from 'graphql';
import { invariant } from 'ts-invariant';

import { ApolloCache } from '../cache';
import {
Expand Down
3 changes: 2 additions & 1 deletion src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { invariant } from 'ts-invariant';
import { invariant } from '../utilities/globals';

import { equal } from '@wry/equality';

import { NetworkStatus, isNetworkRequestInFlight } from './networkStatus';
Expand Down
3 changes: 2 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invariant, InvariantError } from '../utilities/globals';

import { DocumentNode } from 'graphql';
import { invariant, InvariantError } from 'ts-invariant';
import { equal } from '@wry/equality';

import { ApolloLink, execute, FetchResult } from '../link/core';
Expand Down
3 changes: 1 addition & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Core */

import { DEV, checkDEV } from "../utilities";
checkDEV();
import { DEV } from '../utilities/globals';

export {
ApolloClient,
Expand Down
Loading

0 comments on commit 212b1e6

Please sign in to comment.