Skip to content

Commit

Permalink
fix: clean-up getUrlQueryParams and cleanUrl as having # in the impor…
Browse files Browse the repository at this point in the history
…ter path is no longer an issue
  • Loading branch information
BlueCutOfficial committed Jul 5, 2024
1 parent 608382d commit 6e54594
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/compat/src/resolver-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class TemplateResolver implements ASTPlugin {
private findRules(absPath: string): PreprocessedComponentRule | undefined {
// when babel is invoked by vite our filenames can have query params still
// hanging off them. That would break rule matching.
absPath = cleanUrl(absPath, true);
absPath = cleanUrl(absPath);

let fileRules = this.rules.files.get(absPath);
let componentRules: PreprocessedComponentRule | undefined;
Expand Down
14 changes: 4 additions & 10 deletions packages/shared-internals/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,17 @@ export function unrelativize(pkg: Package, specifier: string, fromFile: string)

const postfixRE = /[?#].*$/s;

// this pattern includes URL query params (ex: ?direct)
// but excludes specifiers starting with # (ex: @embroider/virtuals/components/fancy)
// so when using this pattern, @embroider/virtual/fancy would be consider a pathname
// without any params.
const postfixREQueryParams = /[?].*$/s;

// this is the same implementation Vite uses internally to keep its
// cache-busting query params from leaking where they shouldn't.
// includeHashSign true means #my-specifier is considered part of the pathname
export function cleanUrl(url: string, includeHashSign = false): string {
const regexp = includeHashSign ? postfixREQueryParams : postfixRE;
export function cleanUrl(url: string): string {
const regexp = postfixRE;
return url.replace(regexp, '');
}

// includeHashSign true means #my-specifier is considered part of the pathname
export function getUrlQueryParams(url: string, includeHashSign = false): string {
const regexp = includeHashSign ? postfixREQueryParams : postfixRE;
export function getUrlQueryParams(url: string): string {
const regexp = postfixRE;
return url.match(regexp)?.[0] ?? '';
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class RollupModuleRequest implements ModuleRequest {
// strip query params off the source but keep track of them
// we use regexp-based methods over a URL object because the
// source can be a relative path.
let cleanSource = cleanUrl(source, true);
let queryParams = getUrlQueryParams(source, true);
let cleanSource = cleanUrl(source);
let queryParams = getUrlQueryParams(source);

return new RollupModuleRequest(
context,
Expand Down

0 comments on commit 6e54594

Please sign in to comment.