diff --git a/src/index.js b/src/index.js index 07891d0..8916f61 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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 = diff --git a/test/name-option.test.js b/test/name-option.test.js index 22ca42c..fc8d4d2 100644 --- a/test/name-option.test.js +++ b/test/name-option.test.js @@ -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); + }); });