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

Compile ESM bundle to CJS using Babel instead of Rollup. #4911

Merged
merged 3 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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