Skip to content

Commit

Permalink
Compile ESM bundle to CJS using Babel instead of Rollup. (#4911)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
benjamn committed Jun 4, 2019
1 parent 4bd8237 commit 5048b1b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Apollo Client (vNEXT)

- 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/>
[@benjamn](https://github.com/benjamn) in [#4911](https://github.com/apollographql/apollo-client/pull/4911)

### Apollo Cache In-Memory

- Pretend that `__typename` exists on the root Query when matching fragments. <br/>
Expand Down
78 changes: 57 additions & 21 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import nodeResolve from 'rollup-plugin-node-resolve';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import path from 'path';
import fs from 'fs';
import { transformSync } from '@babel/core';
import cjsModulesTransform from '@babel/plugin-transform-modules-commonjs';
import umdModulesTransform from '@babel/plugin-transform-modules-umd';
import invariantPlugin from 'rollup-plugin-invariant';
import { terser as minify } from 'rollup-plugin-terser';

Expand Down Expand Up @@ -49,29 +53,14 @@ export function rollup({
return './lib/' + outputPrefix + '.' + format + '.js';
}

function convert(format) {
function fromSource(format) {
return {
input: outputFile('esm'),
input,
external,
output: {
file: outputFile(format),
format,
sourcemap: true,
name,
globals,
},
onwarn,
};
}

return [
{
input,
external,
output: {
file: outputFile('esm'),
format: 'esm',
sourcemap: true,
},
plugins: [
nodeResolve({
Expand All @@ -90,14 +79,61 @@ export function rollup({
}),
],
onwarn,
},
convert('umd'),
convert('cjs'),
};
}

function fromESM(toFormat) {
return {
input: outputFile('esm'),
output: {
file: outputFile(toFormat),
format: 'esm',
sourcemap: false,
},
// The UMD bundle expects `this` to refer to the global object. By default
// Rollup replaces `this` with `undefined`, but this default behavior can
// be overridden with the `context` option.
context: 'this',
plugins: [{
transform(source, id) {
const output = transformSync(source, {
inputSourceMap: JSON.parse(fs.readFileSync(id + '.map')),
sourceMaps: true,
plugins: [
[toFormat === 'umd' ? umdModulesTransform : cjsModulesTransform, {
loose: true,
allowTopLevelThis: true,
}],
],
});

// There doesn't seem to be any way to get Rollup to emit a source map
// that goes all the way back to the source file (rather than just to
// the bundle.esm.js intermediate file), so we pass sourcemap:false in
// the output options above, and manually write the CJS and UMD source
// maps here.
fs.writeFileSync(
outputFile(toFormat) + '.map',
JSON.stringify(output.map),
);

return {
code: output.code,
};
}
}],
}
}

return [
fromSource('esm'),
fromESM('cjs'),
fromESM('umd'),
{
input: outputFile('cjs'),
output: {
file: outputFile('cjs.min'),
format: 'cjs',
format: 'esm',
},
plugins: [
minify({
Expand Down
93 changes: 88 additions & 5 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"name": "apollo-cache-inmemory",
"path": "./packages/apollo-cache-inmemory/lib/bundle.cjs.min.js",
"maxSize": "5 kB"
"maxSize": "5.05 kB"
},
{
"name": "apollo-client",
Expand Down Expand Up @@ -60,6 +60,9 @@
"graphql-anywhere": "file:packages/graphql-anywhere"
},
"devDependencies": {
"@babel/core": "7.4.5",
"@babel/plugin-transform-modules-commonjs": "7.4.4",
"@babel/plugin-transform-modules-umd": "7.2.0",
"@condenast/bundlesize": "0.18.1",
"@octokit/rest": "16.27.3",
"@types/benchmark": "1.0.31",
Expand Down

0 comments on commit 5048b1b

Please sign in to comment.