Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jimrandomh committed Jul 22, 2020
2 parents 9de74e9 + 5766d53 commit 03e99d2
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 201 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Bug Fixes

- Rework interdependencies between `@apollo/client/*` entry points, so that CommonJS and ESM modules are supported equally well, without any duplication of shared code. <br/>
[@benjamn](https://github.com/benjamn) in [#6058](https://github.com/apollographql/apollo-client/pull/6058)
[@benjamn](https://github.com/benjamn) in [#6656](https://github.com/apollographql/apollo-client/pull/6656) and
[#6657](https://github.com/apollographql/apollo-client/pull/6657)

- Tolerate `!==` callback functions (like `onCompleted` and `onError`) in `useQuery` options, since those functions are almost always freshly evaluated each time `useQuery` is called. <br/>
[@hwillson](https://github.com/hwillson) and [@benjamn](https://github.com/benjamn) in [#6588](https://github.com/apollographql/apollo-client/pull/6588)
Expand All @@ -14,9 +15,15 @@

## Improvements

- Errors of the form `Invariant Violation: 42` thrown in production can now be looked up much more easily, by consulting the auto-generated `@apollo/client/invariantErrorCodes.js` file specific to your `@apollo/client` version. <br/>
[@benjamn](https://github.com/benjamn) in [#6665](https://github.com/apollographql/apollo-client/pull/6665)

- Make the `client` field of the `MutationResult` type non-optional, since it is always provided. <br/>
[@glasser](https://github.com/glasser) in [#6617](https://github.com/apollographql/apollo-client/pull/6617)

- Allow passing an asynchronous `options.renderFunction` to `getMarkupFromTree`. <br/>
[@richardscarrott](https://github.com/richardscarrott) in [#6576](https://github.com/apollographql/apollo-client/pull/6576)

## Apollo Client 3.0.2

## Bug Fixes
Expand Down
67 changes: 51 additions & 16 deletions config/processInvariants.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,78 @@
import { readFileSync, writeFileSync } from "fs";
import { resolve, relative } from "path";
import * as fs from "fs";
import * as path from "path";
import glob = require("glob");

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

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

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

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

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

const source = readFileSync(file, "utf8");
const source = fs.readFileSync(file, "utf8");
const output = transform(source, relPath);
if (source !== output) {
console.log("transformed invariants in " + relPath);
writeFileSync(file, output, "utf8");
fs.writeFileSync(file, output, "utf8");
}
});

fs.writeFileSync(
path.join(distDir, "invariantErrorCodes.js"),
recast.print(errorCodeManifest, {
tabWidth: 2,
}).code + "\n",
);
});

import * as recast from "recast";
import * as parser from "recast/parsers/babel";
const b = recast.types.builders;
const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;
type NumericLiteral = recast.types.namedTypes.NumericLiteral;
type CallExpression = recast.types.namedTypes.CallExpression;
type NewExpression = recast.types.namedTypes.NewExpression;
let nextErrorCode = 1;

function transform(code: string, id: string) {
const errorCodeManifest = b.objectExpression([
b.property("init",
b.stringLiteral("@apollo/client version"),
b.stringLiteral(require("../package.json").version),
),
]);

errorCodeManifest.comments = [
b.commentLine(' This file is meant to help with looking up the source of errors like', true),
b.commentLine(' "Invariant Violation: 35" and is automatically generated by the file', true),
b.commentLine(' @apollo/client/config/processInvariants.ts for each @apollo/client', true),
b.commentLine(' release. The numbers may change from release to release, so please', true),
b.commentLine(' consult the @apollo/client/invariantErrorCodes.js file specific to', true),
b.commentLine(' your @apollo/client version. This file is not meant to be imported.', true),
];

function getErrorCode(
file: string,
expr: CallExpression | NewExpression,
): NumericLiteral {
const numLit = b.numericLiteral(nextErrorCode++);
errorCodeManifest.properties.push(
b.property("init", numLit, b.objectExpression([
b.property("init", b.identifier("file"), b.stringLiteral("@apollo/client/" + file)),
b.property("init", b.identifier("node"), expr),
])),
);
return numLit;
}

function transform(code: string, file: string) {
// If the code doesn't seem to contain anything invariant-related, we
// can skip parsing and transforming it.
if (!/invariant/i.test(code)) {
Expand All @@ -50,7 +92,7 @@ function transform(code: string, id: string) {
}

const newArgs = node.arguments.slice(0, 1);
newArgs.push(b.numericLiteral(nextErrorCode++));
newArgs.push(getErrorCode(file, node));

return b.conditionalExpression(
makeNodeEnvTest(),
Expand Down Expand Up @@ -80,9 +122,7 @@ function transform(code: string, id: string) {
return;
}

const newArgs = [
b.numericLiteral(nextErrorCode++),
];
const newArgs = [getErrorCode(file, node)];

return b.conditionalExpression(
makeNodeEnvTest(),
Expand All @@ -99,11 +139,6 @@ function transform(code: string, id: string) {
return recast.print(ast).code;
}

const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;
type CallExpression = recast.types.namedTypes.CallExpression;
type NewExpression = recast.types.namedTypes.NewExpression;

function isIdWithName(node: Node, ...names: string[]) {
return n.Identifier.check(node) &&
names.some(name => name === node.name);
Expand Down
25 changes: 25 additions & 0 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ function prepareBundle({
};
}

// Resolve indirect imports and exports to the original exporting module,
// so that more imports are explicitly named (fewer *s), and all source
// module identifiers have file extensions.
function resolveESMImportsAndFileExtensions(input, outputDir) {
return {
input,
external(id) {
return externalPackages.has(id);
},
output: {
dir: outputDir,
format: 'esm',
sourcemap: true,
},
// By setting preserveModules to true, we're making sure Rollup
// doesn't attempt to create a single combined ESM bundle with the
// final result of running this job.
preserveModules: true,
plugins: [
nodeResolve(),
],
};
}

export default [
...entryPoints.map(prepareBundle),
// Convert the ESM entry point to a single CJS bundle.
Expand All @@ -104,4 +128,5 @@ export default [
prepareCJSMinified(
'./dist/apollo-client.cjs.js',
),
resolveESMImportsAndFileExtensions(packageJson.module, distDir),
];
Loading

0 comments on commit 03e99d2

Please sign in to comment.