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.19.8 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.7 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.6 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.5 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.4 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.3 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.2 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.1 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.19.0 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.18.8 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.18.7 lib/scripts/esbuild-plugin-import-glob.mjs
lookbook-3.0.0.alpha.2 lib/scripts/esbuild-plugin-import-glob.js
lookbook-3.0.0.alpha.1 lib/scripts/esbuild-plugin-import-glob.js
lookbook-3.0.0.alpha.0 lib/scripts/esbuild-plugin-import-glob.js
coveragebook_components-0.18.0 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.7 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.6 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.5 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.4 lib/scripts/esbuild-plugin-import-glob.mjs
coveragebook_components-0.17.3 lib/scripts/esbuild-plugin-import-glob.mjs