Sha256: f564fd2081ea7beb710c22035f0322e092bcb68900703496c87d9c8f7e474756

Contents?: true

Size: 1.5 KB

Versions: 76

Compression:

Stored size: 1.5 KB

Contents

import fastGlob from "fast-glob";

export default function EsbuildPluginImportGlob({
  importMapper = (path) => path,
}) {
  return {
    name: "require-context",
    setup: (build) => {
      build.onResolve({ filter: /\*/ }, async ({ path, resolveDir }) => {
        if (resolveDir === "") {
          return; // Ignore unresolvable paths
        }

        return {
          path: `${resolveDir}|${path}`,
          namespace: "import-glob",
          pluginData: {
            path,
            resolveDir,
          },
        };
      });

      build.onLoad(
        { filter: /.*/, namespace: "import-glob" },
        async ({ pluginData: { resolveDir, path } }) => {
          const files = (
            await fastGlob(importMapper(path), {
              cwd: resolveDir,
            })
          ).sort();

          if (!files.length) {
            console.warn(
              `Warning: ${path} (in ${resolveDir}) did not match any files!`
            );
          }

          const importerCode = `
          ${files
            .map(
              (module, index) => `import * as module${index} from '${module}'`
            )
            .join(";")}

          const modules = [${files
            .map((_module, index) => `module${index}`)
            .join(",")}];

          export default modules;
          export const filenames = [${files
            .map((module) => `'${module}'`)
            .join(",")}]
        `;

          return { contents: importerCode, resolveDir };
        }
      );
    },
  };
}

Version data entries

76 entries across 76 versions & 2 rubygems

Version Path
coveragebook_components-0.17.2 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.1 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.0 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.16.0 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.15.0 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.14.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.13.1 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.13.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.12.2 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.12.1 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.12.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.11.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.10.1.beta.2 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.10.1.beta.1 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.10.1.beta.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.10.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.9.1 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.9.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.8.9 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.8.8 lib/scripts/esbuild-plugin-import-glob.js