Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

feat: pass immutable info on files #383

Merged
merged 1 commit into from Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/index.js
Expand Up @@ -14,16 +14,14 @@ export default function loader(content) {
});

const context = options.context || this.rootContext;
const name = options.name || '[contenthash].[ext]';
const immutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?\]/gi.test(name);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be another function in https://github.com/webpack/loader-utils

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will test it deeply in near future


const url = loaderUtils.interpolateName(
this,
options.name || '[contenthash].[ext]',
{
context,
content,
regExp: options.regExp,
}
);
const url = loaderUtils.interpolateName(this, name, {
context,
content,
regExp: options.regExp,
});

let outputPath = url;

Expand Down Expand Up @@ -56,7 +54,7 @@ export default function loader(content) {
}

if (typeof options.emitFile === 'undefined' || options.emitFile) {
this.emitFile(outputPath, content);
this.emitFile(outputPath, content, null, { immutable });
}

const esModule =
Expand Down
34 changes: 34 additions & 0 deletions test/name-option.test.js
Expand Up @@ -82,4 +82,38 @@ describe('"name" option', () => {
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should mark hashed asset as immutable', async () => {
const compiler = getCompiler('simple.js', {
name: '[md5:hash:hex:8].asset.[ext]',
});
const stats = await compile(compiler);

let assetInfo;
for (const [name, info] of stats.compilation.assetsInfo) {
if (name.match('asset.')) {
assetInfo = info;
break;
}
}

expect(assetInfo.immutable).toBe(true);
});

it('should not mark unhashed asset as immutable', async () => {
const compiler = getCompiler('simple.js', {
name: 'asset.[ext]',
});
const stats = await compile(compiler);

let assetInfo;
for (const [name, info] of stats.compilation.assetsInfo) {
if (name.match('asset.')) {
assetInfo = info;
break;
}
}

expect(assetInfo.immutable).toBe(false);
});
});