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

fix: immutable flag when the name option have hash in query string #392

Merged
merged 1 commit into from Oct 27, 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
23 changes: 21 additions & 2 deletions src/index.js
Expand Up @@ -15,7 +15,6 @@ export default function loader(content) {

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

const url = interpolateName(this, name, {
context,
Expand Down Expand Up @@ -54,7 +53,27 @@ export default function loader(content) {
}

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

if (typeof name === 'string') {
let normalizedName = name;

const idx = normalizedName.indexOf('?');

if (idx >= 0) {
normalizedName = normalizedName.substr(0, idx);
}

const isImmutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?]/gi.test(
normalizedName
);

if (isImmutable === true) {
assetInfo.immutable = true;
}
}

this.emitFile(outputPath, content, null, assetInfo);
}

const esModule =
Expand Down
29 changes: 23 additions & 6 deletions test/name-option.test.js
Expand Up @@ -90,7 +90,7 @@ describe('"name" option', () => {
const stats = await compile(compiler);

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

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

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.endsWith('png')) {
if (name.startsWith('file.39f5c21c1aee6ff21844c6e1d8251d97.asset.png')) {
expect(info.immutable).toBe(true);
}
}
});

it('should not mark unhashed asset as immutable', async () => {
it('should work and emit "immutable" for hashed assets #3', async () => {
expect.assertions(1);

const compiler = getCompiler('simple.js', {
name: '[name].asset.[ext]?foo=[contenthash]',
});
const stats = await compile(compiler);

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.startsWith('file.asset.png')) {
// eslint-disable-next-line no-undefined
expect(info.immutable).toBe(undefined);
}
}
});

it('should work and not emit "immutable" for not hashed assets', async () => {
expect.assertions(1);

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

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.endsWith('png')) {
expect(info.immutable).toBe(false);
if (name.startsWith('asset.png')) {
// eslint-disable-next-line no-undefined
expect(info.immutable).toBe(undefined);
}
}
});
Expand Down