Skip to content

Commit

Permalink
Avoid creating a core CJS bundle by re-using main bundle exports
Browse files Browse the repository at this point in the history
These changes help avoid creating a CJS specific bundle for
`@apollo/client/core` by comparing `dist/apollo-client.cjs.js`
and `dist/react/index.js` exports, then re-exporting all
non-React exports from the main CJS bundle.
  • Loading branch information
hwillson committed Nov 12, 2019
1 parent e28c3d9 commit 5fdea43
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 38 deletions.
42 changes: 41 additions & 1 deletion config/prepareDist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`,
Expand All @@ -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
);
14 changes: 2 additions & 12 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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}`),
];
}

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 1 addition & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
21 changes: 21 additions & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 5fdea43

Please sign in to comment.