zip-webpack-plugin icon indicating copy to clipboard operation
zip-webpack-plugin copied to clipboard

ReplaceInFileWebpackPlugin not working with zip-webpack-plugin

Open cookiejest opened this issue 6 years ago • 5 comments

The files zipped before file replace occurs:

s: [filename ], rules: [{ search: 'VERSIONNUMBER', replace: version }, { search: 'MASHUPNAME', replace: mashupname }, { search: 'BUILDDESCRIPTION', replace: description } ] }]), new CopyWebpackPlugin( [{ from: '/', to: mashupname }], { copyUnmodified: true } ), new WebpackAutoInject({ // options // example: components: { AutoIncreaseVersion: false } }), new ZipPlugin({ pathPrefix: mashupname, // OPTIONAL: defaults to the Webpack output filename (above) or, // if not present, the basename of the path filename: mashupname + '' + version + '' + process.env.NODE_ENV + '.zip'

  })

cookiejest avatar Jan 15 '19 09:01 cookiejest

I am having the same issue. This is my webpack.prod.js:

module.exports = merge(common, {
    mode: 'production',
    plugins: [
        new CopyWebpackPlugin([
            {from: 'assets'}
        ]),
        new ReplaceInFileWebpackPlugin([{
            dir: 'dist',
            files: ['index.html', 'dantethomas.html', 'login.html'],
            rules: [{
                search: '../dist',
                replace: ''
            }]
        }]),
        new ZipPlugin({
            path: path.resolve(__dirname),
            filename: 'dist'
        })
    ]
});

The proper files are searched and replaced within dist, but when I unzip the resulting zip file, the files appear without the proper string replacements.

joe307bad avatar May 03 '19 19:05 joe307bad

Same issue. Every archiver plugin I find seems to be having some show stopping issue sadly...

Tsury avatar Jan 21 '20 11:01 Tsury

From what I've seen, there needs to be a setting to determine when the zipping happens. It needs to happen after Webpack's process it complete for the replaced data to be available.

For instance, WebPackShell plugin have this setting: onBuildExit: array of scripts to execute after webpack's process is complete.

Tsury avatar Jan 21 '20 14:01 Tsury

Same here. I'm totally new to webpack, but from what I found, it's the hooks that matter. So my guess it's either this plugin is executed on a not very last hook or replaceInFile plugin is executed too late 🤔

Karmalakas avatar Jun 18 '21 16:06 Karmalakas

I had similar issues with the ReplaceInFileWebpackPlugin and this plugin, and tried to rewrite it so that it ran in a different part of the cycle (before the zip plugin), but that was such a pain in the ass because the docs for the plugin api are terrible and there are a lot of gotchas. Just wasn't worth it to try to figure it all out.

If you're already using copywebpackplugin, you can use the transform property of their config to do string replacements

`

        new CopyWebpackPlugin({
            patterns: [
                {   
                    from: 'YOURFOLDER/**/*',
                    transform: {
                        transformer(content, absoluteFrom) {
                            var fileContent = content.toString();
                            var replacedFileContent = fileContent;

                            stringReplacements.forEach(rule => {
                                replacedFileContent = replacedFileContent.replace(rule.search, rule.replace);
                            });

                            return Buffer.from(replacedFileContent);
                        },
                    },
                },
            ]
        }),

`

stringReplacements is just an array of objects, each entry with a "search" and a "replace" property

JesseDahl avatar Apr 12 '24 23:04 JesseDahl