|
2 | 2 | "use strict"; |
3 | 3 | /* eslint-disable @sinonjs/no-prototype-methods/no-prototype-methods */ |
4 | 4 | const fs = require("node:fs"); |
| 5 | +const { execFileSync } = require("node:child_process"); |
5 | 6 | const esbuild = require("esbuild"); |
6 | 7 | const { umdWrapper } = require("esbuild-plugin-umd-wrapper"); |
7 | 8 | const pkg = require("./package.json"); |
| 9 | + |
| 10 | +// Step 1: Run Rollup to generate lib/ from src/ |
| 11 | +console.log("Generating lib/ from src/ using Rollup..."); |
| 12 | +execFileSync("npx", ["rollup", "-c", "rollup.config.mjs"], { |
| 13 | + stdio: "inherit", |
| 14 | +}); |
| 15 | + |
| 16 | +// Step 1b: Mark the generated lib tree as CommonJS for Node. |
| 17 | +fs.writeFileSync( |
| 18 | + "lib/package.json", |
| 19 | + JSON.stringify({ type: "commonjs" }, null, 2), |
| 20 | +); |
| 21 | + |
| 22 | +// Step 2: Load sinon from the generated lib |
8 | 23 | const sinon = require("./lib/sinon"); |
9 | 24 |
|
10 | 25 | // YYYY-MM-DD |
@@ -56,46 +71,60 @@ async function makeBundle(entryPoint, config, done) { |
56 | 71 | done(js); |
57 | 72 | } |
58 | 73 |
|
59 | | -makeBundle( |
60 | | - "./lib/sinon.js", |
61 | | - { |
62 | | - // Add inline source maps to the default bundle |
63 | | - debug: true, |
64 | | - format: "cjs", |
65 | | - // Create a UMD wrapper and install the "sinon" global: |
66 | | - standalone: "sinon", |
67 | | - }, |
68 | | - function (bundle) { |
69 | | - fs.writeFileSync("pkg/sinon.js", bundle); // WebWorker can only load js files |
70 | | - }, |
71 | | -); |
| 74 | +async function buildAll() { |
| 75 | + await makeBundle( |
| 76 | + "./lib/sinon.js", |
| 77 | + { |
| 78 | + // Add inline source maps to the default bundle |
| 79 | + debug: true, |
| 80 | + format: "cjs", |
| 81 | + // Create a UMD wrapper and install the "sinon" global: |
| 82 | + standalone: "sinon", |
| 83 | + }, |
| 84 | + function (bundle) { |
| 85 | + fs.writeFileSync("pkg/sinon.js", bundle); // WebWorker can only load js files |
| 86 | + }, |
| 87 | + ); |
72 | 88 |
|
73 | | -makeBundle( |
74 | | - "./lib/sinon.js", |
75 | | - { |
76 | | - format: "cjs", |
77 | | - // Create a UMD wrapper and install the "sinon" global: |
78 | | - standalone: "sinon", |
79 | | - }, |
80 | | - function (bundle) { |
81 | | - fs.writeFileSync("pkg/sinon-no-sourcemaps.cjs", bundle); |
82 | | - }, |
83 | | -); |
| 89 | + await makeBundle( |
| 90 | + "./lib/sinon.js", |
| 91 | + { |
| 92 | + format: "cjs", |
| 93 | + // Create a UMD wrapper and install the "sinon" global: |
| 94 | + standalone: "sinon", |
| 95 | + }, |
| 96 | + function (bundle) { |
| 97 | + fs.writeFileSync("pkg/sinon-no-sourcemaps.cjs", bundle); |
| 98 | + }, |
| 99 | + ); |
84 | 100 |
|
85 | | -makeBundle( |
86 | | - "./lib/sinon-esm.js", |
87 | | - { |
88 | | - format: "esm", |
89 | | - }, |
90 | | - function (bundle) { |
91 | | - var intro = "let sinon;"; |
92 | | - var outro = `\n${Object.keys(sinon) |
93 | | - .map(function (key) { |
94 | | - return `const _${key} = require_sinon().${key};\nexport { _${key} as ${key} };`; |
95 | | - }) |
96 | | - .join("\n")}`; |
97 | | - |
98 | | - var script = intro + bundle + outro; |
99 | | - fs.writeFileSync("pkg/sinon-esm.js", script); |
100 | | - }, |
101 | | -); |
| 101 | + await makeBundle( |
| 102 | + "./lib/sinon-esm.js", |
| 103 | + { |
| 104 | + format: "esm", |
| 105 | + }, |
| 106 | + function (bundle) { |
| 107 | + var intro = "let sinon;\n"; |
| 108 | + // Replace the bundle's own "export default" with a simple assignment to sinon |
| 109 | + var baseScript = bundle.replace( |
| 110 | + /export default [^;]+;/, |
| 111 | + "sinon = require_sinon_esm();\nif (sinon.default) sinon = sinon.default;", |
| 112 | + ); |
| 113 | + |
| 114 | + var outro = `\nexport default sinon;\n${Object.keys(sinon) |
| 115 | + .filter((key) => key !== "default") |
| 116 | + .map(function (key) { |
| 117 | + return `const _${key} = sinon.${key};\nexport { _${key} as ${key} };`; |
| 118 | + }) |
| 119 | + .join("\n")}`; |
| 120 | + |
| 121 | + var script = intro + baseScript + outro; |
| 122 | + fs.writeFileSync("pkg/sinon-esm.js", script); |
| 123 | + }, |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +buildAll().catch((err) => { |
| 128 | + console.error(err); |
| 129 | + process.exit(1); |
| 130 | +}); |
0 commit comments