Skip to content

Commit 88f1de1

Browse files
committed
chore(server): build only necessary libs
1 parent be1956a commit 88f1de1

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
FROM node:24-alpine
22

3+
ENV NODE_ENV=production
4+
35
# Create app directory
46
WORKDIR /usr/src/app
5-
67
# Copy package.json and yarn.lock
78
COPY package.json yarn.lock ./
89

9-
# Install dependencies
10-
RUN yarn install --frozen-lockfile --cache-folder /tmp/.yarn-cache && rm -rf /tmp/.yarn-cache
10+
# Install only production dependencies
11+
RUN yarn --frozen-lockfile --production \
12+
&& yarn add express dotenv --production \
13+
&& yarn cache clean --force
1114

1215
# Copy the application code
13-
COPY . .
16+
COPY dist ./
1417

1518
# Expose the app's port
1619
EXPOSE 3000
1720

1821
# Start the application
19-
CMD ["yarn", "tsx", "server.ts"]
22+
CMD ["node", "server.js"]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-csfd-api",
3-
"version": "2.14.0",
3+
"version": "3.0.0-next.28",
44
"description": "ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)",
55
"author": "BART! <bart@bartweb.cz>",
66
"scripts": {
@@ -9,6 +9,7 @@
99
"build": "tsc && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && yarn barrels",
1010
"barrels": "barrelsby --delete -c barrels.json",
1111
"postbuild": "npm-prepare-dist -s postinstall -s prepare && yarn fix-paths",
12+
"build:server": "yarn build && yarn tsx scripts/prepare-server.js && yarn tsx scripts/create-version && yarn tsc -p tsconfig.server.json",
1213
"tsc": "tsc",
1314
"demo": "tsx demo",
1415
"lint": "eslint ./src/**/**/* --fix",

scripts/create-version.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
const pkgPath = path.resolve('package.json');
5+
// const outPath = path.resolve('version.js');
6+
const outPathDist = path.resolve('dist/version.js');
7+
8+
// Load package.json
9+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
10+
11+
// Create JS file
12+
const content = `// Auto-generated by create-version.js
13+
export default {
14+
name: ${JSON.stringify(pkg.name)},
15+
version: ${JSON.stringify(pkg.version)},
16+
homepage: ${JSON.stringify(pkg.homepage)}
17+
}`;
18+
19+
// fs.writeFileSync(outPath, content, 'utf-8');
20+
fs.writeFileSync(outPathDist, content, 'utf-8');
21+
22+
console.log('✅ Created', outPathDist);

scripts/prepare-server.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
const filePath = path.resolve(__dirname, '../server.ts');
5+
let content = fs.readFileSync(filePath, 'utf-8');
6+
7+
// Overwrite src → dist
8+
content = content
9+
.replace(/from '\.\/src'/g, "from './cjs/index.js'")
10+
.replace(/from '\.\/package.json'/g, "from './version.js'")
11+
.replace(/from '\.\/src\/dto\/global'/g, "from './types/dto/global'");
12+
13+
fs.writeFileSync(path.resolve(__dirname, '../dist/server.ts'), content);
14+
console.log('server.ts prepared for Docker build ✅');

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ app.get('/', (_, res) => {
110110
res.json({
111111
name: packageJson.name,
112112
version: packageJson.version,
113-
docs: packageJson.homepage,
113+
homepage: packageJson.homepage,
114114
links: Object.values(Endpoint)
115115
});
116116
});

tsconfig.server.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2022",
4+
"module": "ESNext",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"resolveJsonModule": true,
8+
"strictNullChecks": false,
9+
"strict": false,
10+
"skipLibCheck": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"outDir": "./dist",
13+
"declaration": false
14+
},
15+
"include": ["dist/server.ts"],
16+
"exclude": ["node_modules", "src"]
17+
}

0 commit comments

Comments
 (0)