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

Commit 99ec6b6

Browse files
author
Je
committed
feat: use esm.sh
1 parent 218d753 commit 99ec6b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+37
-241482
lines changed

cli.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,9 @@ function main() {
9393
if (match) {
9494
const port = parseInt(match[2])
9595
listenAndServe({ port }, async (req: ServerRequest) => {
96-
const url = new URL('http://localhost' + req.url)
97-
98-
if (/^\/react(\-dom)?$/.test(url.pathname)) {
99-
const vendorName = path.basename(url.pathname)
100-
const env = url.searchParams.get('env') === 'development' ? 'development' : 'production'
101-
req.respond({
102-
status: 200,
103-
headers: new Headers({
104-
'Content-Type': 'application/javascript',
105-
'X-TypeScript-Types': `/${vendorName}/types/index.d.ts`
106-
}),
107-
body: [
108-
`export * from '/${vendorName}/${vendorName}.${env}.js';`,
109-
`export { default } from '/${vendorName}/${vendorName}.${env}.js';`
110-
].join('\n')
111-
})
112-
return
113-
}
114-
11596
try {
97+
const url = new URL('http://localhost' + req.url)
11698
let filepath = path.join(Deno.cwd(), url.pathname)
117-
if (/^\/react(\-dom)?\//.test(url.pathname)) {
118-
filepath = path.join(Deno.cwd(), 'vendor', url.pathname)
119-
}
12099
const info = await Deno.lstat(filepath)
121100
if (info.isDirectory) {
122101
const r = Deno.readDir(filepath)
@@ -139,7 +118,6 @@ function main() {
139118
})
140119
return
141120
}
142-
143121
req.respond({
144122
status: 200,
145123
headers: new Headers({ 'Content-Type': getContentType(filepath) }),

deps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
export { minify } from 'http://localhost/[[email protected],[email protected]]/terser'
3+
export { default as ts } from 'http://localhost/[[email protected],[email protected]]/typescript'
14
export { Tar, Untar } from 'https://deno.land/[email protected]/archive/tar.ts'
25
export * as colors from 'https://deno.land/[email protected]/fmt/colors.ts'
36
export { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
@@ -11,7 +14,4 @@ export * as ws from 'https://deno.land/[email protected]/ws/mod.ts'
1114
export { gzipDecode, gzipEncode } from 'https://deno.land/x/[email protected]/mod.ts'
1215
export { default as AnsiUp } from './vendor/ansi-up/ansi-up.ts'
1316
export { default as less } from './vendor/less/less.js'
14-
export { minify } from './vendor/terser/terser.js'
15-
// @deno-types="./vendor/typescript/lib/typescript.d.ts"
16-
export { default as ts } from './vendor/typescript/lib/typescript.js'
1717
import './vendor/clean-css-builds/v4.2.2.js'

examples/hello-world/import_map.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"https://esm.sh/": "https://esm.sh/[react,react-dom]/"
4+
}
5+
}

hmr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import runtime from 'https://esm.sh/react-refresh/runtime?dev'
12
import events from './events.ts'
23
import util, { hashShort } from './util.ts'
3-
import runtime from './vendor/react-refresh/runtime.js'
44

55
interface Callback {
66
(...args: any[]): void

import_map.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"imports": {
3-
"https://esm.sh/": "http://localhost:9006/",
43
"https://deno.land/x/aleph/": "http://localhost:9006/"
54
}
65
}

project.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,24 @@ export default class Project {
712712
break
713713
}
714714
}
715+
if (dlUrl.startsWith('https://esm.sh/[')) {
716+
dlUrl.replace(/\[([^\]]+)\]/, (_, s: string) => {
717+
const list = s.split(',').map(s => s.trim())
718+
if (list.length > 0) {
719+
const mod = util.trimPrefix(url, 'https://esm.sh/').replace(/\/+$/, '')
720+
if (!list.includes(mod)) {
721+
dlUrl = url
722+
}
723+
}
724+
return _
725+
})
726+
}
715727
if (this.isDev && url.startsWith('https://esm.sh/')) {
716-
dlUrl += '?env=development'
728+
const u = new URL(dlUrl)
729+
if (!u.searchParams.has('dev')) {
730+
u.searchParams.set('env', 'development')
731+
}
732+
dlUrl = u.toString()
717733
}
718734
if (mod.sourceHash === '') {
719735
log.info('Download', url, dlUrl != url ? colors.dim(`• ${dlUrl}`) : '')
@@ -821,7 +837,7 @@ export default class Project {
821837
reactRefresh: this.isDev && !mod.isRemote && (mod.id === './404.js' || mod.id === './app.js' || mod.id.startsWith('./pages/') || mod.id.startsWith('./components/')),
822838
rewriteImportPath: (path: string) => this._rewriteImportPath(mod, path),
823839
}
824-
const { diagnostics, outputText, sourceMapText } = compile(mod.url, sourceContent, compileOptions)
840+
const { diagnostics, outputText, sourceMapText } = compile(mod.sourceFilePath, sourceContent, compileOptions)
825841
if (diagnostics && diagnostics.length > 0) {
826842
throw new Error(`compile ${url}: ${diagnostics.map(d => d.messageText).join(' ')}`)
827843
}
@@ -840,8 +856,14 @@ export default class Project {
840856
content: sourceMapText!,
841857
}
842858
})
843-
mod.jsContent = code
844-
mod.jsSourceMap = map
859+
if (code) {
860+
mod.jsContent = code
861+
} else {
862+
mod.jsContent = jsContent
863+
}
864+
if (util.isNEString(map)) {
865+
mod.jsSourceMap = map
866+
}
845867
}
846868
mod.hash = (new Sha1).update(mod.jsContent).hex()
847869
}
@@ -1057,7 +1079,7 @@ function renameImportUrl(importUrl: string): string {
10571079
const ext = path.extname(path.basename(url.pathname)) || '.js'
10581080
let pathname = util.trimSuffix(url.pathname, ext)
10591081
if (url.search) {
1060-
pathname += '/' + btoa(url.search).replace(/[\.\/=]/g, '')
1082+
pathname += '@' + btoa(url.search).replace(/\//g, '_').replace(/=/g, '')
10611083
}
10621084
if (isRemote) {
10631085
return '/-/' + url.hostname + (url.port ? '/' + url.port : '') + pathname + ext

vendor/react-dom/LICENSE.txt

Lines changed: 0 additions & 21 deletions
This file was deleted.

vendor/react-dom/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

vendor/react-dom/package.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)