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

Commit 381d8bd

Browse files
authoredOct 27, 2020
fix: immutable flag when the name option have hash in query string (#392)
1 parent 14ed4c9 commit 381d8bd

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed
 

‎src/index.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default function loader(content) {
1515

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

2019
const url = interpolateName(this, name, {
2120
context,
@@ -54,7 +53,27 @@ export default function loader(content) {
5453
}
5554

5655
if (typeof options.emitFile === 'undefined' || options.emitFile) {
57-
this.emitFile(outputPath, content, null, { immutable });
56+
const assetInfo = {};
57+
58+
if (typeof name === 'string') {
59+
let normalizedName = name;
60+
61+
const idx = normalizedName.indexOf('?');
62+
63+
if (idx >= 0) {
64+
normalizedName = normalizedName.substr(0, idx);
65+
}
66+
67+
const isImmutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?]/gi.test(
68+
normalizedName
69+
);
70+
71+
if (isImmutable === true) {
72+
assetInfo.immutable = true;
73+
}
74+
}
75+
76+
this.emitFile(outputPath, content, null, assetInfo);
5877
}
5978

6079
const esModule =

‎test/name-option.test.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('"name" option', () => {
9090
const stats = await compile(compiler);
9191

9292
for (const [name, info] of stats.compilation.assetsInfo) {
93-
if (name.endsWith('png')) {
93+
if (name.endsWith('.png')) {
9494
expect(info.immutable).toBe(true);
9595
}
9696
}
@@ -105,7 +105,7 @@ describe('"name" option', () => {
105105
const stats = await compile(compiler);
106106

107107
for (const [name, info] of stats.compilation.assetsInfo) {
108-
if (name.endsWith('png')) {
108+
if (name.endsWith('.png')) {
109109
expect(info.immutable).toBe(true);
110110
}
111111
}
@@ -120,13 +120,29 @@ describe('"name" option', () => {
120120
const stats = await compile(compiler);
121121

122122
for (const [name, info] of stats.compilation.assetsInfo) {
123-
if (name.endsWith('png')) {
123+
if (name.startsWith('file.39f5c21c1aee6ff21844c6e1d8251d97.asset.png')) {
124124
expect(info.immutable).toBe(true);
125125
}
126126
}
127127
});
128128

129-
it('should not mark unhashed asset as immutable', async () => {
129+
it('should work and emit "immutable" for hashed assets #3', async () => {
130+
expect.assertions(1);
131+
132+
const compiler = getCompiler('simple.js', {
133+
name: '[name].asset.[ext]?foo=[contenthash]',
134+
});
135+
const stats = await compile(compiler);
136+
137+
for (const [name, info] of stats.compilation.assetsInfo) {
138+
if (name.startsWith('file.asset.png')) {
139+
// eslint-disable-next-line no-undefined
140+
expect(info.immutable).toBe(undefined);
141+
}
142+
}
143+
});
144+
145+
it('should work and not emit "immutable" for not hashed assets', async () => {
130146
expect.assertions(1);
131147

132148
const compiler = getCompiler('simple.js', {
@@ -135,8 +151,9 @@ describe('"name" option', () => {
135151
const stats = await compile(compiler);
136152

137153
for (const [name, info] of stats.compilation.assetsInfo) {
138-
if (name.endsWith('png')) {
139-
expect(info.immutable).toBe(false);
154+
if (name.startsWith('asset.png')) {
155+
// eslint-disable-next-line no-undefined
156+
expect(info.immutable).toBe(undefined);
140157
}
141158
}
142159
});

0 commit comments

Comments
 (0)
This repository has been archived.