* Transpile every `.js` file under `src/` to CommonJS in `outDir`, copy * non-JS assets, then drop the `dist/cjs/package.json` type marker and * the same `module.exports = exports.default` post-build append the * `build:cjs` npm script writes. The test then `require()`s the result. * @param {str
(outDir)
| 29 | * @param {string} outDir target directory |
| 30 | */ |
| 31 | async function buildCjsBundle(outDir) { |
| 32 | for (const entry of readdirSync(srcDir, { withFileTypes: true })) { |
| 33 | const source = path.join(srcDir, entry.name); |
| 34 | const target = path.join(outDir, entry.name); |
| 35 | |
| 36 | if (entry.name.endsWith(".js")) { |
| 37 | const result = await transformFileAsync(source, { envName: "cjs" }); |
| 38 | |
| 39 | writeFileSync(target, result.code); |
| 40 | } else { |
| 41 | copyFileSync(source, target); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | writeFileSync( |
| 46 | path.join(outDir, "package.json"), |
| 47 | `${JSON.stringify({ type: "commonjs" })}\n`, |
| 48 | ); |
| 49 | |
| 50 | const indexPath = path.join(outDir, "index.js"); |
| 51 | |
| 52 | writeFileSync( |
| 53 | indexPath, |
| 54 | `${readFileSync(indexPath, "utf8")}module.exports = exports.default;\nmodule.exports.default = exports.default;\n`, |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | describe("cjs", () => { |
| 59 | let cjsDir; |
no outgoing calls
no test coverage detected
searching dependent graphs…