| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,52 +197,66 @@ In loader context `[hash]` and `[contenthash]` are the same, but we recommend us | |||
| 197 | 197 | Examples | |
| 198 | 198 | ||
| 199 | 199 | ``` javascript | |
| 200 | - // loaderContext.resourcePath = "/app/js/javascript.js" | ||
| 200 | + // loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" | ||
| 201 | 201 | loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... }); | |
| 202 | 202 | // => js/9473fdd0d880a43c21b7778d34872157.script.js | |
| 203 | 203 | ||
| 204 | - // loaderContext.resourcePath = "/app/js/javascript.js" | ||
| 204 | + // loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" | ||
| 205 | 205 | // loaderContext.resourceQuery = "?foo=bar" | |
| 206 | 206 | loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext][query]", { content: ... }); | |
| 207 | 207 | // => js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar | |
| 208 | 208 | ||
| 209 | - // loaderContext.resourcePath = "/app/js/javascript.js" | ||
| 209 | + // loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" | ||
| 210 | 210 | loaderUtils.interpolateName(loaderContext, "js/[contenthash].script.[ext]", { content: ... }); | |
| 211 | 211 | // => js/9473fdd0d880a43c21b7778d34872157.script.js | |
| 212 | 212 | ||
| 213 | - // loaderContext.resourcePath = "/app/page.html" | ||
| 213 | + // loaderContext.resourcePath = "/absolute/path/to/app/page.html" | ||
| 214 | 214 | loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... }); | |
| 215 | 215 | // => html-9473fd.html | |
| 216 | 216 | ||
| 217 | - // loaderContext.resourcePath = "/app/flash.txt" | ||
| 217 | + // loaderContext.resourcePath = "/absolute/path/to/app/flash.txt" | ||
| 218 | 218 | loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... }); | |
| 219 | 219 | // => c31e9820c001c9c4a86bce33ce43b679 | |
| 220 | 220 | ||
| 221 | - // loaderContext.resourcePath = "/app/img/image.gif" | ||
| 221 | + // loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif" | ||
| 222 | 222 | loaderUtils.interpolateName(loaderContext, "[emoji]", { content: ... }); | |
| 223 | 223 | // => 👍 | |
| 224 | 224 | ||
| 225 | - // loaderContext.resourcePath = "/app/img/image.gif" | ||
| 225 | + // loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif" | ||
| 226 | 226 | loaderUtils.interpolateName(loaderContext, "[emoji:4]", { content: ... }); | |
| 227 | 227 | // => 🙍🏢📤🐝 | |
| 228 | 228 | ||
| 229 | - // loaderContext.resourcePath = "/app/img/image.png" | ||
| 229 | + // loaderContext.resourcePath = "/absolute/path/to/app/img/image.png" | ||
| 230 | 230 | loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7].[ext]", { content: ... }); | |
| 231 | 231 | // => 2BKDTjl.png | |
| 232 | 232 | // use sha512 hash instead of md5 and with only 7 chars of base64 | |
| 233 | 233 | ||
| 234 | - // loaderContext.resourcePath = "/app/img/myself.png" | ||
| 234 | + // loaderContext.resourcePath = "/absolute/path/to/app/img/myself.png" | ||
| 235 | 235 | // loaderContext.query.name = | |
| 236 | 236 | loaderUtils.interpolateName(loaderContext, "picture.png"); | |
| 237 | 237 | // => picture.png | |
| 238 | 238 | ||
| 239 | - // loaderContext.resourcePath = "/app/dir/file.png" | ||
| 239 | + // loaderContext.resourcePath = "/absolute/path/to/app/dir/file.png" | ||
| 240 | 240 | loaderUtils.interpolateName(loaderContext, "[path][name].[ext]?[hash]", { content: ... }); | |
| 241 | 241 | // => /app/dir/file.png?9473fdd0d880a43c21b7778d34872157 | |
| 242 | 242 | ||
| 243 | - // loaderContext.resourcePath = "/app/js/page-home.js" | ||
| 243 | + // loaderContext.resourcePath = "/absolute/path/to/app/js/page-home.js" | ||
| 244 | 244 | loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: ... }); | |
| 245 | 245 | // => script-home.js | |
| 246 | + | ||
| 247 | + // loaderContext.resourcePath = "/absolute/path/to/app/js/javascript.js" | ||
| 248 | + // loaderContext.resourceQuery = "?foo=bar" | ||
| 249 | + loaderUtils.interpolateName( | ||
| 250 | + loaderContext, | ||
| 251 | + (resourcePath, resourceQuery) => { | ||
| 252 | + // resourcePath - `/app/js/javascript.js` | ||
| 253 | + // resourceQuery - `?foo=bar` | ||
| 254 | + | ||
| 255 | + return "js/[hash].script.[ext]"; | ||
| 256 | + }, | ||
| 257 | + { content: ... } | ||
| 258 | + ); | ||
| 259 | + // => js/9473fdd0d880a43c21b7778d34872157.script.js | ||
| 246 | 260 | ``` | |
| 247 | 261 | ||
| 248 | 262 | ### `getHashDigest` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,8 +38,14 @@ function encodeStringToEmoji(content, length) { | |||
| 38 | 38 | function interpolateName(loaderContext, name, options) { | |
| 39 | 39 | let filename; | |
| 40 | 40 | ||
| 41 | + const hasQuery = | ||
| 42 | + loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1; | ||
| 43 | + | ||
| 41 | 44 | if (typeof name === 'function') { | |
| 42 | - filename = name(loaderContext.resourcePath); | ||
| 45 | + filename = name( | ||
| 46 | + loaderContext.resourcePath, | ||
| 47 | + hasQuery ? loaderContext.resourceQuery : undefined | ||
| 48 | + ); | ||
| 43 | 49 | } else { | |
| 44 | 50 | filename = name || '[hash].[ext]'; | |
| 45 | 51 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,6 +167,50 @@ describe('interpolateName()', () => { | |||
| 167 | 167 | 'test content', | |
| 168 | 168 | 'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar', | |
| 169 | 169 | ], | |
| 170 | + [ | ||
| 171 | + '/app/js/javascript.js?foo=bar#hash', | ||
| 172 | + (resourcePath, resourceQuery) => { | ||
| 173 | + expect(resourcePath).toBeDefined(); | ||
| 174 | + expect(resourceQuery).toBeDefined(); | ||
| 175 | + | ||
| 176 | + return 'js/[hash].script.[ext][query]'; | ||
| 177 | + }, | ||
| 178 | + 'test content', | ||
| 179 | + 'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar', | ||
| 180 | + ], | ||
| 181 | + [ | ||
| 182 | + '/app/js/javascript.js?a', | ||
| 183 | + (resourcePath, resourceQuery) => { | ||
| 184 | + expect(resourcePath).toBeDefined(); | ||
| 185 | + expect(resourceQuery).toBeDefined(); | ||
| 186 | + | ||
| 187 | + return 'js/[hash].script.[ext][query]'; | ||
| 188 | + }, | ||
| 189 | + 'test content', | ||
| 190 | + 'js/9473fdd0d880a43c21b7778d34872157.script.js?a', | ||
| 191 | + ], | ||
| 192 | + [ | ||
| 193 | + '/app/js/javascript.js', | ||
| 194 | + (resourcePath, resourceQuery) => { | ||
| 195 | + expect(resourcePath).toBeDefined(); | ||
| 196 | + expect(resourceQuery).not.toBeDefined(); | ||
| 197 | + | ||
| 198 | + return 'js/[hash].script.[ext][query]'; | ||
| 199 | + }, | ||
| 200 | + 'test content', | ||
| 201 | + 'js/9473fdd0d880a43c21b7778d34872157.script.js', | ||
| 202 | + ], | ||
| 203 | + [ | ||
| 204 | + '/app/js/javascript.js?', | ||
| 205 | + (resourcePath, resourceQuery) => { | ||
| 206 | + expect(resourcePath).toBeDefined(); | ||
| 207 | + expect(resourceQuery).not.toBeDefined(); | ||
| 208 | + | ||
| 209 | + return 'js/[hash].script.[ext][query]'; | ||
| 210 | + }, | ||
| 211 | + 'test content', | ||
| 212 | + 'js/9473fdd0d880a43c21b7778d34872157.script.js', | ||
| 213 | + ], | ||
| 170 | 214 | ].forEach((test) => { | |
| 171 | 215 | it('should interpolate ' + test[0] + ' ' + test[1], () => { | |
| 172 | 216 | let resourcePath = ''; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments