All Projects → igoradamenko → esbuild-plugin-alias

igoradamenko / esbuild-plugin-alias

Licence: MIT License
esbuild plugin for path aliases

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to esbuild-plugin-alias

esbuild-plugin-svgr
A plugin for esbuild that enables importing *.svg files as React components.
Stars: ✭ 27 (+35%)
Mutual labels:  esbuild, esbuild-plugin
esbuild-plugin-import-glob
A esbuild plugin which allows to import multiple files using the glob syntax.
Stars: ✭ 28 (+40%)
Mutual labels:  esbuild, esbuild-plugin
esbuild-css-modules-plugin
A esbuild plugin to bundle css modules into js(x)/ts(x)
Stars: ✭ 64 (+220%)
Mutual labels:  esbuild, esbuild-plugin
build-plugin
Track your build performances like never before.
Stars: ✭ 45 (+125%)
Mutual labels:  esbuild, esbuild-plugin
atbuild
Use JavaScript to generate JavaScript
Stars: ✭ 26 (+30%)
Mutual labels:  esbuild
nanobundle
Yet another build tool for libraries, powered by esbuild
Stars: ✭ 45 (+125%)
Mutual labels:  esbuild
codetree
⚡️ Lightning fast online code playground, built on top of webAssembly 🔥.
Stars: ✭ 55 (+175%)
Mutual labels:  esbuild
rescript-react-boilerplate
An opinionated app shell for ReScript & React progressive web apps
Stars: ✭ 62 (+210%)
Mutual labels:  esbuild
rc-dynamic
React DnD component tree --- React 动态组件树 Demo
Stars: ✭ 14 (-30%)
Mutual labels:  esbuild
electron-vite-boilerplate
📚 A Electron + Vite boilerplate of the nature of learning(source-code of vite-plugin-electron) / 学习性的样板工程(vite-plugin-electron源码)
Stars: ✭ 157 (+685%)
Mutual labels:  esbuild
typescript-boilerplate
A modern TypeScript project setup, for Node.js and browsers (using esbuild).
Stars: ✭ 502 (+2410%)
Mutual labels:  esbuild
sapper-typescript-esbuild-template
Sapper template with ESBuild and TypeScript
Stars: ✭ 18 (-10%)
Mutual labels:  esbuild
unplugin-vue
✨ Transform Vue 3 SFC to JavaScript. Supports Vite, esbuild, Rollup and Webpack.
Stars: ✭ 38 (+90%)
Mutual labels:  esbuild
esbuild-plugin-purescript
esbuild integration for PureScript
Stars: ✭ 27 (+35%)
Mutual labels:  esbuild
esbuild-svelte
An esbuild plugin to compile Svelte components
Stars: ✭ 163 (+715%)
Mutual labels:  esbuild
sunder-worker-template
Template for Sunder in Cloudflare worker with Typescript, ESBuild and Jest
Stars: ✭ 44 (+120%)
Mutual labels:  esbuild
tangerine-monorepo
A "fast" TypeScript-based Node.js monorepo setup powered by esbuild & turborepo
Stars: ✭ 191 (+855%)
Mutual labels:  esbuild
aegir
AEgir - Automated JavaScript project building
Stars: ✭ 73 (+265%)
Mutual labels:  esbuild
esbuild-rails
Esbuild Rails plugin
Stars: ✭ 102 (+410%)
Mutual labels:  esbuild
tutorials
This repository contains all the code snippets from articles and videos
Stars: ✭ 33 (+65%)
Mutual labels:  esbuild

esbuild-plugin-alias

npm

esbuild plugin for path aliases.

Rationale

Sometimes it's useful to have dynamic imports that resolves into different files depending on some conditions (e.g. env variables).

Installation

npm install --save-dev esbuild-plugin-alias

Usage

Define plugin in the plugins section of esbuild config like this:

const esbuild = require('esbuild');
const alias = require('esbuild-plugin-alias');

esbuild.build({
  // ...
  plugins: [
    alias({
      'imported-path': '/home/user/lib/src/resolved-path',
    }),
  ],
})

Note: esbuild requires resolved paths to be absolute. So, make sure that values in plugin's config object are absolute paths.

If you need to find a path to an installed dep, you may use require.resolve. E.g.:

alias({
  'react-dom': process.env.NODE_ENV === 'dev' 
    ? require.resolve('@hot-loader/react-dom')
    : require.resolve('react-dom'),
}),

Example

Having this input file:

// src/app.js
import settings from 'settings.env';

console.log(settings);

And esbuild config like this:

// config/build.js
const path = require('path');
const esbuild = require('esbuild');
const alias = require('esbuild-plugin-alias');

esbuild.build({
  entryPoints: ['in.js'],
  bundle: true,
  outfile: 'out.js',
  plugins: [
    alias({
      'settings.env': path.resolve(__dirname, `../src/settings.${process.env.NODE_ENV}.js`),
    }),
  ],
}).catch(err => process.exit(1));

You will get src/settings.dev.js loaded instead of settings.env when NODE_ENV equals dev.

Check test/ for more detailed example.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].