Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

esbuildr/esbuild-plugin-dynamic-import

Repository files navigation

esbuild-plugin-dynamic-import

Deprecated: This plugin is unnecessary now because it has been supported by esbuild natively from v0.19.0.


GitHub Actions type-coverage npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier changesets

Dynamic import variables support for esbuild

TOC

Usage

Install

# pnpm
pnpm add -D esbuild-plugin-dynamic-import

# yarn
yarn add -D esbuild-plugin-dynamic-import

# npm
npm i -D esbuild-plugin-dynamic-import

API

import { dynamicImport } from 'esbuild-plugin-dynamic-import'

esbuild.build({
  plugins: [
    dynamicImport({
      // filter: // optional, default: /\.([cm]?[jt]s|[jt]sx)$/
      // ignore: // optional, default: /\bnode_modules\b/
      // loader: // optional, default: adjust with path extension
    }),
  ],
})

Usage

const lazyImport = () => import(`./pages/${page}`)

// comments are allowed
const lazyImport = () =>
  import(
    /* comment1 */ // inline comment
    /**
     * multiline comment
     */
    `./pages/${
      page
      // inline comment
      /* comment2 */
    }`
  )

The expressions are matched with RegExp instead of parsing as AST for performance reason, so you should only use it for simple cases as following:

  1. relative path required
  2. template literal required
  3. no ) in the () pair
  4. extensions are optional

Additionally, the built-in functionality will be available in esbuild soon (maybe): evanw/esbuild#2508 (comment)

Sponsors

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Backers

Backers

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

MIT © JounQin@1stG.me