Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit c43f951

Browse files
authored
fix #348, add testcase (#350)
1 parent 77ddc34 commit c43f951

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

server/helper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ export function getSourceType(url: string, contentType?: string): SourceType {
147147
return SourceType.Unknown
148148
}
149149

150+
const reEndsWithVersion = /@\d+(\.\d+){0,2}(\-[a-z0-9]+(\.[a-z0-9]+)?)?$/
151+
150152
/**
151153
* fix remote import url to local
152154
* https://esm.sh/react.js?bundle -> /-/esm.sh/react.YnVuZGxl.js
@@ -161,9 +163,10 @@ export function toLocalPath(url: string): string {
161163
if (search !== '') {
162164
const a = util.splitPath(pathname)
163165
const basename = a.pop()!
164-
const ext = extname(basename)
166+
const realext = extname(basename)
167+
const ext = realext != "" && !basename.match(reEndsWithVersion) ? realext : "js"
165168
const search64 = util.btoaUrl(search.slice(1))
166-
a.push(util.trimSuffix(basename, ext) + `.${search64}` + ext)
169+
a.push(util.trimSuffix(basename, ext) + `.${search64}.` + ext)
167170
pathname = '/' + a.join('/')
168171
}
169172
return [

server/helper_test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ Deno.test('server/helper: toRelativePath', () => {
3838
})
3939

4040
Deno.test('server/helper: toLocalPath', () => {
41+
assertEquals(toLocalPath('https://foo.com/[email protected]?action'), `/-/foo.com/[email protected].${util.btoaUrl('action')}.js`)
4142
assertEquals(toLocalPath('https://deno.land/x/[email protected]/'), '/-/deno.land/x/[email protected]/')
42-
assertEquals(toLocalPath('http://foo.com/bar?lang=us-en'), `/-/http_foo.com/bar.${util.btoaUrl('lang=us-en')}`)
43+
assertEquals(toLocalPath('http://foo.com/bar?lang=us-en'), `/-/http_foo.com/bar.${util.btoaUrl('lang=us-en')}.js`)
4344
assertEquals(toLocalPath('http://foo.com:8080/bar'), '/-/http_foo.com_8080/bar')
4445
assertEquals(toLocalPath('file://foo/bar/'), 'foo/bar/')
4546
assertEquals(toLocalPath('/foo/bar/'), '/foo/bar/')

0 commit comments

Comments
 (0)