11#!/usr/bin/env node
22"use strict" ;
33/* eslint-disable @sinonjs/no-prototype-methods/no-prototype-methods */
4- const fs = require ( "fs" ) ;
5- const browserify = require ( "browserify" ) ;
4+ const fs = require ( "node:fs" ) ;
5+ const esbuild = require ( "esbuild" ) ;
6+ const { umdWrapper } = require ( "esbuild-plugin-umd-wrapper" ) ;
67const pkg = require ( "./package.json" ) ;
78const sinon = require ( "./lib/sinon" ) ;
89
@@ -23,63 +24,78 @@ try {
2324 * @param config
2425 * @param done
2526 */
26- function makeBundle ( entryPoint , config , done ) {
27- browserify ( entryPoint , config )
28- . exclude ( "timers" )
29- . exclude ( "timers/promises" )
30- . bundle ( function ( err , buffer ) {
31- if ( err ) {
32- throw err ;
33- }
34- done ( buffer . toString ( ) ) ;
35- } ) ;
27+ async function makeBundle ( entryPoint , config , done ) {
28+ const plugins = config . standalone
29+ ? [ umdWrapper ( { libraryName : config . standalone } ) ]
30+ : [ ] ;
31+
32+ const context = await esbuild . context ( {
33+ absWorkingDir : process . cwd ( ) ,
34+ banner : {
35+ js : preamble ,
36+ } ,
37+ bundle : true ,
38+ color : true ,
39+ define : { "process.env.NODE_DEBUG" : '""' } ,
40+ entryPoints : [ entryPoint ] ,
41+ external : [ "timers" , "timers/promises" , "fs" ] ,
42+ format : config . format ,
43+ minify : false ,
44+ platform : config . platform || "browser" ,
45+ plugins,
46+ sourcemap : config . debug === true ? "inline" : false ,
47+ // target: "es2022",
48+ write : false ,
49+ } ) ;
50+
51+ const { outputFiles } = await context . rebuild ( ) ;
52+ const js = outputFiles [ 0 ] . text ;
53+
54+ context . dispose ( ) ;
55+
56+ done ( js ) ;
3657}
3758
3859makeBundle (
3960 "./lib/sinon.js" ,
4061 {
4162 // Add inline source maps to the default bundle
4263 debug : true ,
64+ format : "cjs" ,
4365 // Create a UMD wrapper and install the "sinon" global:
4466 standalone : "sinon" ,
45- // Do not detect and insert globals:
46- detectGlobals : false ,
4767 } ,
4868 function ( bundle ) {
49- var script = preamble + bundle ;
50- fs . writeFileSync ( "pkg/sinon.js" , script ) ; // WebWorker can only load js files
69+ fs . writeFileSync ( "pkg/sinon.js" , bundle ) ; // WebWorker can only load js files
5170 } ,
5271) ;
5372
5473makeBundle (
5574 "./lib/sinon.js" ,
5675 {
76+ format : "cjs" ,
5777 // Create a UMD wrapper and install the "sinon" global:
5878 standalone : "sinon" ,
59- // Do not detect and insert globals:
60- detectGlobals : false ,
6179 } ,
6280 function ( bundle ) {
63- var script = preamble + bundle ;
64- fs . writeFileSync ( "pkg/sinon-no-sourcemaps.cjs" , script ) ;
81+ fs . writeFileSync ( "pkg/sinon-no-sourcemaps.cjs" , bundle ) ;
6582 } ,
6683) ;
6784
6885makeBundle (
6986 "./lib/sinon-esm.js" ,
7087 {
71- // Do not detect and insert globals:
72- detectGlobals : false ,
88+ format : "esm" ,
7389 } ,
7490 function ( bundle ) {
7591 var intro = "let sinon;" ;
76- var outro = `\nexport default sinon;\ n${ Object . keys ( sinon )
92+ var outro = `\n${ Object . keys ( sinon )
7793 . map ( function ( key ) {
78- return `const _${ key } = sinon .${ key } ;\nexport { _${ key } as ${ key } };` ;
94+ return `const _${ key } = require_sinon() .${ key } ;\nexport { _${ key } as ${ key } };` ;
7995 } )
8096 . join ( "\n" ) } `;
8197
82- var script = preamble + intro + bundle + outro ;
98+ var script = intro + bundle + outro ;
8399 fs . writeFileSync ( "pkg/sinon-esm.js" , script ) ;
84100 } ,
85101) ;
0 commit comments