🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

content-replacer-loader

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

content-replacer-loader

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.

1.0.1
latest
Source
npm
Version published
Maintainers
0
Created
Source

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.

Keywords

webpack

FAQs

Package last updated on 27 Jul 2024

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