Skip to content

Commit

Permalink
feat: added the immutable flag to asset with hash in name (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 31, 2020
1 parent 1496f85 commit a1989d5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ class CopyPlugin {
return;
}

const info = compilation.getAsset(targetPath);
const existingAsset = compilation.getAsset(targetPath);

if (info) {
if (existingAsset) {
if (force) {
logger.log(
`force updating '${webpackTo}' to compilation assets from '${absoluteFrom}'`
);

compilation.updateAsset(targetPath, source, { copied: true });
const info = { copied: true };

if (asset.immutable) {
info.immutable = true;
}

compilation.updateAsset(targetPath, source, info);

return;
}
Expand All @@ -134,7 +140,13 @@ class CopyPlugin {
`writing '${webpackTo}' to compilation assets from '${absoluteFrom}'`
);

compilation.emitAsset(targetPath, source, { copied: true });
const info = { copied: true };

if (asset.immutable) {
info.immutable = true;
}

compilation.emitAsset(targetPath, source, info);
});

logger.debug('end to adding additional assets');
Expand Down
5 changes: 5 additions & 0 deletions src/postProcessPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default async function postProcessPattern(globalRef, pattern, file) {
file.webpackTo = file.webpackTo.replace(/\.?\[ext]/g, '');
}

file.immutable = /\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi.test(
file.webpackTo
);

file.webpackTo = loaderUtils.interpolateName(
{ resourcePath: file.absoluteFrom },
file.webpackTo,
Expand All @@ -113,6 +117,7 @@ export default async function postProcessPattern(globalRef, pattern, file) {
`transforming path '${file.webpackTo}' for '${file.absoluteFrom}'`
);

file.immutable = false;
file.webpackTo = await pattern.transformPath(
file.webpackTo,
file.absoluteFrom
Expand Down
44 changes: 44 additions & 0 deletions test/CopyPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,50 @@ describe('CopyPlugin', () => {
.catch(done);
});

it('should copy files with "copied" flags', (done) => {
expect.assertions(5);

const expectedAssetKeys = [
'directoryfile.5d7817ed5bc246756d73d6a4c8e94c33.txt',
'.dottedfile.5e294e270db6734a42f014f0dd18d9ac',
'nested/nestedfile.31d6cfe0d16ae931b73c59d7e0c089c0.txt',
'nested/deep-nested/deepnested.31d6cfe0d16ae931b73c59d7e0c089c0.txt',
];

run({
preCopy: {
additionalAssets: [
{
name: 'directoryfile.5d7817ed5bc246756d73d6a4c8e94c33.txt',
data: 'Content',
info: { custom: true },
},
],
},
expectedAssetKeys,
patterns: [
{
from: 'directory',
to: '[path][name].[contenthash].[ext]',
force: true,
},
],
})
.then(({ stats }) => {
for (const name of expectedAssetKeys) {
const info = stats.compilation.assetsInfo.get(name);

expect(info.immutable).toBe(true);

if (name === 'directoryfile.5d7817ed5bc246756d73d6a4c8e94c33.txt') {
expect(info.immutable).toBe(true);
}
}
})
.then(done)
.catch(done);
});

it('should copy files and print "copied" in the string representation ', (done) => {
const isWebpack4 = webpack.version[0] === '4';

Expand Down

0 comments on commit a1989d5

Please sign in to comment.