Skip to content

Commit

Permalink
refactor: use query-string instead of manual query parsing (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Oct 8, 2020
1 parent dc16939 commit e85888a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ exports[`basic resolvers: css 1`] = `
}
.foo3 {
background-image: url(\\"/pubpath/bg.png?query\\");
background-image: url(\\"/pubpath/bg.png?query#hash\\");
}
.bar {
background-image: url(\\"/pubpath/bg1.png\\");
Expand Down Expand Up @@ -221,7 +221,7 @@ exports[`basic resolvers-hash: css 1`] = `
}
.foo3 {
background-image: url(\\"/pubpath/bg-bd25d3fd.png?query\\");
background-image: url(\\"/pubpath/bg-bd25d3fd.png?query#hash\\");
}
.bar {
background-image: url(\\"/pubpath/bg-cc57d19a.png\\");
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/resolvers/features/foo.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
}

.foo3 {
background-image: url("./bg.png?query");
background-image: url("./bg.png?query#hash");
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"postcss-modules-scope": "^2.2.0",
"postcss-modules-values": "^3.0.0",
"postcss-value-parser": "^4.1.0",
"query-string": "^6.13.5",
"resolve": "^1.17.0",
"source-map": "^0.7.3",
"tslib": "^2.0.0"
Expand Down
25 changes: 24 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions src/loaders/postcss/url/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs-extra";
import { ParseOptions, parseUrl, stringifyUrl } from "query-string";

import resolveAsync from "../../../utils/resolve-async";

Expand All @@ -13,22 +14,19 @@ export interface UrlFile {
}

/** URL resolver */
export type UrlResolve = (url: string, basedir: string) => Promise<UrlFile>;
export type UrlResolve = (inputUrl: string, basedir: string) => Promise<UrlFile>;

const resolve: UrlResolve = async (url, basedir) => {
const resolve: UrlResolve = async (inputUrl, basedir) => {
const options = { basedir };
let from: string;
const urlWithQueryMatch = /([^?]*)(\?.*)/.exec(url);
let urlQuery = "";
if (urlWithQueryMatch) {
url = urlWithQueryMatch[1];
urlQuery = urlWithQueryMatch[2];
}
const parseOptions: ParseOptions = { parseFragmentIdentifier: true, sort: false, decode: false };
const { url, query, fragmentIdentifier } = parseUrl(inputUrl, parseOptions);
try {
from = await resolveAsync(url, options);
} catch {
from = await resolveAsync(`./${url}`, options);
}
const urlQuery = stringifyUrl({ url: "", query, fragmentIdentifier }, parseOptions);
return { from, source: await fs.readFile(from), urlQuery };
};

Expand Down

0 comments on commit e85888a

Please sign in to comment.