npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

content-replacer-loader

v1.0.1

Published

A NodeJS webpack loader to replace strings in specific file content or replace the file content before passing to its appropriate loader. Supports .js, .jsx, .ts, .tsx, .html, .htm, and .css.

Downloads

13

Readme

content-replacer-loader

content-replacer-loader is a NodeJS webpack loader designed to replace strings in any specific file content or replace the file content with another one before passing it to its appropriate loader. This loader currently supports .js, .jsx, .ts, .tsx, .html, .htm, and .css file extensions.

Features

  • Replace entire file content based on file path or extension.
  • Find and replace strings within the file content.
  • Supports case-insensitive path matching.
  • Options to perform replacements at the start, inclusion, or end of the file content.

Installation

To install content-replacer-loader, you need to use npm or yarn:

npm install content-replacer-loader --save-dev

or

yarn add content-replacer-loader --dev

Usage

To use content-replacer-loader in your webpack configuration, add it to the module.rules section of your webpack.config.js:

const path = require('path');

module.exports = {
  // Your other configurations...
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx|html|htm|css)$/,
        use: [
          {
            loader: 'content-replacer-loader',
            options: {
              // Your options here
            }
          }
        ]
      }
    ]
  }
};

Options

The loader supports options conforming to the IPathOptions interface. These options allow you to specify the content to be replaced or searched and replaced.

IPathOptions Interface

interface IPathOptions {
  entireContent?: {
    startsWith?: IEntireContent | IEntireContent[];
    includes?: IEntireContent | IEntireContent[];
    endsWith?: IEntireContent | IEntireContent[];
  };
  findAndReplace?: {
    startsWith?: IFindAndReplace | IFindAndReplace[];
    includes?: IFindAndReplace | IFindAndReplace[];
    endsWith?: IFindAndReplace | IFindAndReplace[];
  };
}

IEntireContent Interface

interface IEntireContent {
  pathOrExt: string;
  content: string;
  caseInSensitivePathMatch?: boolean;
}

IFindAndReplace Interface

interface IFindAndReplace {
  pathOrExt: string;
  find: { replace: string; find: string | RegExp }[];
  firstReplace?: boolean;
  caseInSensitivePathMatch?: boolean;
  caseInSensitive?: boolean;
}

Example Configuration

Here's an example configuration that demonstrates how to use the content-replacer-loader with the available options:

module.exports = {
  // Your other configurations...
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx|html|htm|css)$/,
        use: [
          {
            loader: 'content-replacer-loader',
            options: {
              entireContent: {
                startsWith: {
                  pathOrExt: '.html',
                  content: '<!DOCTYPE html>'
                },
                includes: [
                  {
                    pathOrExt: '.js',
                    content: 'use strict'
                  },
                  {
                    pathOrExt: '.css',
                    content: '@charset "UTF-8";'
                  }
                ],
                endsWith: {
                  pathOrExt: 'index.ts',
                  content: 'export {}'
                }
              },
              findAndReplace: {
                includes: [
                  {
                    pathOrExt: 'button.js',
                    find: [
                      { find: 'var', replace: 'let' },
                      { find: /console\.log\(/g, replace: 'debug.log(' }
                    ],
                    caseInSensitive: true
                  }
                ]
              }
            }
          }
        ]
      }
    ]
  }
};

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request with any improvements or bug fixes.

Support

If you encounter any issues or have any questions, feel free to open an issue on the project's GitHub page.

Acknowledgments

Thanks to the open-source community for the inspiration and support in developing this project.