Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Note that in dev mode tests run only in Node. Before creating your PR please ens

### Compiling a built version

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

To build run

Expand Down
66 changes: 41 additions & 25 deletions build.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node
"use strict";
/* eslint-disable @sinonjs/no-prototype-methods/no-prototype-methods */
const fs = require("fs");
const browserify = require("browserify");
const fs = require("node:fs");
const esbuild = require("esbuild");
const { umdWrapper } = require("esbuild-plugin-umd-wrapper");
const pkg = require("./package.json");
const sinon = require("./lib/sinon");

Expand All @@ -23,63 +24,78 @@ try {
* @param config
* @param done
*/
function makeBundle(entryPoint, config, done) {
browserify(entryPoint, config)
.exclude("timers")
.exclude("timers/promises")
.bundle(function (err, buffer) {
if (err) {
throw err;
}
done(buffer.toString());
});
async function makeBundle(entryPoint, config, done) {
const plugins = config.standalone
? [umdWrapper({ libraryName: config.standalone })]
: [];

const context = await esbuild.context({
absWorkingDir: process.cwd(),
banner: {
js: preamble,
},
bundle: true,
color: true,
define: { "process.env.NODE_DEBUG": '""' },
entryPoints: [entryPoint],
external: ["timers", "timers/promises", "fs"],
format: config.format,
minify: false,
platform: config.platform || "browser",
plugins,
sourcemap: config.debug === true ? "inline" : false,
// target: "es2022",
write: false,
});

const { outputFiles } = await context.rebuild();
const js = outputFiles[0].text;

context.dispose();

done(js);
}

makeBundle(
"./lib/sinon.js",
{
// Add inline source maps to the default bundle
debug: true,
format: "cjs",
// Create a UMD wrapper and install the "sinon" global:
standalone: "sinon",
// Do not detect and insert globals:
detectGlobals: false,
},
function (bundle) {
var script = preamble + bundle;
fs.writeFileSync("pkg/sinon.js", script); // WebWorker can only load js files
fs.writeFileSync("pkg/sinon.js", bundle); // WebWorker can only load js files
},
);

makeBundle(
"./lib/sinon.js",
{
format: "cjs",
// Create a UMD wrapper and install the "sinon" global:
standalone: "sinon",
// Do not detect and insert globals:
detectGlobals: false,
},
function (bundle) {
var script = preamble + bundle;
fs.writeFileSync("pkg/sinon-no-sourcemaps.cjs", script);
fs.writeFileSync("pkg/sinon-no-sourcemaps.cjs", bundle);
},
);

makeBundle(
"./lib/sinon-esm.js",
{
// Do not detect and insert globals:
detectGlobals: false,
format: "esm",
},
function (bundle) {
var intro = "let sinon;";
var outro = `\nexport default sinon;\n${Object.keys(sinon)
var outro = `\n${Object.keys(sinon)
.map(function (key) {
return `const _${key} = sinon.${key};\nexport { _${key} as ${key} };`;
return `const _${key} = require_sinon().${key};\nexport { _${key} as ${key} };`;
})
.join("\n")}`;

var script = preamble + intro + bundle + outro;
var script = intro + bundle + outro;
fs.writeFileSync("pkg/sinon-esm.js", script);
},
);
6 changes: 4 additions & 2 deletions lib/sinon-esm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";
// eslint-disable-next-line no-undef
sinon = require("./sinon");

const sinon = require("./sinon.js");

module.exports = sinon;
Loading