Skip to content

Commit

Permalink
Merge pull request #159 from zeabur/yuaanlin/nodejs-layer-caching
Browse files Browse the repository at this point in the history
perf(planner/node.js): Enable Node.js layer caching for install deps
  • Loading branch information
MichaelYuhe authored Oct 20, 2023
2 parents 10630d3 + 403167c commit d169c3d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
18 changes: 12 additions & 6 deletions internal/nodejs/__snapshots__/template_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ ENV PORT=8080
WORKDIR /src

RUN corepack enable && corepack prepare --all
COPY . .

RUN yarn install

COPY . .

# Build if we can build it


Expand All @@ -25,10 +26,11 @@ ENV PORT=8080
WORKDIR /src

RUN corepack enable && corepack prepare --all
COPY . .

RUN yarn install

COPY . .

# Build if we can build it


Expand All @@ -44,10 +46,11 @@ ENV PORT=8080
WORKDIR /src

RUN corepack enable && corepack prepare --all
COPY . .

RUN yarn install

COPY . .

# Build if we can build it


Expand All @@ -63,10 +66,11 @@ ENV PORT=8080
WORKDIR /src

RUN corepack enable && corepack prepare --all
COPY . .

RUN yarn install

COPY . .

# Build if we can build it
RUN yarn build

Expand All @@ -82,10 +86,11 @@ ENV PORT=8080
WORKDIR /src

RUN corepack enable && corepack prepare --all
COPY . .

RUN yarn install

COPY . .

# Build if we can build it
RUN yarn build

Expand All @@ -107,10 +112,11 @@ COPY --from=bun-runtime /usr/local/bin/bun /usr/local/bin
COPY --from=bun-runtime /usr/local/bin/bunx /usr/local/bin

RUN corepack enable && corepack prepare --all
COPY . .

RUN bun install

COPY . .

# Build if we can build it


