Skip to content

Commit 5048b1b

Browse files
authored
Compile ESM bundle to CJS using Babel instead of Rollup. (#4911)
* Compile ESM bundle to CJS using Babel instead of Rollup. #4843 (comment) Fixes #4843. * Bump apollo-cache-inmemory bundle size limit from 5KB to 5.05KB. * Mention PR #4911 in CHANGELOG.md.
1 parent 4bd8237 commit 5048b1b

File tree

4 files changed

+152
-27
lines changed

4 files changed

+152
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Apollo Client (vNEXT)
44

5+
- In all Apollo Client packages, the compilation of `lib/bundle.esm.js` to `lib/bundle.cjs.js` and `lib/bundle.umd.js` now uses Babel instead of Rollup, since Babel correctly compiles some [edge cases](https://github.com/apollographql/apollo-client/issues/4843#issuecomment-495717720) that neither Rollup nor TypeScript compile correctly. <br/>
6+
[@benjamn](https://github.com/benjamn) in [#4911](https://github.com/apollographql/apollo-client/pull/4911)
7+
58
### Apollo Cache In-Memory
69

710
- Pretend that `__typename` exists on the root Query when matching fragments. <br/>

config/rollup.config.js

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import nodeResolve from 'rollup-plugin-node-resolve';
22
import typescriptPlugin from 'rollup-plugin-typescript2';
33
import typescript from 'typescript';
44
import path from 'path';
5+
import fs from 'fs';
6+
import { transformSync } from '@babel/core';
7+
import cjsModulesTransform from '@babel/plugin-transform-modules-commonjs';
8+
import umdModulesTransform from '@babel/plugin-transform-modules-umd';
59
import invariantPlugin from 'rollup-plugin-invariant';
610
import { terser as minify } from 'rollup-plugin-terser';
711

@@ -49,29 +53,14 @@ export function rollup({
4953
return './lib/' + outputPrefix + '.' + format + '.js';
5054
}
5155

52-
function convert(format) {
56+
function fromSource(format) {
5357
return {
54-
input: outputFile('esm'),
58+
input,
5559
external,
5660
output: {
5761
file: outputFile(format),
5862
format,
5963
sourcemap: true,
60-
name,
61-
globals,
62-
},
63-
onwarn,
64-
};
65-
}
66-
67-
return [
68-
{
69-
input,
70-
external,
71-
output: {
72-
file: outputFile('esm'),
73-
format: 'esm',
74-
sourcemap: true,
7564
},
7665
plugins: [
7766
nodeResolve({
@@ -90,14 +79,61 @@ export function rollup({
9079
}),
9180
],
9281
onwarn,
93-
},
94-
convert('umd'),
95-
convert('cjs'),
82+
};
83+
}
84+
85+
function fromESM(toFormat) {
86+
return {
87+
input: outputFile('esm'),
88+
output: {
89+
file: outputFile(toFormat),
90+
format: 'esm',
91+
sourcemap: false,
92+
},
93+
// The UMD bundle expects `this` to refer to the global object. By default
94+
// Rollup replaces `this` with `undefined`, but this default behavior can
95+
// be overridden with the `context` option.
96+
context: 'this',
97+
plugins: [{
98+
transform(source, id) {
99+
const output = transformSync(source, {
100+
inputSourceMap: JSON.parse(fs.readFileSync(id + '.map')),
101+
sourceMaps: true,
102+
plugins: [
103+
[toFormat === 'umd' ? umdModulesTransform : cjsModulesTransform, {
104+
loose: true,
105+
allowTopLevelThis: true,
106+
}],
107+
],
108+
});
109+
110+
// There doesn't seem to be any way to get Rollup to emit a source map
111+
// that goes all the way back to the source file (rather than just to
112+
// the bundle.esm.js intermediate file), so we pass sourcemap:false in
113+
// the output options above, and manually write the CJS and UMD source
114+
// maps here.
115+
fs.writeFileSync(
116+
outputFile(toFormat) + '.map',
117+
JSON.stringify(output.map),
118+
);
119+
120+
return {
121+
code: output.code,
122+
};
123+
}
124+
}],
125+
}
126+
}
127+
128+
return [
129+
fromSource('esm'),
130+
fromESM('cjs'),
131+
fromESM('umd'),
96132
{
97133
input: outputFile('cjs'),
98134
output: {
99135
file: outputFile('cjs.min'),
100-
format: 'cjs',
136+
format: 'esm',
101137
},
102138
plugins: [
103139
minify({

package-lock.json

Lines changed: 88 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{
3030
"name": "apollo-cache-inmemory",
3131
"path": "./packages/apollo-cache-inmemory/lib/bundle.cjs.min.js",
32-
"maxSize": "5 kB"
32+
"maxSize": "5.05 kB"
3333
},
3434
{
3535
"name": "apollo-client",
@@ -60,6 +60,9 @@
6060
"graphql-anywhere": "file:packages/graphql-anywhere"
6161
},
6262
"devDependencies": {
63+
"@babel/core": "7.4.5",
64+
"@babel/plugin-transform-modules-commonjs": "7.4.4",
65+
"@babel/plugin-transform-modules-umd": "7.2.0",
6366
"@condenast/bundlesize": "0.18.1",
6467
"@octokit/rest": "16.27.3",
6568
"@types/benchmark": "1.0.31",

0 commit comments

Comments
 (0)