From cd0e4284a0aa090f8776c4eb1045d4b1080e7161 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 19 Feb 2020 20:31:51 +0300 Subject: [PATCH 1/2] feat: the `resourceQuery` is passed to the `interpolateName` method (#163) --- README.md | 36 ++++++++++++++++++++--------- lib/interpolateName.js | 8 ++++++- test/interpolateName.test.js | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b595781..37fb15d 100644 --- a/README.md +++ b/README.md @@ -197,52 +197,66 @@ In loader context `[hash]` and `[contenthash]` are the same, but we recommend us Examples ``` javascript -// loaderContext.resourcePath = "/app/js/javascript.js" +// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... }); // => js/9473fdd0d880a43c21b7778d34872157.script.js -// loaderContext.resourcePath = "/app/js/javascript.js" +// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" // loaderContext.resourceQuery = "?foo=bar" loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext][query]", { content: ... }); // => js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar -// loaderContext.resourcePath = "/app/js/javascript.js" +// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... }); // => js/9473fdd0d880a43c21b7778d34872157.script.js -// loaderContext.resourcePath = "/app/page.html" +// loaderContext.resourcePath = "/absolute/path/to/app/page.html" loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... }); // => html-9473fd.html -// loaderContext.resourcePath = "/app/flash.txt" +// loaderContext.resourcePath = "/absolute/path/to/app/flash.txt" loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... }); // => c31e9820c001c9c4a86bce33ce43b679 -// loaderContext.resourcePath = "/app/img/image.gif" +// loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif" loaderUtils.interpolateName(loaderContext, "[emoji]", { content: ... }); // => 👍 -// loaderContext.resourcePath = "/app/img/image.gif" +// loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif" loaderUtils.interpolateName(loaderContext, "[emoji:4]", { content: ... }); // => 🙍🏢📤🐝 -// loaderContext.resourcePath = "/app/img/image.png" +// loaderContext.resourcePath = "/absolute/path/to/app/img/image.png" loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7].[ext]", { content: ... }); // => 2BKDTjl.png // use sha512 hash instead of md5 and with only 7 chars of base64 -// loaderContext.resourcePath = "/app/img/myself.png" +// loaderContext.resourcePath = "/absolute/path/to/app/img/myself.png" // loaderContext.query.name = loaderUtils.interpolateName(loaderContext, "picture.png"); // => picture.png -// loaderContext.resourcePath = "/app/dir/file.png" +// loaderContext.resourcePath = "/absolute/path/to/app/dir/file.png" loaderUtils.interpolateName(loaderContext, "[path][name].[ext]?[hash]", { content: ... }); // => /app/dir/file.png?9473fdd0d880a43c21b7778d34872157 -// loaderContext.resourcePath = "/app/js/page-home.js" +// loaderContext.resourcePath = "/absolute/path/to/app/js/page-home.js" loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: ... }); // => script-home.js + +// loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" +// loaderContext.resourceQuery = "?foo=bar" +loaderUtils.interpolateName( + loaderContext, + (resourcePath, resourceQuery) => { + // resourcePath - `/app/js/javascript.js` + // resourceQuery - `?foo=bar` + + return "js/[hash].script.[ext]"; + }, + { content: ... } +); +// => js/9473fdd0d880a43c21b7778d34872157.script.js ``` ### `getHashDigest` diff --git a/lib/interpolateName.js b/lib/interpolateName.js index 96cb7ac..6a13a36 100644 --- a/lib/interpolateName.js +++ b/lib/interpolateName.js @@ -38,8 +38,14 @@ function encodeStringToEmoji(content, length) { function interpolateName(loaderContext, name, options) { let filename; + const hasQuery = + loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1; + if (typeof name === 'function') { - filename = name(loaderContext.resourcePath); + filename = name( + loaderContext.resourcePath, + hasQuery ? loaderContext.resourceQuery : undefined + ); } else { filename = name || '[hash].[ext]'; } diff --git a/test/interpolateName.test.js b/test/interpolateName.test.js index 12c37c8..9f31464 100644 --- a/test/interpolateName.test.js +++ b/test/interpolateName.test.js @@ -167,6 +167,50 @@ describe('interpolateName()', () => { 'test content', 'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar', ], + [ + '/app/js/javascript.js?foo=bar#hash', + (resourcePath, resourceQuery) => { + expect(resourcePath).toBeDefined(); + expect(resourceQuery).toBeDefined(); + + return 'js/[hash].script.[ext][query]'; + }, + 'test content', + 'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar', + ], + [ + '/app/js/javascript.js?a', + (resourcePath, resourceQuery) => { + expect(resourcePath).toBeDefined(); + expect(resourceQuery).toBeDefined(); + + return 'js/[hash].script.[ext][query]'; + }, + 'test content', + 'js/9473fdd0d880a43c21b7778d34872157.script.js?a', + ], + [ + '/app/js/javascript.js', + (resourcePath, resourceQuery) => { + expect(resourcePath).toBeDefined(); + expect(resourceQuery).not.toBeDefined(); + + return 'js/[hash].script.[ext][query]'; + }, + 'test content', + 'js/9473fdd0d880a43c21b7778d34872157.script.js', + ], + [ + '/app/js/javascript.js?', + (resourcePath, resourceQuery) => { + expect(resourcePath).toBeDefined(); + expect(resourceQuery).not.toBeDefined(); + + return 'js/[hash].script.[ext][query]'; + }, + 'test content', + 'js/9473fdd0d880a43c21b7778d34872157.script.js', + ], ].forEach((test) => { it('should interpolate ' + test[0] + ' ' + test[1], () => { let resourcePath = ''; From d95b8b53f0ad547133b47ac8226f735c479f76de Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 19 Feb 2020 20:32:36 +0300 Subject: [PATCH 2/2] chore(release): 1.4.0 --- CHANGELOG.md | 10 ++++++++++ package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31d28c7..4d316b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +# [1.4.0](https://github.com/webpack/loader-utils/compare/v1.3.0...v1.4.0) (2020-02-19) + + +### Features + +* the `resourceQuery` is passed to the `interpolateName` method ([#163](https://github.com/webpack/loader-utils/issues/163)) ([cd0e428](https://github.com/webpack/loader-utils/commit/cd0e428)) + + + # [1.3.0](https://github.com/webpack/loader-utils/compare/v1.2.3...v1.3.0) (2020-02-19) diff --git a/package.json b/package.json index f2a336a..6abe0f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loader-utils", - "version": "1.3.0", + "version": "1.4.0", "author": "Tobias Koppers @sokra", "description": "utils for webpack loaders", "dependencies": { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy