Skip to content

Commit 469eeba

Browse files
feat: support the [query] template for the interpolatedName method (#162)
1 parent 909c99d commit 469eeba

9 files changed

+586
-518
lines changed

.travis.yml

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ cache: yarn
1313

1414
matrix:
1515
include:
16-
- node_js: '11'
16+
- node_js: '12'
1717
script: yarn pretest
1818
env: CI=pretest
19+
# Yarn doesn't support node@4
20+
# - node_js: '4'
21+
# script: yarn test:ci
22+
# env: CI=tests 4
1923
- node_js: '6'
2024
script: yarn test:ci
2125
env: CI=tests 6
@@ -25,9 +29,12 @@ matrix:
2529
- node_js: '10'
2630
script: yarn test:ci
2731
env: CI=tests 10
28-
- node_js: '11'
32+
- node_js: '12'
2933
script: yarn test:ci
30-
env: CI=coverage 11
34+
env: CI=coverage 12
35+
- node_js: '13'
36+
script: yarn test:ci
37+
env: CI=coverage 13
3138

3239
before_install:
3340
- yarn install --ignore-engines

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ The following tokens are replaced in the `name` parameter:
176176
* `[ext]` the extension of the resource
177177
* `[name]` the basename of the resource
178178
* `[path]` the path of the resource relative to the `context` query parameter or option.
179-
* `[folder]` the folder the resource is in.
179+
* `[folder]` the folder the resource is in
180+
* `[query]` the queryof the resource, i.e. `?foo=bar`
180181
* `[emoji]` a random emoji representation of `options.content`
181182
* `[emoji:<length>]` same as above, but with a customizable number of emojis
182183
* `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md5 hash)
@@ -200,6 +201,11 @@ Examples
200201
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
201202
// => js/9473fdd0d880a43c21b7778d34872157.script.js
202203

204+
// loaderContext.resourcePath = "/app/js/javascript.js"
205+
// loaderContext.resourceQuery = "?foo=bar"
206+
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext][query]", { content: ... });
207+
// => js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar
208+
203209
// loaderContext.resourcePath = "/app/js/javascript.js"
204210
loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... });
205211
// => js/9473fdd0d880a43c21b7778d34872157.script.js

appveyor.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
environment:
44
matrix:
5+
# Yarn doesn't support node@4
6+
# - nodejs_version: 4
57
- nodejs_version: 6
68
- nodejs_version: 8
79
- nodejs_version: 10
8-
- nodejs_version: 11
10+
- nodejs_version: 12
11+
- nodejs_version: 13
912

1013
clone_depth: 10
1114

azure-pipelines.yml

-21
This file was deleted.

lib/interpolateName.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function interpolateName(loaderContext, name, options) {
5252
let basename = 'file';
5353
let directory = '';
5454
let folder = '';
55+
let query = '';
5556

5657
if (loaderContext.resourcePath) {
5758
const parsed = path.parse(loaderContext.resourcePath);
@@ -83,6 +84,16 @@ function interpolateName(loaderContext, name, options) {
8384
}
8485
}
8586

87+
if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
88+
query = loaderContext.resourceQuery;
89+
90+
const hashIdx = query.indexOf('#');
91+
92+
if (hashIdx >= 0) {
93+
query = query.substr(0, hashIdx);
94+
}
95+
}
96+
8697
let url = filename;
8798

8899
if (content) {
@@ -104,7 +115,8 @@ function interpolateName(loaderContext, name, options) {
104115
.replace(/\[ext\]/gi, () => ext)
105116
.replace(/\[name\]/gi, () => basename)
106117
.replace(/\[path\]/gi, () => directory)
107-
.replace(/\[folder\]/gi, () => folder);
118+
.replace(/\[folder\]/gi, () => folder)
119+
.replace(/\[query\]/gi, () => query);
108120

109121
if (regExp && loaderContext.resourcePath) {
110122
const match = loaderContext.resourcePath.match(new RegExp(regExp));

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "utils for webpack loaders",
66
"dependencies": {
77
"big.js": "^5.2.2",
8-
"emojis-list": "^2.0.0",
8+
"emojis-list": "^3.0.0",
99
"json5": "^1.0.1"
1010
},
1111
"scripts": {
@@ -29,7 +29,7 @@
2929
"eslint-plugin-node": "^8.0.0",
3030
"eslint-plugin-prettier": "^3.0.0",
3131
"jest": "^21.2.1",
32-
"prettier": "^1.15.3",
32+
"prettier": "^1.19.1",
3333
"standard-version": "^4.0.0"
3434
},
3535
"main": "lib/index.js",

test/interpolateName.test.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,58 @@ describe('interpolateName()', () => {
131131
'test content',
132132
'modal.[md5::base64:20].css',
133133
],
134+
[
135+
'/app/js/javascript.js?foo=bar',
136+
'js/[hash].script.[ext][query]',
137+
'test content',
138+
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar',
139+
],
140+
[
141+
'/app/js/javascript.js?foo=bar&bar=baz',
142+
'js/[hash].script.[ext][query]',
143+
'test content',
144+
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar&bar=baz',
145+
],
146+
[
147+
'/app/js/javascript.js?foo',
148+
'js/[hash].script.[ext][query]',
149+
'test content',
150+
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo',
151+
],
152+
[
153+
'/app/js/javascript.js?',
154+
'js/[hash].script.[ext][query]',
155+
'test content',
156+
'js/9473fdd0d880a43c21b7778d34872157.script.js',
157+
],
158+
[
159+
'/app/js/javascript.js?a',
160+
'js/[hash].script.[ext][query]',
161+
'test content',
162+
'js/9473fdd0d880a43c21b7778d34872157.script.js?a',
163+
],
164+
[
165+
'/app/js/javascript.js?foo=bar#hash',
166+
'js/[hash].script.[ext][query]',
167+
'test content',
168+
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar',
169+
],
134170
].forEach((test) => {
135171
it('should interpolate ' + test[0] + ' ' + test[1], () => {
172+
let resourcePath = '';
173+
let resourceQuery = '';
174+
175+
const queryIdx = test[0].indexOf('?');
176+
177+
if (queryIdx >= 0) {
178+
resourcePath = test[0].substr(0, queryIdx);
179+
resourceQuery = test[0].substr(queryIdx);
180+
} else {
181+
resourcePath = test[0];
182+
}
183+
136184
const interpolatedName = loaderUtils.interpolateName(
137-
{ resourcePath: test[0] },
185+
{ resourcePath, resourceQuery },
138186
test[1],
139187
{ content: test[2] }
140188
);

test/stringifyRequest.test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ describe('stringifyRequest()', () => {
170170
),
171171
},
172172
].forEach((testCase) => {
173-
it(`${testCase.test}. should stringify request ${testCase.request} to ${
174-
testCase.expected
175-
} inside context ${testCase.context}`, () => {
173+
it(`${testCase.test}. should stringify request ${testCase.request} to ${testCase.expected} inside context ${testCase.context}`, () => {
176174
const relative = path.relative;
177175

178176
if (testCase.os) {

0 commit comments

Comments
 (0)