From 1cf60f8b12dc31b5baf98c807bb8c5fbfbc55286 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 5 Mar 2019 14:07:44 -0500 Subject: [PATCH] Stop inlining tslib helpers like __extends and __rest. Similar in spirit to https://github.com/apollographql/apollo-link/pull/959 This inlining was first introduced in PR #2661 with the following commit: https://github.com/apollographql/react-apollo/pull/2661/commits/de2b5fc3a4c0786966d57232ca153ea0983e54fb At the time, inlining made sense because TypeScript was injecting copies of the __extends, __rest, etc. helpers into every module that used them. Depending on the tslib package seemed undesirable because the available bundle size measurement tools (e.g. bundlephobia.com) mistakenly counted the entire tslib package against react-apollo, without acknowledging the possibility of sharing that package between multiple Apollo packages. It seemed safer to inline only the helpers we needed at the top of lib/react-apollo.esm.js. Now that we have a more holistic way to measure bundle sizes (#2839), and react-apollo works better with tree-shaking tools (#2659, #2661, #2677), we know that overall application bundle sizes benefit from sharing a single copy of the tslib helper package, even if no tree-shaking is happening. Of course, with tree-shaking, that one copy of the tslib package can be shrunk to contain just the helpers that are actually used. --- rollup.config.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 23a1084524..77cf76dfc8 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -13,6 +13,19 @@ function onwarn(message) { } } +const globals = { + 'apollo-client': 'apollo.core', + 'hoist-non-react-statics': 'hoistNonReactStatics', + 'prop-types': 'propTypes', + 'react': 'react', + 'ts-invariant': 'invariant', + 'tslib': 'tslib', +}; + +function external(id) { + return Object.prototype.hasOwnProperty.call(globals, id); +} + export default [ { input: 'src/index.ts', @@ -20,12 +33,11 @@ export default [ file: 'lib/react-apollo.esm.js', format: 'esm', sourcemap: true, + globals, }, + external, plugins: [ - node({ - module: true, - only: ['tslib'] - }), + node({ module: true }), typescriptPlugin({ typescript }), invariantPlugin(), filesize(), @@ -37,8 +49,10 @@ export default [ output: { file: 'lib/react-apollo.cjs.js', format: 'cjs', - name: 'react-apollo' + name: 'react-apollo', + globals, }, + external, onwarn, }, { @@ -46,8 +60,10 @@ export default [ output: { file: 'lib/react-apollo.umd.js', format: 'umd', - name: 'react-apollo' + name: 'react-apollo', + globals, }, + external, onwarn, }, { @@ -55,8 +71,10 @@ export default [ output: { file: 'dist/bundlesize.js', format: 'cjs', - name: 'react-apollo' + name: 'react-apollo', + globals, }, + external, plugins: [ uglify({ mangle: {