Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ESM build with tsc #10

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ USER node
FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build
# Remove devDependencies
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile


###################
Expand All @@ -30,15 +32,15 @@ RUN pnpm run build
FROM alpine:3.19 As production

ENV PORT=3333
ENV NODE_VERSION 20.11.0-r0
ENV NPM_VERSION 10.2.5-r0
ENV NODE_VERSION 20.11.1-r0
# ENV NPM_VERSION 10.2.5-r0

# Make sure `"type": "module"` is set in package.json
COPY --from=build /app/package.json /app/package.json
# Production dependencies
# COPY --from=build /app/node_modules /app/node_modules
COPY --from=build /app/node_modules /app/node_modules
# Application
COPY --from=build /app/dist /app
COPY --from=build /app/dist/src /app

# COPY --from=prod-deps /app/node_modules /app/node_modules
# COPY --from=build /app/dist /app
Expand Down Expand Up @@ -76,4 +78,4 @@ RUN chown node .srp

USER node

CMD [ "node", "app/main.cjs" ]
CMD [ "node", "app/main.js" ]
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
"default": "./dist/main.cjs"
}
},
"imports": {
"@/*": "./src/*"
},
"scripts": {
"start": "tsx src/main.ts",
"dev": "tsx watch src/main.ts",
"test:coverage": "vitest run --coverage",
"test": "vitest",
"test:ci": "vitest run",
"build": "esbuild src/main.ts --bundle --platform=node --target=node16 --format=cjs --outfile=dist/main.cjs --minify",
"build": "tsc && tsc-alias -p tsconfig.json",
"format": "prettier -w src",
"lint": "tsc --noEmit && eslint ./src && prettier -c src",
"lint:fix": "eslint --fix ./src && prettier -w src",
Expand All @@ -38,6 +41,7 @@
"eslint": "^8.56.0",
"node-mocks-http": "^1.14.1",
"prettier": "^3.1.1",
"tsc-alias": "^1.8.8",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"vitest": "^1.1.3"
Expand Down
84 changes: 84 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './cache';
export * from './cache.js';
33 changes: 17 additions & 16 deletions src/database/config-rule.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { logger } from '@/logger';
import type { SpyConfigRuleEntityAzureTable } from '../entities/spy-config-rule.entity';
import { AzureTable } from './azure-table';
import { logger } from '@/logger/index.js';
import type { SpyConfigRuleEntityAzureTable } from '../entities/spy-config-rule.entity.js';
import { AzureTable } from './azure-table.js';
import invariant from 'tiny-invariant';
import { TableClient } from '@azure/data-tables';
import { env } from '@/environment';
import { env } from '@/environment/index.js';
import { ODataExpression } from 'ts-odata-client';
import { trimStartAndEndSlash } from '@/utils';
import { trimStartAndEndSlash } from '@/utils/index.js';

export class SpyConfigRuleService {
private oDataExpresion = ODataExpression.forV4<SpyConfigRuleEntityAzureTable>();
Expand All @@ -15,17 +15,18 @@ export class SpyConfigRuleService {
async listAllMatchUpstreamUrlRules(upstreamUrl: string) {
const url = trimStartAndEndSlash(upstreamUrl);
await this.tableClient.createTable();
return this.tableClient.list(
this.oDataExpresion
.filter(p =>
p.upstreamUrl
.$equals('')
.or(p.upstreamUrl.$equals(null))
.or(p.upstreamUrl.$equals(url))
.or(p.upstreamUrl.$equals(`${url}/`))
)
.build()
);
const filterQuery = this.oDataExpresion
.filter(p =>
p.upstreamUrl
.$equals('')
.or(p.upstreamUrl.$equals(url))
.or(p.upstreamUrl.$equals(`${url}/`))
)
.build();
logger.debug(`Filter query: ${JSON.stringify(filterQuery, null, 2)}`);
return this.tableClient.list({
filter: filterQuery.filter,
});
}

async insertRule(rule: Omit<SpyConfigRuleEntityAzureTable, 'partitionKey' | 'rowKey'>) {
Expand Down
4 changes: 2 additions & 2 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './azure-table';
export * from './config-rule.service';
export * from './azure-table.js';
export * from './config-rule.service.js';
2 changes: 1 addition & 1 deletion src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './spy-config-rule.entity';
export * from './spy-config-rule.entity.js';
2 changes: 1 addition & 1 deletion src/entities/spy-config-rule.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AzureTableEntityBase } from '../database/azure-table';
import type { AzureTableEntityBase } from '../database/azure-table.js';

export interface SpyConfigRuleEntity {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/environment/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dotenv/config';
import { environmentSchema } from '../environment/env.schema';
import { extractErorMessage } from '@/utils';
import { environmentSchema } from '../environment/env.schema.js';
import { extractErorMessage } from '@/utils/index.js';
import { zodParser } from '@thaitype/record-parser/zod';
/**
* Get environment variables
Expand Down
2 changes: 1 addition & 1 deletion src/environment/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './env';
export * from './env.js';
2 changes: 1 addition & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './logger';
export * from './logger.js';
6 changes: 3 additions & 3 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { env } from '@/environment';
import pino from 'pino';
import pinoHttp from 'pino-http';
import { env } from '@/environment/index.js';
import { pino } from 'pino';
import { pinoHttp } from 'pino-http';
import fs from 'fs';

const logDir = '.srp/logs';
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { server } from './server';
import { extractErorMessage } from './utils/utils';
import { server } from './server.js';
import { extractErorMessage } from './utils/utils.js';

server().catch(error => {
console.error(extractErorMessage(error));
Expand Down
Loading
Loading