Expand Down
12 changes: 6 additions & 6 deletions internal/nodejs/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
func TestGetContextBasedOnMeta_MapShouldBeCorrect(t *testing.T) {
meta := getContextBasedOnMeta(types.PlanMeta{
"nodeVersion": "16",
"installCmd": "npm install",
"installCmd": "RUN npm install",
"buildCmd": "npm run build",
"startCmd": "npm run start",
})

assert.Equal(t, meta, TemplateContext{
NodeVersion: "16",
InstallCmd: "npm install",
InstallCmd: "RUN npm install",
BuildCmd: "npm run build",
StartCmd: "npm run start",
})
Expand All @@ -27,7 +27,7 @@ func TestGetContextBasedOnMeta_MapShouldBeCorrect(t *testing.T) {
func TestGetContextBasedOnMeta_WithOutputdirAndSPAFramework(t *testing.T) {
meta := getContextBasedOnMeta(types.PlanMeta{
"nodeVersion": "16",
"installCmd": "npm install",
"installCmd": "RUN npm install",
"buildCmd": "npm run build",
"startCmd": "npm run start",
"outputDir": "dist",
Expand All @@ -36,7 +36,7 @@ func TestGetContextBasedOnMeta_WithOutputdirAndSPAFramework(t *testing.T) {

assert.Equal(t, meta, TemplateContext{
NodeVersion: "16",
InstallCmd: "npm install",
InstallCmd: "RUN npm install",
BuildCmd: "npm run build",
StartCmd: "npm run start",
})
Expand All @@ -45,7 +45,7 @@ func TestGetContextBasedOnMeta_WithOutputdirAndSPAFramework(t *testing.T) {
func TestGetContextBasedOnMeta_WithOutputdirAndMPAFramework(t *testing.T) {
meta := getContextBasedOnMeta(types.PlanMeta{
"nodeVersion": "16",
"installCmd": "npm install",
"installCmd": "RUN npm install",
"buildCmd": "npm run build",
"startCmd": "npm run start",
"outputDir": "dist",
Expand All @@ -54,7 +54,7 @@ func TestGetContextBasedOnMeta_WithOutputdirAndMPAFramework(t *testing.T) {

assert.Equal(t, meta, TemplateContext{
NodeVersion: "16",
InstallCmd: "npm install",
InstallCmd: "RUN npm install",
BuildCmd: "npm run build",
StartCmd: "npm run start",
})
Expand Down
26 changes: 14 additions & 12 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,34 +439,36 @@ func GetInstallCmd(ctx *nodePlanContext) string {
}

pkgManager := DeterminePackageManager(ctx)
var installCmd string
cmds := []string{"COPY package.json* tsconfig.json* .npmrc* ."}
switch pkgManager {
case types.NodePackageManagerNpm:
installCmd = "npm install"
cmds = append(cmds, "COPY package-lock.json* .", "RUN npm install")
case types.NodePackageManagerPnpm:
installCmd = "pnpm install"
cmds = append(cmds, "COPY pnpm-lock.yaml* .", "RUN pnpm install")
case types.NodePackageManagerBun:
installCmd = "bun install"
cmds = append(cmds, "COPY bun.lockb* .", "RUN bun install")
case types.NodePackageManagerYarn:
fallthrough
cmds = append(cmds, "COPY yarn.lock* .", "RUN yarn install")
default:
installCmd = "yarn install"
cmds = append(cmds, "RUN yarn install")
}

needPlaywright := DetermineNeedPlaywright(ctx)
if needPlaywright {
installCmd = `apt-get update && apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdbus-1-3 libdrm2 libxkbcommon-x11-0 libxcomposite-dev libxdamage1 libxfixes-dev libxrandr2 libgbm-dev libasound2 && ` + installCmd
cmds = append([]string{
"RUN apt-get update && apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdbus-1-3 libdrm2 libxkbcommon-x11-0 libxcomposite-dev libxdamage1 libxfixes-dev libxrandr2 libgbm-dev libasound2",
}, cmds...)
}

needPuppeteer := DetermineNeedPuppeteer(ctx)
if needPuppeteer {
installCmd = `apt-get update
RUN apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0 libxshmfence1 libglu1
ENV PUPPETEER_CACHE_DIR=/src/.cache/puppeteer
RUN ` + installCmd
cmds = append([]string{
"RUN apt-get update && apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0 libxshmfence1 libglu1",
"ENV PUPPETEER_CACHE_DIR=/src/.cache/puppeteer",
}, cmds...)
}

*cmd = optional.Some(installCmd)
*cmd = optional.Some(strings.Join(cmds, "\n"))
return cmd.Unwrap()
}

Expand Down
12 changes: 6 additions & 6 deletions internal/nodejs/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestTemplate_NBuildCmd_NOutputDir(t *testing.T) {
ctx := nodejs.TemplateContext{
NodeVersion: "18",

InstallCmd: "yarn install",
InstallCmd: "RUN yarn install",
BuildCmd: "",
StartCmd: "yarn start",
}
Expand All @@ -26,7 +26,7 @@ func TestTemplate_NBuildCmd_OutputDir_NSPA(t *testing.T) {
ctx := nodejs.TemplateContext{
NodeVersion: "18",

InstallCmd: "yarn install",
InstallCmd: "RUN yarn install",
BuildCmd: "",
StartCmd: "yarn start",
}
Expand All @@ -40,7 +40,7 @@ func TestTemplate_NBuildCmd_OutputDir_SPA(t *testing.T) {
ctx := nodejs.TemplateContext{
NodeVersion: "18",

InstallCmd: "yarn install",
InstallCmd: "RUN yarn install",
BuildCmd: "",
StartCmd: "yarn start",
}
Expand All @@ -54,7 +54,7 @@ func TestTemplate_BuildCmd_NOutputDir(t *testing.T) {
ctx := nodejs.TemplateContext{
NodeVersion: "18",

InstallCmd: "yarn install",
InstallCmd: "RUN yarn install",
BuildCmd: "yarn build",
StartCmd: "yarn start",
}
Expand All @@ -68,7 +68,7 @@ func TestTemplate_BuildCmd_OutputDir(t *testing.T) {
ctx := nodejs.TemplateContext{
NodeVersion: "18",

InstallCmd: "yarn install",
InstallCmd: "RUN yarn install",
BuildCmd: "yarn build",
StartCmd: "yarn start",
}
Expand All @@ -82,7 +82,7 @@ func TestTemplate_BuildCmd_Bun(t *testing.T) {
ctx := nodejs.TemplateContext{
Bun: true,
NodeVersion: "18",
InstallCmd: "bun install",
InstallCmd: "RUN bun install",
StartCmd: "bun start main.ts",
}

Expand Down
5 changes: 3 additions & 2 deletions internal/nodejs/templates/template.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ COPY --from=bun-runtime /usr/local/bin/bunx /usr/local/bin
{{- end }}

RUN corepack enable && corepack prepare --all
COPY . .

RUN {{ .InstallCmd }}
{{ .InstallCmd }}

COPY . .

# Build if we can build it
{{ if .BuildCmd }}RUN {{ .BuildCmd }}{{ end }}
Expand Down

0 comments on commit d169c3d

Please sign in to comment.