Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace requireReactLazily with CJS bundle manipulation #6361

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ function prepareCJS(input, output) {
},
plugins: [
nodeResolve(),
// When generating the `dist/core/core.cjs.js` entry point (in
// `config/prepareDist.js`), we filter and re-export the exports we
// need from the main Apollo Client CJS bundle (to exclude React related
// code). This means that consumers of `core.cjs.js` attempt to load the
// full AC CJS bundle first (before filtering exports), which then means
// the React require in the AC CJS bundle is attempted and not found
// (since people using `core.cjs.js` want to use Apollo Client without
// React). To address this, we make React an optional require in the CJS
// bundle.
(() => {
const cjsBundle = output.replace(`${distDir}/`, '');
return {
generateBundle(_option, bundle) {
const { code } = bundle[cjsBundle];
const regex = /var React = require\('react'\);/;
const matches = code.match(regex);
if (matches && matches.length === 1) {
bundle[cjsBundle].code =
code.replace(
regex,
"try { var React = require('react'); } catch (error) {}"
);
} else {
throw new Error(
'The CJS bundle could not prepared as a single React ' +
'require could not be found.'
);
}
}
}
})()
],
};
}
Expand Down

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

16 changes: 8 additions & 8 deletions examples/bundling/no-tree-shaking/rollup-ac3/package-lock.json

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

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

147 changes: 94 additions & 53 deletions examples/bundling/tree-shaking/rollup-ac3/package-lock.json

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

4 changes: 2 additions & 2 deletions examples/bundling/tree-shaking/rollup-ac3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"dependencies": {
"@apollo/client": "file:../../../../dist",
"graphql": "^14.5.8",
"react": "^16.13.0",
"react-dom": "^16.13.0"
"react": "file:../../../../node_modules/react",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the bundling example package lock updates, and these dependency changes, aren't directly related to the CJS bundling work in this PR. I did notice they needed to be adjusted while using the bundling app to test this PR's changes, so I thought I would leave them in here.

"react-dom": "file:../../../../node_modules/react-dom"
},
"devDependencies": {
"@babel/preset-react": "^7.0.0",
Expand Down
Loading