Skip to content

Commit b4b2c0a

Browse files
alqhjannikkeye
andcommitted
fix: handling resource paths with query parameters
* Issue #55, #50 - Fix issue with loading resource with Query parameters, it doesn't currently slice the path for query parameters * fix(core): support paths with query params and loaders * fix: linter warrnings Co-authored-by: Jannik Keye <jannik.keye@peerigon.com>
1 parent af5b624 commit b4b2c0a

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/extractLoader.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) {
7575
}
7676
}
7777

78+
function extractQueryFromPath(givenRelativePath) {
79+
const indexOfLastExclMark = givenRelativePath.lastIndexOf("!");
80+
const indexOfQuery = givenRelativePath.lastIndexOf("?");
81+
82+
if (indexOfQuery !== -1 && indexOfQuery > indexOfLastExclMark) {
83+
return {
84+
relativePathWithoutQuery: givenRelativePath.slice(0, indexOfQuery),
85+
query: givenRelativePath.slice(indexOfQuery),
86+
};
87+
}
88+
89+
return {
90+
relativePathWithoutQuery: givenRelativePath,
91+
query: "",
92+
};
93+
}
94+
7895
async function evalModule(src, filename) {
7996
const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumber();
8097
const rndPlaceholderPattern = new RegExp(rndPlaceholder, "g");
@@ -91,10 +108,8 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) {
91108
exports,
92109
__webpack_public_path__: publicPath, // eslint-disable-line camelcase
93110
require: givenRelativePath => {
94-
const indexOfQuery = Math.max(givenRelativePath.indexOf("?"), givenRelativePath.length);
95-
const relativePathWithoutQuery = givenRelativePath.slice(0, indexOfQuery);
111+
const {relativePathWithoutQuery, query} = extractQueryFromPath(givenRelativePath);
96112
const indexOfLastExclMark = relativePathWithoutQuery.lastIndexOf("!");
97-
const query = givenRelativePath.slice(indexOfQuery);
98113
const loaders = givenRelativePath.slice(0, indexOfLastExclMark + 1);
99114
const relativePath = relativePathWithoutQuery.slice(indexOfLastExclMark + 1);
100115
const absolutePath = resolve.sync(relativePath, {
@@ -161,6 +176,9 @@ function rndNumber() {
161176
.slice(2);
162177
}
163178

179+
// getPublicPath() encapsulates the complexity of reading the publicPath from the current
180+
// webpack config. Let's keep the complexity in this function.
181+
/* eslint-disable complexity */
164182
/**
165183
* Retrieves the public path from the loader options, context.options (webpack <4) or context._compilation (webpack 4+).
166184
* context._compilation is likely to get removed in a future release, so this whole function should be removed then.
@@ -186,6 +204,7 @@ function getPublicPath(options, context) {
186204

187205
return "";
188206
}
207+
/* eslint-enable complexity */
189208

190209
// For CommonJS interoperability
191210
module.exports = extractLoader;

test/extractLoader.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ describe("extractLoader", () => {
2121
expect(simpleJs).to.be.a.file();
2222
expect(simpleJs).to.have.content("hello");
2323
}));
24+
it("should extract resource with query params into simple-css-with-query-param.js", () =>
25+
compile({testModule: "simple-css-with-query-params.js"}).then(() => {
26+
const simpleJs = path.resolve(__dirname, "dist/simple-css-with-query-params-dist.js");
27+
28+
expect(simpleJs).to.be.a.file();
29+
expect(simpleJs).to.have.content("simple-dist.css");
30+
}));
31+
it("should extract resource with query params and loader into simple-css-with-query-param-and-loader.js", () =>
32+
compile({testModule: "simple-css-with-query-params-and-loader.js"}).then(() => {
33+
const simpleJs = path.resolve(__dirname, "dist/simple-css-with-query-params-and-loader-dist.js");
34+
35+
expect(simpleJs).to.be.a.file();
36+
expect(simpleJs).to.have.content("renamed-simple.css");
37+
}));
2438
it("should extract the html of modules/simple.html into simple.html", () =>
2539
compile({testModule: "simple.html"}).then(() => {
2640
const simpleHtml = path.resolve(__dirname, "dist/simple-dist.html");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable import/unambiguous, import/no-unresolved, import/no-webpack-loader-syntax */
2+
module.exports = require("!!file-loader?name=renamed-simple.css!./simple.css?v=1.2");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable import/unambiguous, import/no-unresolved */
2+
module.exports = require("./simple.css?v=1.2");

0 commit comments

Comments
 (0)