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

Commit

Permalink
feat: pass immutable flag to asset info (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschlenk committed Aug 31, 2020
1 parent 718aef5 commit 40fcde8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
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);

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);
});
});

0 comments on commit 40fcde8

Please sign in to comment.