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 19, 2024
1 parent a309525 commit 7a4d2cb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 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
4 changes: 2 additions & 2 deletions packages/shared-internals/src/colocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function syntheticJStoHBS(source: string): string | null {
// explicit js is the only case we care about here. Synthetic template JS is
// only ever JS (never TS or anything else). And extensionless imports are
// handled by the default resolving system doing extension search.
if (cleanUrl(source, true).endsWith('.js')) {
if (cleanUrl(source).endsWith('.js')) {
return source.replace(/.js(\?.*)?/, '.hbs$1');
}

Expand All @@ -19,7 +19,7 @@ export function needsSyntheticComponentJS(
foundFile: string,
packageCache: Pick<PackageCache, 'ownerOfFile'>
): string | null {
requestedSpecifier = cleanUrl(requestedSpecifier, true);
requestedSpecifier = cleanUrl(requestedSpecifier);
foundFile = cleanUrl(foundFile);
if (
discoveredImplicitHBS(requestedSpecifier, foundFile) &&
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 @@ -29,8 +29,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 7a4d2cb

Please sign in to comment.