Skip to content

Commit

Permalink
Use recast at build time
Browse files Browse the repository at this point in the history
Recast shouldn't be a bundle dependency.
  • Loading branch information
hwillson committed Nov 12, 2019
1 parent 3939b44 commit c83e9d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
33 changes: 33 additions & 0 deletions config/prepareDist.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// store it in the appropriate dist sub-directory.

const fs = require('fs');
const recast = require('recast');


/* @apollo/client */
Expand Down Expand Up @@ -67,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
);
31 changes: 0 additions & 31 deletions src/core/core.cjs.ts

This file was deleted.

0 comments on commit c83e9d4

Please sign in to comment.