npm.io
2.0.0 • Published 4h ago

esbuild-plugin-replace

Licence
MIT
Version
2.0.0
Deps
1
Size
9 kB
Vulns
0
Weekly
0
Stars
24

esbuild-plugin-replace

An esbuild plugin for replacing strings in files while bundling.

Based on @rollup/plugin-replace

CI npm license

Documentation | GitHub

Install

npm install esbuild-plugin-replace --save-dev

Quick Start

import { build } from "esbuild";
import { replace } from "esbuild-plugin-replace";

await build({
  entryPoints: ["src/index.js"],
  bundle: true,
  plugins: [
    replace({
      __VERSION__: JSON.stringify("1.0.0"),
      __AUTHOR__: JSON.stringify("naecoo"),
    }),
  ],
});

Options

Replacement Values

Specify replacements directly:

replace({
  __VERSION__: JSON.stringify("1.0.0"),
});

Or use the values option:

replace({
  values: {
    __VERSION__: JSON.stringify("1.0.0"),
  },
});

Values can also be functions:

replace({
  __BUILD_TIME__: () => JSON.stringify(Date.now()),
});
Filtering
  • include - RegExp to match files to process (default: /.*/)
  • exclude - RegExp to match files to skip
replace({
  include: /\.js$/,
  exclude: /node_modules/,
  values: { __VERSION__: '"1.0.0"' },
});
Delimiters

Custom delimiters for matching (default: word boundaries \b):

replace({
  delimiters: ["", ""],
  "@/src/": "./src/",
});

TypeScript

Full TypeScript support is included. Types are auto-generated.

import { replace, ReplaceOptions } from "esbuild-plugin-replace";

const options: ReplaceOptions = {
  __VERSION__: JSON.stringify("1.0.0"),
};

replace(options);

License

MIT

Keywords