All files / processes/lib loaderjsGenerator.js

33.33% Statements 3/9
100% Branches 0/0
33.33% Functions 1/3
33.33% Lines 3/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49        1x 1x                                               1x                                      
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
export function generateLoaderJs(filename, suffix) {
  // read contents of loader.js.template.template as string
  const loaderJsTemplate = fs.readFileSync(`${__dirname}/loader.js.template`, {
    encoding: "utf-8",
  });
  // replace the correct placeholder with the actual filename
  const loaderJsReplaced = loaderJsTemplate.replaceAll(
    "{{path}}",
    `../${filename}`
  );
  // write script to the dist folder as loader.js.template
  fs.mkdirSync(`./dist/${suffix}`, { recursive: true })
  fs.writeFileSync(
    path.resolve(`./dist/${suffix}/loader.js`),
    loaderJsReplaced,
    {
      encoding: "utf-8",
    }
  );
}
 
export function compatRootLoaderSource(template, nestedLoaderPath) {
  return template
    .replaceAll("{{path}}", nestedLoaderPath)
    .replaceAll("./../wrapper.js", "./wrapper.js");
}
 
export function generateSingleLoaderJs(loaderPath) {
  const loaderJsTemplate = fs.readFileSync(`${__dirname}/loader.js.template`, {
    encoding: "utf-8",
  });
 
  fs.writeFileSync(
    path.resolve("./dist/loader.js"),
    compatRootLoaderSource(loaderJsTemplate, loaderPath),
    {
      encoding: "utf-8",
    }
  );
}