erikdesjardins / zip-webpack-plugin

Webpack plugin to zip up emitted files.
https://www.npmjs.com/package/zip-webpack-plugin
MIT License
106 stars 21 forks source link

ReplaceInFileWebpackPlugin not working with zip-webpack-plugin #33

Open cookiejest opened 5 years ago

cookiejest commented 5 years ago

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'

  })
joe307bad commented 5 years ago

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.

Tsury commented 4 years ago

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

Tsury commented 4 years ago

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.

Karmalakas commented 3 years ago

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 🤔

JesseDahl commented 4 months ago

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