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

Fix source maps and simplify getApolloContext to avoid global symbols. #7371

Merged
merged 4 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions config/rewriteSourceMaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from "fs";
import * as path from "path";
import { distDir } from './helpers';
import glob = require("glob");

glob(`${distDir}/**/*.js.map`, (error, files) => {
if (error) throw error;

const rootDir = path.dirname(distDir);

const startTime = Date.now();
let rewriteCount = 0;

Promise.all(files.map(async file => {
const content = await fs.promises.readFile(file, "utf8");
const map = JSON.parse(content);
if (map.sourcesContent) return;
if (map.sources) {
map.sourcesContent = await Promise.all(
map.sources.map((relSourcePath: string) => {
const sourcePath = path.normalize(
path.join(path.dirname(file), relSourcePath));
const relPath = path.relative(rootDir, sourcePath);
// Disallow reading paths outside rootDir.
if (relPath.startsWith("../")) {
throw new Error(`Bad path: ${sourcePath}`);
}
return fs.promises.readFile(sourcePath, "utf8");
})
);
++rewriteCount;
return fs.promises.writeFile(file, JSON.stringify(map));
}
})).then(() => {
console.log(`Rewrote ${
rewriteCount
} source maps in ${
Date.now() - startTime
}ms`);
}, error => {
console.error(error);
process.exit(-1);
});
});
9 changes: 9 additions & 0 deletions config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"sourceMap": true,
"inlineSourceMap": false,
// This option should cause sourcesContent to be included in the
// source map JSON, so we don't have to rely on the (nonexistent)
// relative paths in the sources array, but it appears tsc does not
// respect this option unless the source maps are inlined into the
// source files, which we definitely do not want. Instead, we use the
// config/rewriteSourceMaps.ts script to add sourcesContent.
"inlineSources": true,
},
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "npm run invariants && npm run rollup && npm run prepdist && npm run resolve",
"postbuild": "npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve",
"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",
Expand Down
2 changes: 1 addition & 1 deletion src/react/context/ApolloConsumer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { invariant } from 'ts-invariant';

import { ApolloClient } from '../../core';
Expand Down
35 changes: 15 additions & 20 deletions src/react/context/ApolloContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import * as React from 'react';
import { ApolloClient } from '../../core';
import { canUseWeakMap } from '../../utilities';

export interface ApolloContextValue {
client?: ApolloClient<object>;
Expand All @@ -10,27 +11,21 @@ export interface ApolloContextValue {
// (which can lead to problems like having an Apollo Client instance added
// in one context, then attempting to retrieve it from another different
// context), a single Apollo context is created and tracked in global state.
// Since the created context is React specific, we've decided to attach it to
// the `React` object for sharing.
// We use React.createContext as the key instead of just React to avoid
// ambiguities between default and namespace React imports.

// If Symbol's aren't available, we'll use a fallback string as the context
// property (we're looking at you, IE11).
const contextSymbol = typeof Symbol === 'function' && Symbol.for ?
Symbol.for('__APOLLO_CONTEXT__') :
'__APOLLO_CONTEXT__';

export function resetApolloContext() {
Object.defineProperty(React, contextSymbol, {
value: React.createContext<ApolloContextValue>({}),
enumerable: false,
configurable: true,
writable: false,
});
}
const cache = new (canUseWeakMap ? WeakMap : Map)<
typeof React.createContext,
React.Context<ApolloContextValue>
>();

export function getApolloContext() {
if (!(React as any)[contextSymbol]) {
resetApolloContext();
let context = cache.get(React.createContext)!;
if (!context) {
context = React.createContext<ApolloContextValue>({});
cache.set(React.createContext, context);
}
return (React as any)[contextSymbol] as React.Context<ApolloContextValue>;
return context;
}

export { getApolloContext as resetApolloContext }
2 changes: 1 addition & 1 deletion src/react/context/ApolloProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { invariant } from 'ts-invariant';

import { ApolloClient } from '../../core';
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { invariant } from 'ts-invariant';

import { ApolloClient } from '../../core';
Expand Down