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

replace-string-loader

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

replace-string-loader

Webpack loader for replacing a text and other data in a file

0.0.7
latest
Source
npm
Version published
Weekly downloads
977
-6.86%
Maintainers
1
Weekly downloads
 
Created
Source

Webpack loader for replacing a text and other data in a file

Instalation:

npm install --save-dev replace-string-loader

Options:

options: {
    search: "string or /regExp/",
    replace: "string or function",
    flags: "string",
    file: bool
}
search: a string or a regular expression that will be found and replaced in the file.  
replace: a string or a function for replacing. 
    If an options.replace is a function: this function recieves next arguments:
        match – a found match,
        p1, p2, ..., pn – string content (if exist),
        offset – position where the match was found,
        s – a basic string.
    The function must return a string!!!
flags: regExp flags (g, i, m). Used if options.search is a string and ignored if options.search is a regular expression.
file: if true - a found result will save to the file.

Usage:

Searching and replacing '105px' to '200px' in scss files: Result will be saved to a json file.

module: {
        rules: [{
            test: /\.scss$/,
            use[{
                    loader: 'replace-string-loader',
                    options: {
                        search: '105px',
                        replace: '200px',
                        flags: 'g',
                        file: true
                    }
                }]
        }]
    }

An output json file:

{
    "file": "[last-file-folder][file-name].[ext]",
    "fullFilePath": "full-path-to-file/[file-name].[ext]",
    "matches": [
        {
            "matchLine": 12,
            "matchContent": "min-height: 105px;",
            "replaceTo": "min-height: 200px;"
        },
        {
            "matchLine": 22,
            "matchContent": "min-height: 105px;",
            "replaceTo": "min-height: 200px;"
        }
    ]
}

or, without saving into the file

module: {
        rules: [{
            test: /\.scss$/,
            use[{
                    loader: 'replace-string-loader',
                    options: {
                        search: /105px/g,
                        replace: '200px',
                        file: false
                    }
                }]
        }]
    }

Searching and replacing 'exampleVarA' to 'exampleVarB' in js files:

module: {
        rules: [{
            test: /\.js$/,
            use[{
                    loader: 'replace-string-loader',
                    options: {
                        search: /exampleVarA/g,
                        replace: 'exampleVarB'
                    }
                }]
        }]
    }

Searching and replacing '105px' to '1050px' in css files:

module: {
        rules: [{
            test: /\.css$/,
            use[{
                    loader: 'replace-string-loader',
                    options: {
                        search: '105px',
                        replace: function(match){ return `${parseInt(match) * 10}px`;}
                    }
                }]
        }]
    }

Keywords

webpack

FAQs

Package last updated on 28 Mar 2017

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