Skip to content

Commit

Permalink
Rename "esm.sh/run"(v2) to "esm.sh/x" (esm-dev#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Dec 19, 2024
1 parent 3a6f28b commit d12e633
Show file tree
Hide file tree
Showing 32 changed files with 384 additions and 410 deletions.
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
.PHONY: cli
cli:
@go run cli/cmd/main.go
cli/run:
@DEBUG=1 go run cli/cmd/main.go run cli/cmd/demo/${app}

serv: config.json
@rm -rf .esmd/storage
@go run main.go --config=config.json --debug

.PHONY: test
test:
@./test/bootstrap.ts ${dir}

run: config.json
@rm -rf .esmd/storage
@go run main.go --config=config.json --debug
2 changes: 1 addition & 1 deletion cli/cmd/demo/preact/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/svelte/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/vanilla/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
</script>
</head>
<body>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/vue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-markdown/preact/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-markdown/react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-markdown/svelte/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-markdown/vanilla/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
</script>
</head>
<body>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-markdown/vue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-unocss/preact/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<div id="root">
<p class="center-box font-sans text-gray-400">Loading...</p>
</div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-unocss/react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<div id="root">
<p class="center-box font-sans text-gray-400">Loading...</p>
</div>
<script type="module" src="https://esm.sh/run" main="./app/main.tsx"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.tsx"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-unocss/svelte/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<div id="root">
<p class="center-box font-sans text-gray-400">Loading...</p>
</div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-unocss/vanilla/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ <h1 class="font-sans text-5xl fw500 text-primary select-none">esm.sh</h1>
<a class="logo i-tabler-brand-github" href="https://github.com/esm-dev/esm.sh" target="_blank" title="Github"></a>
</div>
</div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion cli/cmd/demo/with-unocss/vue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<div id="root">
<p class="center-box font-sans text-gray-400">Loading...</p>
</div>
<script type="module" src="https://esm.sh/run" main="./app/main.ts"></script>
<script type="module" src="https://esm.sh/x" main="./app/main.ts"></script>
</body>
</html>
8 changes: 5 additions & 3 deletions cli/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ type LoaderWorker struct {
outReader *bufio.Reader
}

func (l *LoaderWorker) Start(loaderjs []byte) (err error) {
func (l *LoaderWorker) Start(wd string, loaderJS []byte) (err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
jsPath := filepath.Join(homeDir, ".esm.sh", "run", fmt.Sprintf("loader@%d.js", VERSION))
fi, err := os.Stat(jsPath)
if (err != nil && os.IsNotExist(err)) || (err == nil && fi.Size() != int64(len(loaderjs))) || os.Getenv("DEBUG") == "1" {
if (err != nil && os.IsNotExist(err)) || (err == nil && fi.Size() != int64(len(loaderJS))) || os.Getenv("DEBUG") == "1" {
os.MkdirAll(filepath.Dir(jsPath), 0755)
err = os.WriteFile(jsPath, loaderjs, 0644)
err = os.WriteFile(jsPath, loaderJS, 0644)
if err != nil {
return
}
Expand All @@ -49,8 +49,10 @@ func (l *LoaderWorker) Start(loaderjs []byte) (err error) {
}

cmd := exec.Command(denoPath, "run", "--no-lock", "-A", jsPath)
cmd.Dir = wd
cmd.Stdin, l.stdin = io.Pipe()
l.stdout, cmd.Stdout = io.Pipe()

err = cmd.Start()
if err != nil {
l.stdin = nil
Expand Down
8 changes: 4 additions & 4 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ func (d *DevServer) ServeHtml(w http.ResponseWriter, r *http.Request, pathname s
w.Write([]byte(">"))
continue
}
// replace `<script type="module" src="https://esm.sh/run" main="$main"></script>`
// replace `<script type="module" src="https://esm.sh/x" main="$main"></script>`
// with `<script type="module" src="$main"></script>`
if (strings.HasPrefix(srcAttr, "http://") || strings.HasPrefix(srcAttr, "https://")) && strings.HasSuffix(srcAttr, "/run") && mainAttr != "" {
if (strings.HasPrefix(srcAttr, "http://") || strings.HasPrefix(srcAttr, "https://")) && strings.HasSuffix(srcAttr, "/x") && mainAttr != "" {
w.Write([]byte("<script"))
for attrKey, attrVal := range attrs {
if attrKey != "main" {
Expand Down Expand Up @@ -323,7 +323,7 @@ func (d *DevServer) ServeHtml(w http.ResponseWriter, r *http.Request, pathname s
}
}
w.Write([]byte("</script>"))
fmt.Fprintf(w, `<script>console.log("%%c💚 Built with esm.sh/run, please uncheck \"Disable cache\" in Network tab for better DX!", "color:green")</script>`)
fmt.Fprintf(w, `<script>console.log("%%c💚 Built with esm.sh/x, please uncheck \"Disable cache\" in Network tab for better DX!", "color:green")</script>`)
}

func (d *DevServer) ServeModule(w http.ResponseWriter, r *http.Request, pathname string, sourceCode []byte) {
Expand Down Expand Up @@ -886,7 +886,7 @@ func (d *DevServer) getLoader() (loader *LoaderWorker, err error) {
return
}
loader = &LoaderWorker{}
err = loader.Start(loaderJs)
err = loader.Start(d.rootDir, loaderJs)
if err != nil {
return nil, err
}
Expand Down
20 changes: 13 additions & 7 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ func (ctx *BuildContext) buildModule() (result *BuildMeta, err error) {
"global.process.env.NODE_ENV": fmt.Sprintf(`"%s"`, nodeEnv),
}
} else {
if ctx.isBrowserTarget() {
switch ctx.esmPath.PkgName {
case "react", "typescript":
// safe to reserve `process` for these packages
delete(define, "process")
}
}
for k, v := range define {
define["global."+k] = v
}
Expand Down Expand Up @@ -791,7 +798,7 @@ func (ctx *BuildContext) buildModule() (result *BuildMeta, err error) {
if args.Kind == esbuild.ResolveJSRequireCall || args.Kind == esbuild.ResolveJSRequireResolve {
ctx.cjsRequires = append(ctx.cjsRequires, [3]string{
"npm:" + specifier,
string(replacement.cjs),
string(replacement.iife),
"",
})
return esbuild.OnResolveResult{
Expand Down Expand Up @@ -1072,22 +1079,21 @@ rebuild:

if len(ctx.cjsRequires) > 0 {
requires := make([][3]string, 0, len(ctx.cjsRequires))
mark := NewStringSet()
set := NewStringSet()
for _, r := range ctx.cjsRequires {
specifier := r[0]
if mark.Has(specifier) {
continue
if !set.Has(specifier) {
set.Add(specifier)
requires = append(requires, r)
}
mark.Add(specifier)
requires = append(requires, r)
}
isEsModule := make([]bool, len(requires))
for i, r := range requires {
specifier := r[0]
importUrl := r[2]
if strings.HasPrefix(specifier, "npm:") {
// npm replacements
fmt.Fprintf(header, `var __%x$=(()=>{%s})();`, i, r[1])
fmt.Fprintf(header, `var __%x$=%s;`, i, r[1])
} else if isJsonModuleSpecifier(specifier) {
fmt.Fprintf(header, `import __%x$ from"%s";`, i, importUrl)
deps.Add(r[1])
Expand Down
5 changes: 4 additions & 1 deletion server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (ctx *BuildContext) resolveEntry(esmPath EsmPath) (entry BuildEntry) {
}
}

// get rid of invalid main and types, and lookup the entry from file system
ctx.finalizeBuildEntry(&entry)

// lookup esm entry from the sub-module directory if it's not defined in `package.json`
Expand Down Expand Up @@ -379,7 +380,7 @@ func (ctx *BuildContext) resolveEntry(esmPath EsmPath) (entry BuildEntry) {
} else {
entry = BuildEntry{
main: pkgJson.Main,
module: pkgJson.Type == "module",
module: pkgJson.Main != "" && (pkgJson.Type == "module" || strings.HasSuffix(pkgJson.Main, ".mjs")),
types: pkgJson.Types,
}
if pkgJson.Module != "" && ctx.existsPkgFile(pkgJson.Module) {
Expand Down Expand Up @@ -429,6 +430,7 @@ func (ctx *BuildContext) resolveEntry(esmPath EsmPath) (entry BuildEntry) {
}
}

// get rid of invalid main and types, and lookup the entry from file system
ctx.finalizeBuildEntry(&entry)

// lookup esm entry from the package directory if it's not defined in `package.json`
Expand Down Expand Up @@ -542,6 +544,7 @@ func (ctx *BuildContext) resolveEntry(esmPath EsmPath) (entry BuildEntry) {
}
}

ctx.finalizeBuildEntry(&entry)
return
}

Expand Down
29 changes: 1 addition & 28 deletions server/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package server
import (
"bytes"
"embed"
"fmt"
"os"
"path"
"strings"
"sync"

esbuild "github.com/evanw/esbuild/pkg/api"
Expand All @@ -15,7 +13,6 @@ import (
var (
embedFS EmbedFS
embedBuildCache sync.Map
npmReplacements map[string]npmReplacement
)

type EmbedFS interface {
Expand All @@ -35,31 +32,7 @@ func (fs MockEmbedFS) ReadFile(name string) ([]byte, error) {
return os.ReadFile(path.Join(fs.root, name))
}

type npmReplacement struct {
esm []byte
cjs []byte
}

func buildNpmReplacements(fs EmbedFS) (err error) {
npmReplacements = make(map[string]npmReplacement)
return walkEmbedFS(fs, "server/embed/npm-replacements", []string{".mjs"}, func(path string) error {
esm, err := fs.ReadFile(path)
if err != nil {
return err
}
code, err := minify(string(esm), esbuild.LoaderJS, esbuild.ES2022)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
npmReplacements[strings.TrimSuffix(strings.TrimSuffix(strings.TrimPrefix(path, "server/embed/npm-replacements/"), ".mjs"), "/index")] = npmReplacement{
esm: esm,
cjs: regexpExportAsExpr.ReplaceAll(bytes.ReplaceAll(bytes.TrimSuffix(bytes.TrimSpace(code), []byte{';'}), []byte("export{"), []byte("return{")), []byte("$2:$1")),
}
return nil
})
}

func buildEmbedTS(filename string, target string, debug bool) (js []byte, err error) {
func buildEmbedTSModule(filename string, target string, debug bool) (js []byte, err error) {
cacheKey := filename + "?" + target
if data, ok := embedBuildCache.Load(cacheKey); ok {
return data.([]byte), nil
Expand Down
59 changes: 0 additions & 59 deletions server/embed/purge.html

This file was deleted.

Loading

0 comments on commit d12e633

Please sign in to comment.