1.0.1 • Published 12 months ago

content-replacer-loader v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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.