Skip to content

Commit

Permalink
Wire up the @apollo/client/cache entry point
Browse files Browse the repository at this point in the history
Similar to the changes made in #5541, this commit adds a direct
entry point for the Apollo Client cache code. Importing from
`@apollo/client/cache` gives access to the cache code directly,
without pulling in other parts of the Apollo Client codebase.
  • Loading branch information
hwillson committed Nov 13, 2019
1 parent 94f3b84 commit 819ddef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
75 changes: 43 additions & 32 deletions config/prepareDist.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
const fs = require('fs');
const recast = require('recast');

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


/* @apollo/client */

Expand Down Expand Up @@ -42,7 +44,7 @@ const distPackageJson = JSON.stringify(packageJson, (_key, value) => {
}, 2) + "\n";

// Save the modified package.json to "dist"
fs.writeFileSync(`${__dirname}/../dist/package.json`, distPackageJson);
fs.writeFileSync(`${distRoot}/package.json`, distPackageJson);

// Copy supporting files into "dist"
const srcDir = `${__dirname}/..`;
Expand All @@ -51,7 +53,7 @@ fs.copyFileSync(`${srcDir}/README.md`, `${destDir}/README.md`);
fs.copyFileSync(`${srcDir}/LICENSE`, `${destDir}/LICENSE`);


/* @apollo/client/core */
/* @apollo/client/core, @apollo/client/cache */

function buildPackageJson(bundleName) {
return JSON.stringify({
Expand All @@ -62,33 +64,42 @@ function buildPackageJson(bundleName) {
}, null, 2) + "\n";
}

// Create a `core` bundle package.json, storing it in the dist core
// directory. This helps provide a way for Apollo Client to be used without
// React, via `@apollo/client/core`.
fs.writeFileSync(
`${__dirname}/../dist/core/package.json`,
buildPackageJson('core')
);

// Build a new `core.cjs.js` entry point file, that includes everything
// except the exports listed in `src/react/index.ts`. Copy this file into
// the `dist/core` directory, to allow Apollo Client core only imports
// using `@apollo/client/core`.

const reactIndexSrc = fs.readFileSync(`${__dirname}/../dist/react/index.js`);
const reactExports = [];
recast.visit(recast.parse(reactIndexSrc), {
visitExportSpecifier(path) {
reactExports.push(path.value.exported.name);
return false;
},
});

fs.writeFileSync(`${__dirname}/../dist/core/core.cjs.js`, [
"const allExports = require('../apollo-client.cjs');",
`const reactExportNames = new Set(${JSON.stringify(reactExports)});`,
"Object.keys(allExports).forEach(name => {",
" if (!reactExportNames.has(name)) exports[name] = allExports[name];",
"});",
"",
].join('\n'));
function loadExportNames(bundleName) {
const indexSrc =
fs.readFileSync(`${distRoot}/${bundleName}/index.js`);
const exportNames = [];
recast.visit(recast.parse(indexSrc), {
visitExportSpecifier(path) {
exportNames.push(path.value.exported.name);
return false;
},
});
return exportNames;
}

function writeCjsIndex(bundleName, exportNames, includeNames = true) {
const filterPrefix = includeNames ? '' : '!';
fs.writeFileSync(`${distRoot}/${bundleName}/${bundleName}.cjs.js`, [
"const allExports = require('../apollo-client.cjs');",
`const names = new Set(${JSON.stringify(exportNames)});`,
"Object.keys(allExports).forEach(name => {",
` if (${filterPrefix}names.has(name)) {`,
" exports[name] = allExports[name];",
" }",
"});",
"",
].join('\n'));
}

// Create `core` and `cache` bundle package.json files, storing them in their
// associated dist directory. This helps provide a way for the Apollo Client
// core to be used without React (via `@apollo/client/core`), and the cache
// to be used by itself (via `@apollo/client/cache`). Also create
// `core.cjs.js` and `cache.cjs.js` CommonJS entry point files that only
// include the exports needed for each bundle.

fs.writeFileSync(`${distRoot}/core/package.json`, buildPackageJson('core'));
writeCjsIndex('core', loadExportNames('react'), false);

fs.writeFileSync(`${distRoot}/cache/package.json`, buildPackageJson('cache'));
writeCjsIndex('cache', loadExportNames('cache'));
10 changes: 10 additions & 0 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { Transaction, ApolloCache } from './core/cache';
export { Cache } from './core/types/Cache';
export { DataProxy } from './core/types/DataProxy';

export {
InMemoryCache,
InMemoryCacheConfig,
} from './inmemory/inMemoryCache';
export { defaultDataIdFromObject } from './inmemory/policies';
export * from './inmemory/types';
10 changes: 1 addition & 9 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ export { isApolloError, ApolloError } from '../errors/ApolloError';

/* Cache */

export { Transaction, ApolloCache } from '../cache/core/cache';
export { Cache } from '../cache/core/types/Cache';
export { DataProxy } from '../cache/core/types/DataProxy';
export {
InMemoryCache,
InMemoryCacheConfig,
} from '../cache/inmemory/inMemoryCache';
export { defaultDataIdFromObject } from '../cache/inmemory/policies';
export * from '../cache/inmemory/types';
export * from '../cache';

/* Link */

Expand Down

0 comments on commit 819ddef

Please sign in to comment.