esbuild-plugin-import-pattern

raw JSON →
0.1.0 verified Mon Apr 27 auth: no javascript

An esbuild plugin for importing modules using wildcard patterns (e.g. './files/*.js'). Current stable version is 0.1.0. It improves upon esbuild-plugin-import-glob by providing a simpler API and support for named import selection via hash syntax. The plugin integrates with esbuild's build process and is published on npm as a development dependency. Released under the MIT license, it is designed for Node.js environments and requires esbuild as a peer dependency.

error Error: The plugin "esbuild-plugin-import-pattern" is not found.
cause The plugin is not installed or not imported correctly.
fix
Run 'npm install esbuild-plugin-import-pattern --save-dev' and import as 'const { importPatternPlugin } = require("esbuild-plugin-import-pattern")'.
error Error: Wildcard pattern must contain at least one '*'
cause The import pattern does not include a wildcard character.
fix
Change the import path to include '*', e.g., './files/*.js'.
error TypeError: importPatternPlugin is not a function
cause The require or import incorrectly assigned the module.
fix
Use destructuring: const { importPatternPlugin } = require('esbuild-plugin-import-pattern') (not require('esbuild-plugin-import-pattern').default or direct assignment).
gotcha Wildcard patterns must contain at least one '*' character.
fix Ensure pattern includes a '*', e.g., './files/*.js'.
gotcha Default import from a wildcard module is not exported. You must use named imports like { entries }, { modules }, or { paths }.
fix Use named imports: import { entries } from './files/*.js'.
gotcha When using wildcard pattern with hash syntax for named exports, esbuild may still include unused exports. The hash only reduces bundling overhead but does not tree-shake unused exports.
fix Use hash syntax (e.g., '#default') if all exports share the same named exports to avoid unnecessary code.
gotcha If multiple wildcard imports exist in the same file, they must be renamed using 'as' keyword.
fix Use: import { entries as cats } from './cats/*.js' and import { entries as dogs } from './dogs/*.js'.
npm install esbuild-plugin-import-pattern
yarn add esbuild-plugin-import-pattern
pnpm add esbuild-plugin-import-pattern

Install the plugin and configure esbuild with it. The plugin is applied as a plugin during build, allowing wildcard imports in source files. The build script demonstrates bundling with the plugin enabled.

npm install esbuild-plugin-import-pattern --save-dev

const esbuild = require('esbuild');
const { importPatternPlugin } = require('esbuild-plugin-import-pattern');

esbuild.build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/bundle.js',
  plugins: [importPatternPlugin()],
}).then(() => {
  console.log('Build succeeded');
}).catch((err) => {
  console.error(err);
  process.exit(1);
});