Skip to content

Commit

Permalink
Use built-in npm package manager (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Dec 18, 2024
1 parent a7cf10c commit 3a6f28b
Show file tree
Hide file tree
Showing 56 changed files with 2,298 additions and 2,552 deletions.
26 changes: 1 addition & 25 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,6 @@ jobs:
with:
deno-version: v1.x

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Restore pnpm store
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('test/**') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Run `go test`
run: go test ./server ./server/storage -v
env:
Expand All @@ -58,4 +34,4 @@ jobs:
GO_TEST_S3_SECRET_ACCESS_KEY: ${{ secrets.GO_TEST_S3_SECRET_ACCESS_KEY }}

- name: Run `test/bootstrap.ts`
run: test/bootstrap.ts
run: test/bootstrap.ts -q
15 changes: 6 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,16 @@ We use [Deno](https://deno.land) to run all the integration testing cases. Make

```bash
# Run all tests
./test/bootstrap.ts
make test

# Run a test for a specific case (directory name)
./test/bootstrap.ts react-18

# Run tests with `clean` option (purge previous builds)
./test/bootstrap.ts --clean
# Run a specific test
make dir=react-18
```

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

```bash
cp -r test/_template test/case_name
nvim test/case_name/test.ts
./test/bootstrap.ts case_name
cp -r test/_template test/new_test
nvim test/new_test/test.ts
make dir=new_test
```
25 changes: 17 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build Stage
FROM golang:1.23-alpine AS build-stage
# 1. build the server from source code
FROM golang:1.23-alpine AS build

ENV ESM_SH_REPO https://github.com/esm-dev/esm.sh
ENV ESM_SH_VERSION v136
Expand All @@ -10,18 +10,27 @@ RUN git clone --branch $ESM_SH_VERSION --depth 1 $ESM_SH_REPO /tmp/esm.sh
WORKDIR /tmp/esm.sh
RUN CGO_ENABLED=0 GOOS=linux go build -o esmd main.go

# Release Stage
FROM node:22-alpine AS release-stage
# 2. run the server
FROM alpine AS server

ENV HOME /home

RUN apk update && apk add --no-cache git git-lfs libcap-utils
RUN git lfs install
RUN npm i -g pnpm

COPY --from=build-stage /tmp/esm.sh/esmd /bin/esmd
RUN mkdir -p $HOME/.esmd/bin
COPY --from=build /tmp/esm.sh/esmd /bin/esmd
COPY --from=denoland/deno:bin-2.1.4 /deno $HOME/.esmd/bin/deno

## add esm(non-root) user
RUN addgroup -g 1000 esm
RUN adduser -u 1000 -G esm -D esm
RUN chown -R esm:esm $HOME/.esmd

## allow user esm(non-root) to listen 80 port
RUN setcap cap_net_bind_service=ep /bin/esmd
RUN chown node:node /bin/esmd

USER node
USER esm
WORKDIR /tmp
EXPOSE 80
CMD ["esmd"]
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.PHONY: cli
cli:
@go run cli/cmd/main.go

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

run: config.json
@rm -rf .esmd/storage
@go run main.go --debug --config=config.json
@go run main.go --config=config.json --debug
15 changes: 8 additions & 7 deletions cli/cmd/internal/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const regexpVuePath = /^\/\*?vue@([~\^]?[\w\+\-\.]+)(\/|\?|&|$)/;
const regexpSveltePath = /^\/\*?svelte@([~\^]?[\w\+\-\.]+)(\/|\?|&|$)/;
const output = (type, data) => Deno.stdout.write(enc.encode(">>>" + type + ":" + JSON.stringify(data) + "\n"));

let tsx, unoGenerators;
let tsx
let unoGenerators;

async function transformModule(filename, importMap, sourceCode) {
const imports = importMap?.imports;
Expand Down Expand Up @@ -62,7 +63,7 @@ async function transformModule(filename, importMap, sourceCode) {
}

async function transformVue(filename, sourceCode, importMap, isDev) {
const { transform } = await import("npm:@esm.sh/vue-loader@1.0.3");
const { transform } = await import("npm:@esm.sh/vue-compiler@1.0.1");
const ret = await transform(filename, sourceCode, {
imports: { "@vue/compiler-sfc": import("npm:@vue/compiler-sfc@" + getVueVersion(importMap)) },
isDev,
Expand All @@ -73,7 +74,7 @@ async function transformVue(filename, sourceCode, importMap, isDev) {

async function transformSvelte(filename, sourceCode, importMap, isDev) {
const { compile, VERSION } = await import(`npm:svelte@${getSvelteVersion(importMap)}/compiler`);
const majorVersion = parseInt(VERSION.split(".")[0]);
const majorVersion = parseInt(VERSION.split(".", 1)[0]);
if (majorVersion < 5) {
throw new Error("Unsupported Svelte version: " + VERSION + ". Please use svelte@5 or higher.");
}
Expand Down Expand Up @@ -120,12 +121,12 @@ async function unocss(config, content, id) {
if (!unoGenerators) {
unoGenerators = new Map();
}
const generatorKey = config?.filename ?? ".";
let uno = unoGenerators.get(generatorKey);
const generatorId = config?.filename ?? ".";
let uno = unoGenerators.get(generatorId);
if (!uno || uno.configCSS !== config?.css) {
uno = import("npm:@esm.sh/unocss@0.2.2").then(({ init }) => init(config?.css));
uno = import("npm:@esm.sh/unocss@0.4.1").then(({ init }) => init({ configCSS: config?.css }));
uno.configCSS = config?.css;
unoGenerators.set(generatorKey, uno);
unoGenerators.set(generatorId, uno);
}
const { update, generate } = await uno;
if (id) {
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const helpMessage = "\033[30mesm.sh - The no-build CDN for modern web developmen
Usage: esm.sh [command] [options]
Commands:
add Add NPM packages to the "importmap" script
init Create a new esm.sh web app
run Serve an esm.sh web app
add Add NPM packages to the "importmap" script
`

//go:embed internal
Expand All @@ -27,10 +27,10 @@ func main() {
switch os.Args[1] {
case "init":
cli.Init(&efs)
case "run":
cli.Run(&efs)
case "add":
cli.Add()
case "run":
cli.Run(&efs)
default:
fmt.Print(helpMessage)
}
Expand Down
Loading

0 comments on commit 3a6f28b

Please sign in to comment.