@@ -2,6 +2,10 @@ import nodeResolve from 'rollup-plugin-node-resolve';
2
2
import typescriptPlugin from 'rollup-plugin-typescript2' ;
3
3
import typescript from 'typescript' ;
4
4
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' ;
5
9
import invariantPlugin from 'rollup-plugin-invariant' ;
6
10
import { terser as minify } from 'rollup-plugin-terser' ;
7
11
@@ -49,29 +53,14 @@ export function rollup({
49
53
return './lib/' + outputPrefix + '.' + format + '.js' ;
50
54
}
51
55
52
- function convert ( format ) {
56
+ function fromSource ( format ) {
53
57
return {
54
- input : outputFile ( 'esm' ) ,
58
+ input,
55
59
external,
56
60
output : {
57
61
file : outputFile ( format ) ,
58
62
format,
59
63
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 ,
75
64
} ,
76
65
plugins : [
77
66
nodeResolve ( {
@@ -90,14 +79,60 @@ export function rollup({
90
79
} ) ,
91
80
] ,
92
81
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' ) ,
96
131
{
97
132
input : outputFile ( 'cjs' ) ,
98
133
output : {
99
134
file : outputFile ( 'cjs.min' ) ,
100
- format : 'cjs ' ,
135
+ format : 'esm ' ,
101
136
} ,
102
137
plugins : [
103
138
minify ( {
0 commit comments