1.0.6 • Published 4 years ago
replace-attribute-loader v1.0.6
Table of Contents
Install with npm:
npm install --save-dev replace-attribute-loaderInstall with yarn:
yarn add replace-attribute-loader --devreplace-attribute-loader enables you to replace attributes from your source code. For example when you have test attributes that you do not want in a production build. This loader can be configured to replace one or more attributes, optionally with a new value.
TL;DR
- Replace attributes
 - Optional replacement values
 - Easy configuration
 
Open the webpack configuration file in which you have specified the loaders. Add the loader wherever you like. Example 1:
module.exports = {
    ...,
    module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: [
              {
                loader: 'replace-attribute-loader',
                options: {
                  attribute: 'data-test-hook',
                },
              },
            ],
          },
        ]
    }
};Options can be specified in multiple ways. Example 1: removes all occurrences of 'data-test-hook'
module.exports = {
    ...,
    module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: [
              {
                loader: 'replace-attribute-loader',
                options: {
                  attribute: 'data-test-hook',
                },
              },
            ],
          },
        ]
    }
};Example 2: removes all occurrences of 'data-test-hook' and 'data-test'
module.exports = {
    ...,
    module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: [
              {
                loader: 'replace-attribute-loader',
                options: {
                  attribute: ['data-test-hook', 'data-test'],
                },
              },
            ],
          },
        ]
    }
};Example 3: replaces all occurrences of 'data-test-hook' and 'data-test' with the specified value
module.exports = {
    ...,
    module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: [
              {
                loader: 'replace-attribute-loader',
                options: {
                  attribute: {
                    'data-test-hook': 'data-foo="bar"',
                    'data-test': 'data-foo="bar"',
                  },
                },
              },
            ],
          },
        ]
    }
};