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

Commit 40fcde8

Browse files
authoredAug 31, 2020
feat: pass immutable flag to asset info (#383)
1 parent 718aef5 commit 40fcde8

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed
 

‎src/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ export default function loader(content) {
1414
});
1515

1616
const context = options.context || this.rootContext;
17+
const name = options.name || '[contenthash].[ext]';
18+
const immutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?\]/gi.test(name);
1719

18-
const url = loaderUtils.interpolateName(
19-
this,
20-
options.name || '[contenthash].[ext]',
21-
{
22-
context,
23-
content,
24-
regExp: options.regExp,
25-
}
26-
);
20+
const url = loaderUtils.interpolateName(this, name, {
21+
context,
22+
content,
23+
regExp: options.regExp,
24+
});
2725

2826
let outputPath = url;
2927

@@ -56,7 +54,7 @@ export default function loader(content) {
5654
}
5755

5856
if (typeof options.emitFile === 'undefined' || options.emitFile) {
59-
this.emitFile(outputPath, content);
57+
this.emitFile(outputPath, content, null, { immutable });
6058
}
6159

6260
const esModule =

‎test/name-option.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,38 @@ describe('"name" option', () => {
8282
);
8383
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
8484
});
85+
86+
it('should mark hashed asset as immutable', async () => {
87+
const compiler = getCompiler('simple.js', {
88+
name: '[md5:hash:hex:8].asset.[ext]',
89+
});
90+
const stats = await compile(compiler);
91+
92+
let assetInfo;
93+
for (const [name, info] of stats.compilation.assetsInfo) {
94+
if (name.match('asset.')) {
95+
assetInfo = info;
96+
break;
97+
}
98+
}
99+
100+
expect(assetInfo.immutable).toBe(true);
101+
});
102+
103+
it('should not mark unhashed asset as immutable', async () => {
104+
const compiler = getCompiler('simple.js', {
105+
name: 'asset.[ext]',
106+
});
107+
const stats = await compile(compiler);
108+
109+
let assetInfo;
110+
for (const [name, info] of stats.compilation.assetsInfo) {
111+
if (name.match('asset.')) {
112+
assetInfo = info;
113+
break;
114+
}
115+
}
116+
117+
expect(assetInfo.immutable).toBe(false);
118+
});
85119
});

0 commit comments

Comments
 (0)
This repository has been archived.