webpack-encore icon indicating copy to clipboard operation
webpack-encore copied to clipboard

Improve CopyWebpackPlugin with WebpackManifestPlugin workflow

Open shadowc opened this issue 3 years ago • 1 comments

Let's improve on this PR to be able to use CopyWebpackPlugin AND add hashes to the destination files when needed: https://github.com/symfony/webpack-encore/pull/409

For reference on how we could accomplish this, the ManifestPlugin can be used to produce hashed files for the CopyWebpackPlugin when needed (code from my personal website):

return new ManifestPlugin({
    filter(fileDescriptor) {
        // Filter out some weirdness coming from source scss files
        return !fileDescriptor.name.match(/.scss$/);
    },
    map(fileDescriptor) {
        // Correct entries regarding the copy-webpack-plugin
        // adding hashes to both handle and file name
        const copyReplacePattern = new RegExp(
            `(${config.serverPath}/images/backgrounds)/(.+)-([0-9a-f]{8}).(jpg)`,
        );

        const copyMatches = fileDescriptor.name.match(copyReplacePattern);
        if (copyMatches && copyMatches.length === 5) {
            fileDescriptor.name = `${config.serverPath}/images/backgrounds/${copyMatches[2]}.${copyMatches[4]}`;
        }

        return fileDescriptor;
    },
    writeToFileEmit: true,
    basePath: `${config.serverPath}/`,
    publicPath: config.isHmr
        ? `//${config.serverHost}:${config.serverPort}/${config.serverPath}/`
        : `/${config.serverPath}/`,
});

This function knows in advance that the only copied files are some images I had in a specific directory, but we can adapt this logic to fix hashes going into the manifest in a more general way.

With this, we could probably stop using the forked WebpackManifestPlugin, as @weaverryan said:

but I think maybe we don’t need that anymore, as the PR I linked to IS merged now
So we could probably require the plugin again at version 3.1.1 or higher

... and work our way from there.

So, to summarize, from @weaverryan :


So, things we could do:

  • [ ] Re-add WebpackManifestPlugin properly (instead of using a copy)
  • [ ] Investigate getting CopyWebpackPlugin working with WebpackManifestPlugin (found this https://github.com/webpack-contrib/copy-webpack-plugin/issues/104#issuecomment-370174211 )
  • [ ] If we do (A) and (B), we could deprecate our custom copy() functionality, which is pretty complex

shadowc avatar Feb 09 '22 16:02 shadowc

Sounds great! Let's do it :)

weaverryan avatar Feb 09 '22 19:02 weaverryan