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

Commit

Permalink
fix: immutable flag when the name option have hash in query string (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 27, 2020
1 parent 14ed4c9 commit 381d8bd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
23 changes: 21 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 381d8bd

Please sign in to comment.