Skip to content

Commit 9a7b00e

Browse files
authored
Refactor project structure (#1087)
1 parent 1fe60f2 commit 9a7b00e

File tree

131 files changed

+365
-257
lines changed

Some content is hidden

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

131 files changed

+365
-257
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ on:
88

99
jobs:
1010
test:
11-
name: Test All
11+
name: Deno
1212
runs-on: ubuntu-latest
1313

14+
strategy:
15+
matrix:
16+
deno-version: [1.x, 2.x]
17+
1418
steps:
1519
- name: Checkout
1620
uses: actions/checkout@v4
@@ -23,7 +27,7 @@ jobs:
2327
- name: Setup Deno
2428
uses: denoland/setup-deno@main
2529
with:
26-
deno-version: v1.x
30+
deno-version: ${{ matrix.deno-version }}
2731

2832
- name: Run Unit Tests
2933
run: go test ./server ./server/storage -v

CONTRIBUTING.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Create a `config.json` file in the project root directory following the example
2424
"port": 8080,
2525
"workDir": ".esmd",
2626
"npmRegistry": "https://registry.npmjs.org/", // change to your own registry if needed
27-
"npmToken": "xxxxxx", // remove this line if you don't have one
27+
"npmToken": "xxxxxx" // remove this line if you don't have one
2828
}
2929
```
3030

@@ -33,29 +33,30 @@ More server options please check [config.exmaple.jsonc](./config.example.jsonc).
3333
## Running the Server from Source Code
3434

3535
```bash
36-
go run -tags debug main.go
37-
# or
38-
make dev
36+
make dev/server
3937
```
4038

4139
Then you can import `React` from "http://localhost:8080/react"
4240

43-
## Running Integration Tests
41+
## Running Server Integration Tests
4442

4543
We use [Deno](https://deno.land) to run all the integration testing cases. Make sure you have Deno installed on your computer.
4644

4745
```bash
4846
# Run all tests
49-
make test
47+
make test/server
5048

