diff --git a/config/prepareDist.js b/config/prepareDist.js index aca5b0a652d..96c312c948f 100644 --- a/config/prepareDist.js +++ b/config/prepareDist.js @@ -11,8 +11,13 @@ // - Create a new `package.json` for each sub-set bundle we support, and // store it in the appropriate dist sub-directory. -const packageJson = require('../package.json'); const fs = require('fs'); +const recast = require('recast'); + + +/* @apollo/client */ + +const packageJson = require('../package.json'); // The root package.json is marked as private to prevent publishing // from happening in the root of the project. This sets the package back to @@ -44,6 +49,9 @@ const destDir = `${srcDir}/dist`; fs.copyFileSync(`${srcDir}/README.md`, `${destDir}/README.md`); fs.copyFileSync(`${srcDir}/LICENSE`, `${destDir}/LICENSE`); + +/* @apollo/client/core */ + function buildPackageJson(bundleName) { return JSON.stringify({ name: `@apollo/client/${bundleName}`, @@ -60,3 +68,35 @@ 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 = []; +const ast = recast.parse(reactIndexSrc); +recast.visit(ast, { + visitExportSpecifier(path) { + reactExports.push(path.value.exported.name); + return false; + } +}); + +const coreCjs = [ + "const allExports = require('../apollo-client.cjs');", + `const filteredExports = + Object.keys(allExports) + .filter(key => !['${reactExports.join("', '")}'].includes(key)) + .reduce((acc, key) => { + acc[key] = allExports[key]; + return acc; + }, {});`, + "module.exports = filteredExports;" +].join('\n'); + +fs.writeFileSync( + `${__dirname}/../dist/core/core.cjs.js`, + coreCjs +); diff --git a/config/rollup.config.js b/config/rollup.config.js index f1a90e12d12..db4419dc9b7 100644 --- a/config/rollup.config.js +++ b/config/rollup.config.js @@ -4,6 +4,8 @@ import { terser as minify } from 'rollup-plugin-terser'; import cjs from 'rollup-plugin-commonjs'; import fs from 'fs'; +import packageJson from '../package.json'; + const distDir = './dist'; const globals = { @@ -137,24 +139,12 @@ function prepareTesting() { }; } -import packageJson from '../package.json'; -const corePackageJson = require(`../${distDir}/core/package.json`); -const coreDir = `${distDir}/core`; - function rollup() { return [ - // @apollo/client prepareESM(packageJson.module, distDir), prepareCJS(packageJson.module, packageJson.main), prepareCJSMinified(packageJson.main), prepareTesting(), - - // @apollo/client/core - prepareCJS( - `${coreDir}/${corePackageJson.module}`, - `${coreDir}/${corePackageJson.main}` - ), - prepareCJSMinified(`${coreDir}/${corePackageJson.main}`), ]; } diff --git a/package-lock.json b/package-lock.json index 2a0dc4acab9..e4c363dc034 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@apollo/client", - "version": "3.0.0-beta.4", + "version": "3.0.0-beta.7", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -6992,9 +6992,9 @@ } }, "recast": { - "version": "0.18.2", - "resolved": "https://registry.npmjs.org/recast/-/recast-0.18.2.tgz", - "integrity": "sha512-MbuHc1lzIDIn7bpxaqIAGwwtyaokkzPqINf1Vm/LA0BSyVrTgXNVTTT7RzWC9kP+vqrUoYVpd6wHhI8x75ej8w==", + "version": "0.18.5", + "resolved": "https://registry.npmjs.org/recast/-/recast-0.18.5.tgz", + "integrity": "sha512-sD1WJrpLQAkXGyQZyGzTM75WJvyAd98II5CHdK3IYbt/cZlU0UzCRVU11nUFNXX9fBVEt4E9ajkMjBlUlG+Oog==", "dev": true, "requires": { "ast-types": "0.13.2", diff --git a/package.json b/package.json index b0a16c05904..0b55008c322 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "scripts": { "prebuild": "npm run clean", "build": "npx tsc", - "postbuild": "npm run prepdist && npm run bundle", + "postbuild": "npm run bundle && npm run prepdist", "watch": "npx tsc-watch --onSuccess \"npm run postbuild\"", "clean": "npx rimraf -r dist coverage lib", "test": "jest --config ./config/jest.config.js", @@ -89,6 +89,7 @@ "prop-types": "15.7.2", "react": "^16.11.0", "react-dom": "^16.11.0", + "recast": "^0.18.5", "rimraf": "^3.0.0", "rollup": "1.21.2", "rollup-plugin-commonjs": "^10.1.0", diff --git a/src/index.ts b/src/index.ts index 419f3556c3c..78a29b8b5a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,2 @@ -/* Core */ - export * from './core'; - -/* React */ - -export { ApolloProvider } from './react/context/ApolloProvider'; -export { ApolloConsumer } from './react/context/ApolloConsumer'; -export { - getApolloContext, - resetApolloContext, - ApolloContextValue -} from './react/context/ApolloContext'; -export { useQuery } from './react/hooks/useQuery'; -export { useLazyQuery } from './react/hooks/useLazyQuery'; -export { useMutation } from './react/hooks/useMutation'; -export { useSubscription } from './react/hooks/useSubscription'; -export { useApolloClient } from './react/hooks/useApolloClient'; -export { RenderPromises } from './react/ssr/RenderPromises'; -export * from './react/types/types'; -export * from './react/parser/parser'; +export * from './react'; diff --git a/src/react/index.ts b/src/react/index.ts new file mode 100644 index 00000000000..0cf189fadde --- /dev/null +++ b/src/react/index.ts @@ -0,0 +1,21 @@ +export { ApolloProvider } from './context/ApolloProvider'; +export { ApolloConsumer } from './context/ApolloConsumer'; +export { + getApolloContext, + resetApolloContext, + ApolloContextValue +} from './context/ApolloContext'; +export { useQuery } from './hooks/useQuery'; +export { useLazyQuery } from './hooks/useLazyQuery'; +export { useMutation } from './hooks/useMutation'; +export { useSubscription } from './hooks/useSubscription'; +export { useApolloClient } from './hooks/useApolloClient'; +export { RenderPromises } from './ssr/RenderPromises'; +export { + DocumentType, + IDocumentDefinition, + operationName, + parser +} from './parser/parser'; + +export * from './types/types';