1
1
import commonjs from '@rollup/plugin-commonjs' ;
2
- import resolve from '@rollup/plugin-node-resolve' ;
2
+ import { nodeResolve } from '@rollup/plugin-node-resolve' ;
3
3
import babel from '@rollup/plugin-babel' ;
4
4
import replace from '@rollup/plugin-replace' ;
5
- import typescript from '@rollup/plugin-typescript' ;
6
5
import { terser } from 'rollup-plugin-terser' ;
7
6
import { env } from 'process' ;
8
7
import pkg from './package.json' ;
9
8
10
9
const isPlaygroundBuild = ! ! env . TEMPORAL_PLAYGROUND ;
11
- const isTest262 = ! ! env . TEST262 ;
12
- const isProduction = env . NODE_ENV === 'production' && ! isTest262 ;
10
+ const isTest262Build = ! ! env . TEST262 ;
11
+ const isProduction = env . NODE_ENV === 'production' ;
12
+ const isTranspiledBuild = ! ! env . TRANSPILE ;
13
13
const libName = 'temporal' ;
14
14
15
- const plugins = [
16
- typescript ( {
17
- typescript : require ( 'typescript' )
18
- } ) ,
19
- replace ( { exclude : 'node_modules/**' , 'globalThis.__debug__' : ! isTest262 && ! isProduction , preventAssignment : true } ) ,
20
- resolve ( { preferBuiltins : false } ) ,
21
- commonjs ( ) ,
22
- babel ( {
23
- exclude : 'node_modules/**' ,
24
- babelHelpers : 'external' ,
25
- presets : [
26
- [
27
- '@babel/preset-env' ,
28
- {
29
- targets : '> 0.25%, not dead'
15
+ function withPlugins (
16
+ options = {
17
+ babelConfig : undefined ,
18
+ optimize : false ,
19
+ debugBuild : true
20
+ }
21
+ ) {
22
+ const basePlugins = [
23
+ replace ( { exclude : 'node_modules/**' , 'globalThis.__debug__' : options . debugBuild , preventAssignment : true } ) ,
24
+ commonjs ( ) ,
25
+ nodeResolve ( { preferBuiltins : false } )
26
+ ] ;
27
+ if ( options . babelConfig ) {
28
+ basePlugins . push ( babel ( options . babelConfig ) ) ;
29
+ }
30
+ if ( options . optimize ) {
31
+ basePlugins . push (
32
+ terser ( {
33
+ keep_classnames : true ,
34
+ keep_fnames : true ,
35
+ ecma : 2015 ,
36
+ compress : {
37
+ keep_fargs : true ,
38
+ keep_classnames : true ,
39
+ keep_fnames : true
40
+ } ,
41
+ mangle : {
42
+ keep_classnames : true ,
43
+ keep_fnames : true
30
44
}
31
- ]
32
- ]
33
- } ) ,
34
- isProduction && terser ( )
35
- ] . filter ( Boolean ) ;
45
+ } )
46
+ ) ;
47
+ }
48
+ return basePlugins ;
49
+ }
36
50
37
- const input = 'lib /index.ts ' ;
51
+ const input = 'tsc-out /index.js ' ;
38
52
39
53
const external = [
40
54
// Some dependencies (e.g. es-abstract) are imported using sub-paths, so the
@@ -53,59 +67,97 @@ function outputEntry(file, format) {
53
67
} ;
54
68
}
55
69
56
- let builds = [
57
- {
58
- input,
59
- external,
60
- output : [
61
- // ESM bundle
62
- outputEntry ( pkg . module , 'es' ) ,
63
- // CJS bundle.
64
- // Note that because package.json specifies "type":"module", the name of
65
- // this file MUST end in ".cjs" in order to be treated as a CommonJS file.
66
- outputEntry ( pkg . main , 'cjs' )
67
- ] ,
68
- plugins
69
- } ,
70
- {
71
- input,
72
- // UMD bundle for using in script tags, etc
73
- // Note that some build systems don't like reading UMD files if they end in
74
- // '.cjs', so this entry in package.json should end in a .js file extension.
75
- output : [ outputEntry ( pkg . browser , 'umd' ) ] ,
76
- plugins
77
- }
78
- ] ;
70
+ const es5BundleBabelConfig = {
71
+ babelHelpers : 'bundled' ,
72
+ presets : [
73
+ [
74
+ '@babel/preset-env' ,
75
+ {
76
+ targets : '> 0.25%, not dead, ie 11'
77
+ }
78
+ ]
79
+ ]
80
+ } ;
81
+
82
+ let builds = [ ] ;
79
83
80
- if ( isTest262 ) {
84
+ if ( isTest262Build ) {
81
85
builds = [
82
86
{
83
- input : 'lib /init.ts ' ,
87
+ input : 'tsc-out /init.js ' ,
84
88
output : {
85
89
name : libName ,
86
90
file : 'dist/script.js' ,
87
91
format : 'iife' ,
88
92
sourcemap : true
89
93
} ,
90
- plugins
94
+ plugins : withPlugins ( {
95
+ debugBuild : false , // Test262 tests don't pass in debug builds
96
+ optimize : isProduction ,
97
+ babelConfig : isTranspiledBuild ? es5BundleBabelConfig : undefined
98
+ } )
91
99
}
92
100
] ;
93
- }
94
-
95
- if ( isPlaygroundBuild ) {
101
+ } else if ( isPlaygroundBuild ) {
96
102
builds = [
97
103
{
98
- input : 'lib /init.ts ' ,
104
+ input : 'tsc-out /init.js ' ,
99
105
output : {
100
106
name : libName ,
101
107
file : 'dist/playground.cjs' ,
102
108
format : 'cjs' ,
103
109
exports : 'named' ,
104
110
sourcemap : true
105
111
} ,
106
- plugins
112
+ plugins : withPlugins ( {
113
+ debugBuild : true
114
+ } )
107
115
}
108
116
] ;
117
+ } else {
118
+ // Production / production-like builds
119
+
120
+ // - an ES2020 CJS bundle for "main"
121
+ // - an ES2020 ESM bundle for "module"
122
+ // Note that all dependencies are marked as external and won't be included in
123
+ // these bundles.
124
+ const modernBuildDef = {
125
+ input,
126
+ external,
127
+ output : [
128
+ // ESM bundle
129
+ outputEntry ( pkg . module , 'es' ) ,
130
+ // CJS bundle.
131
+ // Note that because package.json specifies "type":"module", the name of
132
+ // this file MUST end in ".cjs" in order to be treated as a CommonJS file.
133
+ outputEntry ( pkg . main , 'cjs' )
134
+ ] ,
135
+ plugins : withPlugins ( {
136
+ debugBuild : ! isProduction ,
137
+ optimize : isProduction
138
+ // Here is where we could insert the JSBI -> native BigInt plugin if we
139
+ // could find a way to provide a separate bundle for modern browsers
140
+ // that can use native BigInt.
141
+ // Maybe use node's exports + a user-defined condition?
142
+ // https://nodejs.org/api/packages.html#resolving-user-conditions
143
+ } )
144
+ } ;
145
+ // A legacy build that
146
+ // - bundles all our dependencies (big-integer) into this file
147
+ // - transpiles down to ES5
148
+ const legacyUMDBuildDef = {
149
+ input,
150
+ // UMD bundle for using in script tags, etc
151
+ // Note that some build systems don't like reading UMD files if they end in
152
+ // '.cjs', so this entry in package.json should end in a .js file extension.
153
+ output : [ outputEntry ( pkg . browser , 'umd' ) ] ,
154
+ plugins : withPlugins ( {
155
+ debugBuild : ! isProduction ,
156
+ optimize : isProduction ,
157
+ babelConfig : es5BundleBabelConfig
158
+ } )
159
+ } ;
160
+ builds = [ modernBuildDef , legacyUMDBuildDef ] ;
109
161
}
110
162
111
163
export default builds ;
0 commit comments