Skip to content

Commit f04f3eb

Browse files
Fix issue 2618 - Remove browserify in favor of esbuild (#2661)
* change build tool in readme * add esbuild plugin, remove babelify and browserify * change build script to use esbuild * change imports for test * change imports and docs and pageerror * change config for esm * change esm source back to cjs
1 parent 48b69df commit f04f3eb

File tree

7 files changed

+69
-1328
lines changed

7 files changed

+69
-1328
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Note that in dev mode tests run only in Node. Before creating your PR please ens
145145

146146
### Compiling a built version
147147

148-
Build requires Node. Under the hood [Browserify](http://browserify.org/) is used.
148+
Build requires Node. Under the hood [esbuild](https://esbuild.github.io/) is used.
149149

150150
To build run
151151

build.cjs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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");
67
const pkg = require("./package.json");
78
const 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

3859
makeBundle(
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

5473
makeBundle(
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

6885
makeBundle(
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
);

lib/sinon-esm.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
"use strict";
2-
// eslint-disable-next-line no-undef
3-
sinon = require("./sinon");
2+
3+
const sinon = require("./sinon.js");
4+
5+
module.exports = sinon;

0 commit comments

Comments
 (0)