@@ -41,14 +41,30 @@ function throwNotDefined(details : string) : never {
4141}
4242
4343
44+ /**
45+ * Default getPathname implementation
46+ * remove search string
47+ */
48+ const defaultGetPathname = ( path : string ) => {
49+
50+ // alternative: new URL(path, 'file://').pathname
51+ const searchPos = path . indexOf ( '?' ) ;
52+ if ( searchPos !== - 1 )
53+ return path . slice ( 0 , searchPos ) ;
54+ return path ;
55+ }
56+
57+
4458/**
4559 * Default resolve implementation
4660 * resolve() should handle 3 situations :
4761 * - resolve a relative path ( eg. import './details.vue' )
4862 * - resolve an absolute path ( eg. import '/components/card.vue' )
4963 * - resolve a module name ( eg. import { format } from 'date-fns' )
5064 */
51- const defaultPathResolve : PathResolve = ( { refPath, relPath } : PathContext ) => {
65+ const defaultPathResolve : PathResolve = ( { refPath, relPath } : PathContext , options : Options ) => {
66+
67+ const { getPathname } = options ;
5268
5369 // initial resolution: refPath is not defined
5470 if ( refPath === undefined )
@@ -64,7 +80,7 @@ const defaultPathResolve : PathResolve = ({ refPath, relPath } : PathContext) =>
6480 // normalize('./test') -> 'test'
6581 // normalize('/test') -> '/test'
6682
67- return Path . normalize ( Path . join ( Path . dirname ( refPath . toString ( ) ) , relPathStr ) ) ;
83+ return Path . normalize ( Path . join ( Path . dirname ( getPathname ( refPath . toString ( ) ) ) , relPathStr ) ) ;
6884}
6985
7086/**
@@ -73,8 +89,8 @@ const defaultPathResolve : PathResolve = ({ refPath, relPath } : PathContext) =>
7389 */
7490function defaultGetResource ( pathCx : PathContext , options : Options ) : Resource {
7591
76- const { pathResolve, getFile, log } = options ;
77- const path = pathResolve ( pathCx ) ;
92+ const { pathResolve, getPathname , getFile, log } = options ;
93+ const path = pathResolve ( pathCx , options ) ;
7894 const pathStr = path . toString ( ) ;
7995 return {
8096 id : pathStr ,
@@ -86,7 +102,7 @@ function defaultGetResource(pathCx : PathContext, options : Options) : Resource
86102 if ( typeof res === 'string' || res instanceof ArrayBuffer ) {
87103
88104 return {
89- type : Path . extname ( pathStr ) ,
105+ type : Path . extname ( getPathname ( pathStr ) ) ,
90106 getContentData : async ( asBinary ) => {
91107
92108 if ( res instanceof ArrayBuffer !== asBinary )
@@ -103,7 +119,7 @@ function defaultGetResource(pathCx : PathContext, options : Options) : Resource
103119 }
104120
105121 return {
106- type : res . type !== undefined ? res . type : Path . extname ( pathStr ) ,
122+ type : res . type !== undefined ? res . type : Path . extname ( getPathname ( pathStr ) ) ,
107123 getContentData : res . getContentData ,
108124 }
109125 }
@@ -161,6 +177,7 @@ export async function loadModule(path : AbstractPath, options : Options = throwN
161177 pathResolve = defaultPathResolve ,
162178 getResource = defaultGetResource ,
163179 createCJSModule = defaultCreateCJSModule ,
180+ getPathname = defaultGetPathname ,
164181 } = options ;
165182
166183 // moduleCache should be defined with Object.create(null). require('constructor') etc... should not be a default module
@@ -176,6 +193,8 @@ export async function loadModule(path : AbstractPath, options : Options = throwN
176193 getResource,
177194 //@ts -ignore: is specified more than once, so this usage will be overwritten.ts(2783)
178195 createCJSModule,
196+ //@ts -ignore: is specified more than once, so this usage will be overwritten.ts(2783)
197+ getPathname,
179198 ...options ,
180199 } ;
181200
0 commit comments