MCPcopy Create free account
hub / github.com/webpack/less-loader / buildCjsBundle

Function buildCjsBundle

test/cjs.test.js:31–56  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

29 * @param {string} outDir target directory
30 */
31async 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
58describe("cjs", () => {
59 let cjsDir;

Callers 1

cjs.test.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…