GitHub - whaaaley/esbuild-plugin-glob-import: Use globs to import multiple files. (original) (raw)

esbuild-plugin-glob-import

Use globs to import multiple files.

Install

$ npm i esbuild-plugin-glob-import

Setup

Add the plugin to your esbuild options.

import esbuild from 'esbuild' import globImport from 'esbuild-plugin-glob-import'

esbuild.build({ // ... plugins: [ globImport() ] })

Options

Example with default options

Options:

const opts = { camelCase: true, entryPoint: 'index.js', entryPointMatch: arr => arr[arr.length - 1] === opts.entryPoint }

File structure:

/pages
  /home
    home.css
    index.js
  /about
    /content
      summary.js
      history.js
    about.css
    index.js
  /error
    error.css
    index.js

Glob import statement:

import pages from './pages/**/index.js' console.log(pages)

Output:

{ home: function () { // ... }, about: function () { // ... }, error: function () { // ... } }

Alternative Example

Using the same file structure and import statement from the previous example but with different options.

Options:

const opts = { camelCase: false, entryPoint: false, entryPointMatch: null }

Output:

{ home: { 'index.js': function () { // ... } }, about: { 'index.js': function () { // ... }, content { 'summary.js': function () { // ... }, 'history.js': function () { // ... } } }, error: { 'index.js': function () { // ... } } }

Prior Art