Skip to content

Commit

Permalink
Serverless optimization (#5)
Browse files Browse the repository at this point in the history
* build:webpack

* tsconfig.json serverless

* Dockerfile
  • Loading branch information
vmozharov authored Oct 22, 2023
1 parent 9a2a82b commit b50541e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
60 changes: 60 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Docker
Dockerfile
.dockerignore

# Compiled output
/dist
node_modules
error-graph.json

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
**/.DS_Store

# Tests
/coverage
/.nyc_output
/test
**/*.spec.ts
**/*.test.ts
**/*.spec.tsx
**/*.test.tsx

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# Local settings
.env*.local
.env.test
.eslintrc.js
.prettierrc


# Git
.github
.gitignore
.git

# Other
README.md
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Development stage
FROM amd64/node:18.18-alpine As development

RUN mkdir -p /usr/src/app/node_modules && chown -R node:node /usr/src/app
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./

USER node

RUN npm ci

COPY --chown=node:node . .

## Build stage
FROM amd64/node:18.18-alpine As build

RUN mkdir -p /usr/src/app/node_modules && chown -R node:node /usr/src/app
WORKDIR /usr/src/app

COPY --chown=node:node package*.json ./

# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development image
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules

COPY --chown=node:node . .

USER node

RUN npm run build:webpack

RUN npm ci --omit=dev && npm cache clean --force


## Production stage
FROM amd64/node:18.18-alpine As production

RUN mkdir -p /usr/src/app/node_modules && chown -R node:node /usr/src/app
WORKDIR /usr/src/app

# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
COPY --chown=node:node --from=build /usr/src/app/.env ./

USER node

CMD ["node", "dist/main.js"]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"build:webpack": "nest build --webpack",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"format:check": "prettier --check \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
Expand Down
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {configModule, configProviders} from './config'
@Module({
imports: [
configModule,
DevtoolsModule.register({http: process.env[NODE_ENV.name] === NODE_ENV.options.DEVELOPMENT}),
UsersModule,
...(process.env[NODE_ENV.name] === NODE_ENV.options.DEVELOPMENT
? [DevtoolsModule.register({http: true})]
: []),
],
controllers: [AppController],
providers: [
Expand Down
11 changes: 9 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
// For common server
// "module": "commonjs",

// For serverless
// https://docs.nestjs.com/fundamentals/lazy-loading-modules#getting-started
"module": "esnext",
"moduleResolution": "node",

"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
Expand All @@ -16,6 +23,6 @@
"noImplicitAny": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
}
}

0 comments on commit b50541e

Please sign in to comment.