New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

esbuild-plugin-text-replace

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esbuild-plugin-text-replace

Replace Text with Regex in files before bundling.

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104K
increased by1.15%
Maintainers
1
Weekly downloads
 
Created
Source

esbuild-plugin-text-replace

Replace content before bundling with support for Filefilter, Namespace and Regex.

Install

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

Hint: Node >=10.1.0 for fs.promise

Usage

as a plugin

import esbuild from 'esbuild'
import textReplace from 'esbuild-plugin-text-replace'

await esbuild.build(
    {
        entryPoints: ['./test-build-input'],
        outfile: 'test-build-out.js',
        plugins: [ 
            textReplace(
                {
                    include: /mypackage\/dist\/loader\.js$/,
                    pattern:[
                        ['const installRetry','let installRetry'],
                        [/const\s+{\s*textReplace\s*}\s*=\s*require\s*\(\s*'esbuild-plugin-text-replace'\s*\)\s*;/g , "'import textReplace from 'esbuild-plugin-text-replace'"]
                    ]
                }
            )
        ],
    }
)

as part of a pipe

import esbuild from 'esbuild';
import pipe from 'esbuild-plugin-pipe';
import textReplace from 'esbuild-plugin-text-replace';

await esbuild.build(
    {
        entryPoints: ['./test-build-input'],
        outfile: 'test-build-out.js',
        bundle: true,
        plugins: [
            pipe({
                filter: /.*/,
                namespace: '',
                plugins: [
                    textReplace(
                        {
                            include: /mypackage\/dist\/loader\.js$/,
                            pattern:[
                                ['const installRetry','let installRetry'],
                                [/const\s+{\s*textReplace\s*}\s*=\s*require\s*\(\s*'esbuild-plugin-text-replace'\s*\)\s*;/g , "'import textReplace from 'esbuild-plugin-text-replace'"]
                            ]
                        }
                    )
                ]
            })
        ]
    }
)

Options

include

Filter filepath by regex.

Type: RegExp Default: /.*/

Note: Try to never use the default value as this is has a huge impact on speed if all files are matched!

namespace

Type: String Default: all

More info about esbuild namespaces

pattern

Search with Text or Regex and replace the found content with a string.

Type: Array Default: []

All information about the replaceAll regex Options and replacer functions.

Examples:

[
    // transform 2020-10-02 to 02.10.2020
    [/(\d{4})-(\d{2})-(\d{2})/g , (match,p1,p2,p3,offset,wholeString)=>`${p3}.${p2}.${p1}`], 
    ['__buildVersion' , '"1.1.1"'],
    [/(\s*)const(\s+a\s*=\s*1[\s;\n])/g, '$1let$2']
]

Note: /g for globale replacement is a must requirement

History

  • 1.3.0 pipe mode only: Use the regex from parameter include to transform only files which match
  • 1.2.0 Add esbuild pipe support
  • 1.1.3 Initial relase

Roadmap

  • tests
  • speed tests

Contribution

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Built With

License

Distributed under the "bsd-2-clause" License. See LICENSE.txt for more information.

Keywords

FAQs

Package last updated on 26 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc