All files / src/web-fs-loader index.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
100% Lines 7/7

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                1x 24x         15x 15x 11x   4x               12x          
import type { InputPluginOption } from "@rollup/browser";
 
/**
 * Adds a plugin that handles very last check to see if a file exists and that it can be loaded.
 * This plugin also provides a web based filesystem that can be used by all other plugins in this package.
 * This plugin must be declared FIRST in the rollup config. This plugin is ideally injected at runtime by
 * the IDE. If the desired filesystem does not exist, it will fall back to the available node filesystem.
 */
export default (): InputPluginOption => {
  return {
    name: "web-fs-loader",
    resolveId: {
      order: "post",
      async handler(importee, importer) {
        try {
          await this.fs.stat(importee);
          return importee;
        } catch (_) {}
        throw new Error(
          `Could not resolve import "${importee}" from "${importer}"`,
        );
      },
    },
    load: {
      order: "post",
      handler(id) {
        return this.fs.readFile(id, { encoding: "utf8" });
      },
    },
  };
};