Skip to content

Commit

Permalink
Splitting modules by analyzing dependency tree (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Dec 29, 2024
1 parent 74ca7a4 commit 8deff62
Show file tree
Hide file tree
Showing 52 changed files with 1,327 additions and 1,209 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.go]
indent_style = tab
indent_size = 2
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
with:
go-version: 1.23.x

- name: Run `deploy-ci.sh`
- name: Run Deploy Script
run: ./scripts/deploy-ci.sh
env:
GOOS: linux
GOOS: ${{ secrets.DEPLOY_HOST_OS }}
GOARCH: ${{ secrets.DEPLOY_HOST_ARCH }}
DEPLOY_HOST_NAME: ${{ secrets.DEPLOY_HOST_NAME }}
DEPLOY_HOST_PORT: ${{ secrets.DEPLOY_HOST_PORT }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ jobs:
with:
deno-version: v1.x

- name: Run `go test`
- name: Run Unit Tests
run: go test ./server ./server/storage -v
env:
GO_TEST_S3_ENDPOINT: ${{ secrets.GO_TEST_S3_ENDPOINT }}
GO_TEST_S3_REGION: ${{ secrets.GO_TEST_S3_REGION }}
GO_TEST_S3_ACCESS_KEY_ID: ${{ secrets.GO_TEST_S3_ACCESS_KEY_ID }}
GO_TEST_S3_SECRET_ACCESS_KEY: ${{ secrets.GO_TEST_S3_SECRET_ACCESS_KEY }}

- name: Run `test/bootstrap.ts`
- name: Run Integration Tests
run: test/bootstrap.ts -q
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# esm.sh

The _no-build_ content delivery network(CDN) for modern web development.
A _no-build_ content delivery network(CDN) for modern web development.

## How to Use

Expand Down Expand Up @@ -92,17 +92,35 @@ import useSWR from "https://esm.sh/swr?alias=react:preact/compat&deps=preact@10.

### Bundling Strategy

By default, esm.sh bundles sub-modules that ain't declared in the `exports` field.
By default, esm.sh bundles sub-modules of a package that are not shared by entry modules defined in the `exports` field of `package.json`.

Bundling sub-modules can reduce the number of network requests for performance. However, it may bundle shared modules
repeatedly. In extreme case, it may break the side effects of the package, or change the `import.meta.url` semantics. To
avoid this, you can add `?bundle=false` to disable the default bundling behavior:
Bundling sub-modules can reduce the number of network requests, improving performance. However, it may result in repeated bundling of shared modules. In extreme cases, this can break package side effects or alter the `import.meta.url` semantics. To prevent this, you can disable the default bundling behavior by adding `?bundle=false`:

```js
import "https://esm.sh/@pyscript/core?bundle=false";
```

For package authors, you can override the bundling strategy by adding the `esm.sh` field to `package.json`:
For package authors, it is recommended to define the `exports` field in `package.json`. This specifies the entry modules of the package, allowing esm.sh to accurately analyze the dependency tree and bundle the modules without duplication.

```jsonc
{
"name": "foo",
"exports": {
".": {
"import": "./index.js",
"require": "./index.cjs",
"types": "./index.d.ts"
},
"./submodule": {
"import": "./submodule.js",
"require": "./submodule.cjs",
"types": "./submodule.d.ts"
}
}
}
```

Or you can override the bundling strategy by adding the `esm.sh` field to your `package.json`:

```jsonc
{
Expand All @@ -113,7 +131,7 @@ For package authors, you can override the bundling strategy by adding the `esm.s
}
```

esm.sh also supports `?bundle=all` query to bundle the module with all external dependencies(except in `peerDependencies`) into a single JS file.
You can also use the `?bundle=all` query to bundle the module along with all its external dependencies (excluding those in `peerDependencies`) into a single JavaScript file.

```js
import { Button } from "https://esm.sh/antd?bundle=all";
Expand Down
6 changes: 3 additions & 3 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ func Init(efs *embed.FS) {

if *framework == "" {
*framework = term.Select(raw, "Select a framework:", frameworks)
} else if !includes(frameworks, *framework) {
} else if !stringInSlice(frameworks, *framework) {
fmt.Println("Invalid framework: ", *framework)
os.Exit(1)
}

if *cssFramework == "" {
*cssFramework = term.Select(raw, "Select a CSS framework:", cssFrameworks)
} else if !includes(cssFrameworks, *cssFramework) {
} else if !stringInSlice(cssFrameworks, *cssFramework) {
*cssFramework = cssFrameworks[0]
}

if *lang == "" {
*lang = term.Select(raw, "Select a variant:", langVariants)
} else if !includes(langVariants, *lang) {
} else if !stringInSlice(langVariants, *lang) {
*lang = langVariants[0]
}

Expand Down
4 changes: 2 additions & 2 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func parseCommandFlag() (string, []string) {
return args[0], args[1:]
}

// includes returns true if the given value is included in the array.
func includes(arr []string, value string) bool {
// stringInSlice returns true if the given value is included in the array.
func stringInSlice(arr []string, value string) bool {
for _, v := range arr {
if v == value {
return true
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/evanw/esbuild v0.24.2
github.com/gorilla/websocket v1.5.3
github.com/ije/esbuild-internal v0.24.2
github.com/ije/gox v0.9.3
github.com/ije/rex v1.14.4
github.com/ije/gox v0.9.7
github.com/ije/rex v1.14.5
github.com/mssola/useragent v1.0.0
github.com/yuin/goldmark v1.7.8
github.com/yuin/goldmark-meta v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/ije/esbuild-internal v0.24.2 h1:i1sjIu6suFZ1Arc4w0gWNE8OLLiAhEQ3nhU987uB+5w=
github.com/ije/esbuild-internal v0.24.2/go.mod h1:s7HvKZ4ZGifyzvgWpSwnJOQTr6b+bsgfNBZ8HAEwwSM=
github.com/ije/gox v0.9.3 h1:2JdnELIXaHF1JCojgS0ftq4ftF7koBcSO+PtVSWyZP8=
github.com/ije/gox v0.9.3/go.mod h1:3GTaK8WXf6oxRbrViLqKNLTNcMR871Dz0zoujFNmG48=
github.com/ije/rex v1.14.4 h1:wnARHMHmP7rwYuuzkg8nsxTJ4xa2aFuMqzGE7YDeS4A=
github.com/ije/rex v1.14.4/go.mod h1:OSFw49N5XRBzjskSqsAKzNPxksK9gUWT4CKjO7KkyWU=
github.com/ije/gox v0.9.7 h1:KfEiWOvk/ZqhJnTxJCWMB8mkBLi3HYhGJrrxQyua204=
github.com/ije/gox v0.9.7/go.mod h1:3GTaK8WXf6oxRbrViLqKNLTNcMR871Dz0zoujFNmG48=
github.com/ije/rex v1.14.5 h1:Db6lqRJOMxqeiSs4cixa8AkIqkldhMg0mO+qdHmrOUE=
github.com/ije/rex v1.14.5/go.mod h1:+ZeiG36LreDX5XrIl4i4feyvjr5kzMcHCO/a6LIoS48=
github.com/mssola/useragent v1.0.0 h1:WRlDpXyxHDNfvZaPEut5Biveq86Ze4o4EMffyMxmH5o=
github.com/mssola/useragent v1.0.0/go.mod h1:hz9Cqz4RXusgg1EdI4Al0INR62kP7aPSRNHnpU+b85Y=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
Expand Down
32 changes: 16 additions & 16 deletions scripts/deploy-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ ssh next.esm.sh << EOF
fi
echo \$gv
servicefn=/etc/systemd/system/esmd.service
servicefile=/etc/systemd/system/esmd.service
reload=no
if [ ! -f \$servicefn ]; then
echo "[Unit]" >> \$servicefn
echo "Description=esm.sh service" >> \$servicefn
echo "After=network.target" >> \$servicefn
echo "StartLimitIntervalSec=0" >> \$servicefn
echo "[Service]" >> \$servicefn
echo "Type=simple" >> \$servicefn
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefn
echo "USER=\${USER}" >> \$servicefn
echo "Restart=always" >> \$servicefn
echo "RestartSec=5" >> \$servicefn
echo "Environment=\"USER=\${USER}\"" >> \$servicefn
echo "Environment=\"HOME=\${HOME}\"" >> \$servicefn
echo "[Install]" >> \$servicefn
echo "WantedBy=default.target" >> \$servicefn
if [ ! -f \$servicefile ]; then
echo "[Unit]" >> \$servicefile
echo "Description=esm.sh service" >> \$servicefile
echo "After=network.target" >> \$servicefile
echo "StartLimitIntervalSec=0" >> \$servicefile
echo "[Service]" >> \$servicefile
echo "Type=simple" >> \$servicefile
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefile
echo "USER=\${USER}" >> \$servicefile
echo "Restart=always" >> \$servicefile
echo "RestartSec=5" >> \$servicefile
echo "Environment=\"USER=\${USER}\"" >> \$servicefile
echo "Environment=\"HOME=\${HOME}\"" >> \$servicefile
echo "[Install]" >> \$servicefile
echo "WantedBy=default.target" >> \$servicefile
reload=yes
else
systemctl stop esmd.service
Expand Down
36 changes: 18 additions & 18 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,31 @@ ssh -p $sshPort ${user}@${host} << EOF
echo \$gv
if [ "$init" == "yes" ]; then
servicefn=/etc/systemd/system/esmd.service
if [ -f \$servicefn ]; then
rm -f servicefn
servicefile=/etc/systemd/system/esmd.service
if [ -f \$servicefile ]; then
rm -f servicefile
fi
echo "[Unit]" >> \$servicefn
echo "Description=esm.sh service" >> \$servicefn
echo "After=network.target" >> \$servicefn
echo "StartLimitIntervalSec=0" >> \$servicefn
echo "[Service]" >> \$servicefn
echo "Type=simple" >> \$servicefn
echo "[Unit]" >> \$servicefile
echo "Description=esm.sh service" >> \$servicefile
echo "After=network.target" >> \$servicefile
echo "StartLimitIntervalSec=0" >> \$servicefile
echo "[Service]" >> \$servicefile
echo "Type=simple" >> \$servicefile
if [ "$config" != "" ]; then
mkdir -p /etc/esmd
rm -f /etc/esmd/config.json
echo "$config" >> /etc/esmd/config.json
echo "ExecStart=/usr/local/bin/esmd --config=/etc/esmd/config.json" >> \$servicefn
echo "ExecStart=/usr/local/bin/esmd --config=/etc/esmd/config.json" >> \$servicefile
else
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefn
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefile
fi
echo "USER=\${USER}" >> \$servicefn
echo "Restart=always" >> \$servicefn
echo "RestartSec=5" >> \$servicefn
echo "Environment=\"USER=\${USER}\"" >> \$servicefn
echo "Environment=\"HOME=\${HOME}\"" >> \$servicefn
echo "[Install]" >> \$servicefn
echo "WantedBy=default.target" >> \$servicefn
echo "USER=\${USER}" >> \$servicefile
echo "Restart=always" >> \$servicefile
echo "RestartSec=5" >> \$servicefile
echo "Environment=\"USER=\${USER}\"" >> \$servicefile
echo "Environment=\"HOME=\${HOME}\"" >> \$servicefile
echo "[Install]" >> \$servicefile
echo "WantedBy=default.target" >> \$servicefile
else
systemctl stop esmd.service
echo "Stopped esmd.service."
Expand Down
Loading

0 comments on commit 8deff62

Please sign in to comment.