11import commonjs from '@rollup/plugin-commonjs' ;
2- import resolve from '@rollup/plugin-node-resolve' ;
2+ import { nodeResolve } from '@rollup/plugin-node-resolve' ;
33import babel from '@rollup/plugin-babel' ;
44import replace from '@rollup/plugin-replace' ;
5- import typescript from '@rollup/plugin-typescript' ;
65import { terser } from 'rollup-plugin-terser' ;
76import { env } from 'process' ;
87import pkg from './package.json' ;
98
109const 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 ;
1313const libName = 'temporal' ;
1414
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
3044 }
31- ]
32- ]
33- } ) ,
34- isProduction && terser ( )
35- ] . filter ( Boolean ) ;
45+ } )
46+ ) ;
47+ }
48+ return basePlugins ;
49+ }
3650
37- const input = 'lib /index.ts ' ;
51+ const input = 'tsc-out /index.js ' ;
3852
3953const external = [
4054 // Some dependencies (e.g. es-abstract) are imported using sub-paths, so the
@@ -53,59 +67,97 @@ function outputEntry(file, format) {
5367 } ;
5468}
5569
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 = [ ] ;
7983
80- if ( isTest262 ) {
84+ if ( isTest262Build ) {
8185 builds = [
8286 {
83- input : 'lib /init.ts ' ,
87+ input : 'tsc-out /init.js ' ,
8488 output : {
8589 name : libName ,
8690 file : 'dist/script.js' ,
8791 format : 'iife' ,
8892 sourcemap : true
8993 } ,
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+ } )
9199 }
92100 ] ;
93- }
94-
95- if ( isPlaygroundBuild ) {
101+ } else if ( isPlaygroundBuild ) {
96102 builds = [
97103 {
98- input : 'lib /init.ts ' ,
104+ input : 'tsc-out /init.js ' ,
99105 output : {
100106 name : libName ,
101107 file : 'dist/playground.cjs' ,
102108 format : 'cjs' ,
103109 exports : 'named' ,
104110 sourcemap : true
105111 } ,
106- plugins
112+ plugins : withPlugins ( {
113+ debugBuild : true
114+ } )
107115 }
108116 ] ;
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 ] ;
109161}
110162
111163export default builds ;
0 commit comments