index.js
function regex(replacements) {
    return {
        name: 'regex',

        renderChunk(code) {
            const magicString = new MagicString(code);
            let hasReplacements = false;

            replacements.forEach(replacement => {
                let [find, replace = ''] = replacement;
                if (typeof find === 'string') find = new RegExp(find);
                if (!find.global) find = new RegExp(find.source, 'g' + String(find).split('/').pop());

                let match, start, end;
                while ((match = find.exec(code))) {
                    hasReplacements = true;

                    start = match.index;
                    end = start + match[0].length;

                    magicString.overwrite(start, end, typeof replace === 'function' ? replace.apply(null, match) || '' : replace.replace(/\$(\d+)/, (str, index) => match[index]));
                }
            });

            if (!hasReplacements) return null;

            return {
                code: magicString.toString(),
                map: magicString.generateMap({ hires: true })
            };
        }
    };
}
function regex(replacements) {
    return {
        name: 'regex',

        renderChunk(code) {
            const magicString = new MagicString(code);
            let hasReplacements = false;

            replacements.forEach(replacement => {
                let [find, replace = ''] = replacement;
                if (typeof find === 'string') find = new RegExp(find);
                if (!find.global) find = new RegExp(find.source, 'g' + String(find).split('/').pop());

                let match, start, end;
                while ((match = find.exec(code))) {
                    hasReplacements = true;

                    start = match.index;
                    end = start + match[0].length;

                    magicString.overwrite(start, end, typeof replace === 'function' ? replace.apply(null, match) || '' : replace.replace(/\$(\d+)/, (str, index) => match[index]));
                }
            });

            if (!hasReplacements) return null;

            return {
                code: magicString.toString(),
                map: magicString.generateMap({ hires: true })
            };
        }
    };
}
function regex ( replacements ) { return { name : 'regex' , renderChunk ( code ) { const magicString = new MagicString ( code ) ; let hasReplacements = false ; replacements . forEach ( replacement => { let [ find , replace = '' ] = replacement ; if ( typeof find === 'string' ) find = new RegExp ( find ) ; if ( ! find . global ) find = new RegExp ( find . source , 'g' + String ( find ) . split ( '/' ) . pop ( ) ) ; let match , start , end ; while ( ( match = find . exec ( code ) ) ) { hasReplacements = true ; start = match . index ; end = start + match [ 0 ] . length ; magicString . overwrite ( start , end , typeof replace === 'function' ? replace . apply ( null , match ) || '' : replace . replace ( / \$(\d+) / , ( str , index ) => match [ index ] ) ) ; } } ) ; if ( ! hasReplacements ) return null ; return { code : magicString . toString ( ) , map : magicString . generateMap ( { hires : true } ) } ; } } ; }
regex replace after tree shaking
regex replace after tree shaking