diff --git a/config/prepareDist.js b/config/prepareDist.js index 86e5f8a9c07..906baaa3b15 100644 --- a/config/prepareDist.js +++ b/config/prepareDist.js @@ -14,6 +14,8 @@ const fs = require('fs'); const recast = require('recast'); +const distRoot = `${__dirname}/../dist`; + /* @apollo/client */ @@ -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}/..`; @@ -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({ @@ -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')); diff --git a/src/cache/index.ts b/src/cache/index.ts new file mode 100644 index 00000000000..28385dae60d --- /dev/null +++ b/src/cache/index.ts @@ -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'; diff --git a/src/core/index.ts b/src/core/index.ts index c84426336e2..33b17f9fcca 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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 */