Skip to content

Commit

Permalink
final node_modules checks and clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Dec 9, 2024
1 parent fb9d498 commit aa9009a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
5 changes: 1 addition & 4 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ class NodeModulesResource extends ResourceInterface {
const { projectDirectory } = this.compilation.context;
const { pathname, searchParams } = url;
const fromImportMap = pathname.startsWith(IMPORT_MAP_RESOLVED_PREFIX);
const isNodeModulesPathnameShortcut = pathname.startsWith('/node_modules/');
const resolvedHref = fromImportMap
? pathname.replace(IMPORT_MAP_RESOLVED_PREFIX, 'file://')
: isNodeModulesPathnameShortcut
? getResolvedHrefFromPathnameShortcut(pathname, projectDirectory)
: new URL(`.${pathname}`, projectDirectory).href; // worst case fall back, assume project root
: getResolvedHrefFromPathnameShortcut(pathname, projectDirectory);
const params = searchParams.size > 0
? `?${searchParams.toString()}`
: '';
Expand Down
18 changes: 8 additions & 10 deletions packages/plugin-import-raw/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ class ImportRawResource extends ResourceInterface {
}

async shouldResolve(url) {
const { href, pathname, searchParams } = url;
const { href, searchParams } = url;
const matches = (this.options.matches || []).filter(matcher => href.indexOf(matcher) >= 0);

if (matches.length > 0 && !searchParams.has('type') && !pathname.startsWith(IMPORT_MAP_RESOLVED_PREFIX)) {
if (matches.length > 0 && !searchParams.has('type')) {
return true;
}
}

async resolve(url) {
const { projectDirectory } = this.compilation.context;
const { pathname, searchParams } = url;
const { searchParams, href, pathname } = url;
const params = url.searchParams.size > 0
? `${searchParams.toString()}&type=raw`
: 'type=raw';
const root = pathname.startsWith('file://')
? new URL(`file://${pathname}`).href
: pathname.startsWith('/node_modules')
? new URL(`.${pathname}`, projectDirectory).href
: new URL(`file://${pathname}`);
const matchedUrl = new URL(`${root}?${params}`);
const fromImportMap = pathname.startsWith(IMPORT_MAP_RESOLVED_PREFIX);
const resolvedHref = fromImportMap
? pathname.replace(IMPORT_MAP_RESOLVED_PREFIX, 'file://')
: href;
const matchedUrl = new URL(`${resolvedHref}?${params}`);

return new Request(matchedUrl);
}
Expand Down

0 comments on commit aa9009a

Please sign in to comment.