async validate()

in scripts/compilation/Inliner.js [415:499]


  async validate() {
    if (this.bailout) {
      return this;
    }
    this.indexContents = fs.readFileSync(this.outfile, "utf-8");

    const externalsToCheck = new Set(
      Object.keys(this.variantMap)
        .filter((variant) => !this.transitiveVariants.includes(variant) && !variant.endsWith("index"))
        .map((variant) => path.basename(variant).replace(/.js$/, ""))
    );

    const inspect = (contents) => {
      for (const line of contents.split("\n")) {
        // we expect to see a line with require() and the variant external in it
        if (line.includes("require(")) {
          const checkOrder = [...externalsToCheck].sort().reverse();
          for (const external of checkOrder) {
            if (line.includes(external)) {
              if (this.verbose) {
                console.log("Inline index confirmed require() for variant external:", external);
              }
              externalsToCheck.delete(external);
            }
          }
        }
      }
    };

    inspect(this.indexContents);

    if (this.hasSubmodules) {
      const submodules = fs.readdirSync(path.join(path.dirname(this.outfile), "submodules"));
      for (const submodule of submodules) {
        const submoduleIndexContents = fs.readFileSync(
          path.join(path.dirname(this.outfile), "submodules", submodule, "index.js"),
          "utf-8"
        );
        inspect(submoduleIndexContents);
      }
    }

    if (externalsToCheck.size) {
      throw new Error(
        "require() statements for the following variant externals: " +
          [...externalsToCheck].join(", ") +
          " were not found in the index."
      );
    }

    // check ESM compat.
    const tmpFileContents = this.canonicalExports
      .filter((sym) => !sym.includes(":"))
      .map((sym) => `import { ${sym} } from "${this.pkgJson.name}";`)
      .join("\n");
    fs.writeFileSync(path.join(__dirname, "tmp", this.package + ".mjs"), tmpFileContents, "utf-8");
    await spawnProcess("node", [path.join(__dirname, "tmp", this.package + ".mjs")]);

    if (this.hasSubmodules) {
      const submodules = fs.readdirSync(path.join(root, this.subfolder, this.package, "src", "submodules"));
      for (const submodule of submodules) {
        const canonicalExports = Object.keys(
          require(path.join(root, this.subfolder, this.package, "dist-cjs", "submodules", submodule, "index.js"))
        );
        const tmpFileContents = canonicalExports
          .filter((sym) => !sym.includes(":"))
          .map((sym) => `import { ${sym} } from "${this.pkgJson.name}/${submodule}";`)
          .join("\n");
        const tmpFilePath = path.join(__dirname, "tmp", this.package + "_" + submodule + ".mjs");
        fs.writeFileSync(tmpFilePath, tmpFileContents, "utf-8");
        await spawnProcess("node", [tmpFilePath]);
        fs.rmSync(tmpFilePath);
        if (this.verbose) {
          console.log("ESM compatibility verified for submodule", submodule);
        }
      }
    }

    if (this.verbose) {
      console.log("ESM compatibility verified.");
    }
    fs.rmSync(path.join(__dirname, "tmp", this.package + ".mjs"));

    return this;
  }