Skip to content

Commit 9e8538d

Browse files
committed
Compile ESM bundle to CJS using Babel instead of Rollup.
#4843 (comment) Fixes #4843.
1 parent f7ecafa commit 9e8538d

File tree

3 files changed

+147
-26
lines changed

3 files changed

+147
-26
lines changed

config/rollup.config.js

+56-21
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,60 @@ 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+
allowTopLevelThis: true,
105+
}],
106+
],
107+
});
108+
109+
// There doesn't seem to be any way to get Rollup to emit a source map
110+
// that goes all the way back to the source file (rather than just to
111+
// the bundle.esm.js intermediate file), so we pass sourcemap:false in
112+
// the output options above, and manually write the CJS and UMD source
113+
// maps here.
114+
fs.writeFileSync(
115+
outputFile(toFormat) + '.map',
116+
JSON.stringify(output.map),
117+
);
118+
119+
return {
120+
code: output.code,
121+
};
122+
}
123+
}],
124+
}
125+
}
126+
127+
return [
128+
fromSource('esm'),
129+
fromESM('cjs'),
130+
fromESM('umd'),
96131
{
97132
input: outputFile('cjs'),
98133
output: {
99134
file: outputFile('cjs.min'),
100-
format: 'cjs',
135+
format: 'esm',
101136
},
102137
plugins: [
103138
minify({

package-lock.json

+88-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -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)