5149
# Run a specific test
52-
make test dir=react-18
50+
make test/server dir=react-18
5351
```
5452

55-
To add a new integration test case, copy the [test/_template](./test/_template) directory and rename it to your case name.
53+
To add a new integration test case, copy the [test/.template](./test/.template) directory and rename it to your case name.
5654

5755
```bash
58-
cp -r test/_template test/new_test
59-
nvim test/new_test/test.ts
60-
make dir=new_test
56+
# copy the testing template
57+
cp -r test/.template test/test-case-name
58+
# edit the test code
59+
vi test/test-case-name/test.ts
60+
# run the test
61+
make test/server dir=test-case-name
6162
```

HOSTING.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You can find all the server options in [config.example.jsonc](./config.example.j
3030
You will need [Go](https://golang.org/dl) 1.22+ to compile and run the server.
3131

3232
```bash
33-
go run main.go --config=config.json
33+
go run server/cmd/main.go --config=config.json
3434
```
3535

3636
Then you can import `React` from <http://localhost:8080/react>.
@@ -40,15 +40,15 @@ Then you can import `React` from <http://localhost:8080/react>.
4040
You can deploy the server to a single machine with the [deploy.sh](./scripts/deploy.sh) script.
4141

4242
```bash
43-
# first time deploy
43+
# first time to deploy
4444
./scripts/deploy.sh --init
4545
# update the server
4646
./scripts/deploy.sh
4747
```
4848

4949
Recommended hosting requirements:
5050

51-
- Linux system (Debian/Ubuntu)
51+
- Linux system with systemd installed
5252
- 4x CPU cores or more
5353
- 8GB RAM or more
5454
- 100GB disk space or more
@@ -164,3 +164,6 @@ To deploy the server with CloudFlare CDN, you need to create following cache rul
164164
```ruby
165165
(not starts_with(http.user_agent, "Deno/") and not starts_with(http.user_agent, "Bun/") and not starts_with(http.user_agent, "Node/") and not starts_with(http.user_agent, "Node.js/") and http.user_agent ne "undici")
166166
```
167+
168+
> [!NOTE]
169+
> Since Cloudflare does not respect the `Vary` header, we need to bypass the cache for `Deno`/`Bun`/`Node` clients.

Makefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
dev/cli:
2-
@go run -tags debug cli/cmd/main.go serve cli/cmd/demo/${app}
1+
dev/cli/serve:
2+
@go run -tags debug main.go serve cli/demo/${app}
33

4-
dev: config.json
4+
dev/cli/init:
5+
@go run -tags debug main.go init
6+
7+
dev/cli/i:
8+
@go run -tags debug main.go i ${package}
9+
10+
dev/cli/im/add:
11+
@go run -tags debug main.go im add ${package}
12+
13+
dev/cli/im/update:
14+
@go run -tags debug main.go im update ${package}
15+
16+
dev/server: config.json
517
@rm -rf .esmd/log
618
@rm -rf .esmd/storage
719
@rm -rf .esmd/esm.db
8-
@go run -tags debug main.go --config=config.json
20+
@go run -tags debug server/cmd/main.go --config=config.json
921

10-
.PHONY: test
11-
test:
22+
test/server:
1223
@./test/bootstrap.ts ${dir}

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ With [import maps](https://github.com/WICG/import-maps), you can even use bare i
5757
```js
5858
// Examples
5959
import tslib from "https://esm.sh/gh/microsoft/tslib"; // latest
60-
import tslib from "https://esm.sh/gh/microsoft/tslib@2.6.0"; // the version '2.6.0' is a git tag
60+
import tslib from "https://esm.sh/gh/microsoft/tslib@d72d6f7"; // with commit hash
61+
import tslib from "https://esm.sh/gh/microsoft/tslib@v2.8.0"; // with tag
6162
```
6263
- **[pkg.pr.new](https://pkg.pr.new)** (starts with `/pr/` or `/pkg.pr.new/`):
6364
```js
@@ -66,9 +67,9 @@ With [import maps](https://github.com/WICG/import-maps), you can even use bare i
6667
import { Bench } from "https://esm.sh/pr/tinybench@a832a55"; // --compact
6768
```
6869

69-
### Transforming `.tsx`/`.vue`/`.svelte` on the Fly
70+
### Transforming `.ts(x)`/`.vue`/`.svelte` on the Fly
7071

71-
esm.sh allows you to import `.tsx`, `.vue`, and `.svelte` files directly in the browser without any build steps.
72+
esm.sh allows you to import `.ts(x)`, `.vue`, and `.svelte` files directly in the browser without any build steps.
7273

7374
```js
7475
import { Airplay } from "https://esm.sh/gh/phosphor-icons/react@v2.1.5/src/csr/Airplay.tsx?deps=react@18.2.0";

cli/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# A CLI tool for no-build web apps with esm.sh CDN.
2+
3+
A CLI tool for no-build web apps with esm.sh CDN, written in Go.
4+
5+
## Installation
6+
7+
You can install esm.sh CLI from source code:
8+
9+
```bash
10+
go install github.com/esm-dev/esm.sh
11+
```
12+
13+
You can also install esm.sh CLI via `npm`:
14+
15+
```bash
16+
npm install -g esm.sh
17+
```
18+
19+
Or use `npx` without installation:
20+
21+
```bash
22+
npx esm.sh [command]
23+
```
24+
25+
### Usage
26+
27+
```
28+
$ esm.sh --help
29+
Usage: esm.sh [command] <options>
30+
31+
Commands:
32+
i, add [...pakcage] Alias to 'esm.sh im add'.
33+
im, importmap Manage "importmap" script.
34+
init Create a new no-build web app with esm.sh CDN.
35+
serve Serve a no-build web app with esm.sh CDN, HMR, transforming TS/Vue/Svelte on the fly.
36+
build Build a no-build web app with esm.sh CDN.
37+
```

cli/add.go

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

cli/cmd/main.go

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

cli/command_im.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const helpMessage = "\033[30mImport Map Management CLI with esm.sh CDN.\033[0m" + `
9+
10+
Usage: esm.sh im [sub-command] <options>
11+
12+
Sub Commands:
13+
add [...packages] Add packages to "importmap" script
14+
update [...packages] Update packages in "importmap" script
15+
`
16+
17+
// Manage `importmap` script
18+
func ManageImportMap(subCommand string) {
19+
if len(os.Args) < 3 {
20+
fmt.Print(helpMessage)
21+
return
22+
}
23+
var packages []string
24+
if subCommand == "" {
25+
subCommand = os.Args[2]
26+
packages = os.Args[3:]
27+
} else {
28+
packages = os.Args[2:]
29+
}
30+
switch subCommand {
31+
case "add":
32+
if len(packages) == 0 {
33+
return
34+
}
35+
fmt.Println(packages)
36+
case "update":
37+
fmt.Println(packages)
38+
default:
39+
fmt.Printf("Unknown sub command \"%s\"\n", subCommand)
40+
}
41+
}

0 commit comments

Comments
 (0)