Skip to content

Commit a2e2563

Browse files
committed
feat: use esm
BREAKING CHANGE: move code base to esm Signed-off-by: Vitor Hugo Salgado <[email protected]>
1 parent 5750f5a commit a2e2563

16 files changed

+95
-56
lines changed

.dockerignore

-1
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,4 @@ dist
119119
.github/
120120

121121
docs/
122-
dist/
123122
tools/

.eslintrc.cjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
extends: ['plugin:@typescript-eslint/recommended'],
66
env: {
77
jest: true,
8-
node: true
8+
node: true,
99
},
1010
rules: {
1111
'no-console': 'error',
@@ -14,6 +14,6 @@ module.exports = {
1414
'@typescript-eslint/no-useless-constructor': ['error'],
1515
'@typescript-eslint/no-inferrable-types': ['off'],
1616

17-
'import/extensions': ['error', 'ignorePackages', { js: 'always', jsx: 'never', ts: 'never', tsx: 'never' }]
18-
}
17+
'import/extensions': ['error', 'ignorePackages', { js: 'always', jsx: 'never', ts: 'never', tsx: 'never' }],
18+
},
1919
}

.prettierrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"trailingComma": "none",
2+
"trailingComma": "all",
33
"printWidth": 120,
44
"singleQuote": true,
55
"arrowParens": "avoid",

.versionrc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ module.exports = {
1010
{ type: 'refactor', section: 'Refactor', hidden: false },
1111
{ type: 'perf', section: 'Perf', hidden: false },
1212
{ type: 'test', section: 'Refactor', hidden: false },
13-
{ type: 'build', section: 'Build', hidden: false }
14-
]
13+
{ type: 'build', section: 'Build', hidden: false },
14+
],
1515
}

Makefile

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@ SHELL := /bin/bash
55
help:
66
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
77

8-
up: ## Run a development environment with Docker Compose.
9-
@docker-compose -f ./deployments/dev/docker-compose.yml up
8+
up: ## Run a local development environment with Docker Compose.
9+
@docker-compose -f ./deployments/dev/docker-compose.yml up --build --force-recreate
1010

11-
down: ## Stop Docker Compose development environment.
11+
recreate: ## Recreate and run development docker compose
12+
@docker-compose -f ./deployments/dev/docker-compose.yml up --build --force-recreate
13+
14+
down: ## Stop Docker Compose local development environment.
1215
@docker-compose -f ./deployments/dev/docker-compose.yml down
1316

14-
clean: ## Clean Docker Compose development environment.
17+
clean: ## Clean Docker Compose local development environment.
1518
@docker-compose -f ./deployments/dev/docker-compose.yml down --remove-orphans --volumes
1619

1720
.PHONY: test
18-
test:
21+
test: ## Run tests
1922
@npm test
2023

21-
fmt: # Format code
24+
fmt: ## Format code
2225
@npm run format
2326

24-
lint: # Run static analysis
27+
lint: ## Run static analysis
2528
@npm run lint
2629

27-
check: # Run all checks for this project
30+
check: ## Run all checks for this project
2831
@npm run format:check
2932
@npm run lint
3033
@npm run test

build/docker/Dockerfile.dev

-3
This file was deleted.

cmd/create-nodejs-ts/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env node
22

3-
const Path = require('path')
4-
const FsExt = require('fs-extra')
3+
import FsExt from 'fs-extra'
4+
import Path, { dirname } from 'path'
5+
import { fileURLToPath } from 'url'
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url))
58

69
const paramOr = (map, arg, def) => map.get(arg) || def
710
const makePath = (...p) => Path.join(...p)
@@ -38,7 +41,7 @@ const Ignores = [
3841
'Makefile',
3942
'package.json',
4043
'package-lock.json',
41-
'yarn.lock'
44+
'yarn.lock',
4245
]
4346

4447
const NoDeps = ['fs-extra', 'standard-release']
@@ -48,7 +51,7 @@ const Templates = [
4851
{ file: 'README.md', copyTo: 'README.md' },
4952
{ file: '.gitignore.husky', copyTo: '.husky/.gitignore' },
5053
{ file: '.gitignore.root', copyTo: '.gitignore' },
51-
{ file: '.dockerignore.root', copyTo: '.dockerignore' }
54+
{ file: '.dockerignore.root', copyTo: '.dockerignore' },
5255
]
5356

5457
const PkgFieldsToKeep = ['scripts', 'dependencies', 'devDependencies']
@@ -86,7 +89,7 @@ function main() {
8689
Summary:
8790
Destination: ${destination}
8891
App: ${app}
89-
`
92+
`,
9093
)
9194

9295
console.log('Copying Project Files ...')
@@ -102,7 +105,7 @@ App: ${app}
102105
const pkg = FsExt.readJsonSync(makePath(source, 'package.json'))
103106
const newPkg = {
104107
name: app,
105-
main: 'dist/index.js'
108+
main: 'dist/index.js',
106109
}
107110

108111
PkgFieldsToKeep.forEach(field => {
@@ -128,4 +131,4 @@ App: ${app}
128131
return Promise.resolve()
129132
}
130133

131-
main().catch(console.error)
134+
await main()

deployments/dev/docker-compose.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ version: '3.8'
22

33
services:
44
dev-app:
5-
build:
6-
context: ./
7-
dockerfile: ../../build/docker/Dockerfile.dev
8-
container_name: nodejs-ts.dev-app
5+
image: node:16.14.2
6+
working_dir: /app
97
volumes:
108
- ../../:/app
9+
- ../../node_modules:/app/node_modules
10+
- ../../dist:/app/dist
11+
command: bash -c "npm i --quiet --ignore-scripts && npm run start:dev"
1112
ports:
12-
- 8080:8080
13+
- "8080:8080"
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import 'dotenv/config'
2-
import type { Config as JestConfig } from '@jest/types'
32

4-
const config: JestConfig.InitialOptions = {
3+
export default {
54
verbose: true,
65
collectCoverage: false,
76
restoreMocks: true,
8-
transform: { '^.+\\.tsx?$': 'ts-jest' },
7+
transform: {},
8+
preset: 'ts-jest/presets/default-esm',
9+
extensionsToTreatAsEsm: ['.ts'],
910
globals: {
1011
'ts-jest': {
11-
tsconfig: './tsconfig.test.json'
12-
}
12+
tsconfig: './tsconfig.test.json',
13+
useESM: true,
14+
},
1315
},
1416
collectCoverageFrom: ['**/src/*/**/*.ts', '!**/__fixtures__/**', '!**/__tests__/**'],
1517
coveragePathIgnorePatterns: ['<rootDir>/dist/', '/node_modules/', '<rootDir>/scripts', '<rootDir>/tools'],
@@ -19,9 +21,7 @@ const config: JestConfig.InitialOptions = {
1921
branches: 10,
2022
functions: 10,
2123
lines: 10,
22-
statements: 10
23-
}
24-
}
24+
statements: 10,
25+
},
26+
},
2527
}
26-
27-
export default config

lint-staged.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = {
44
'*.{js,jsx,ts,tsx,md,json}': 'prettier --write --ignore-unknown',
5-
'*.ts': 'eslint --ext .ts .'
5+
'*.ts': 'eslint --ext .ts .',
66
}

package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
"name": "create-nodejs-ts",
33
"version": "2.1.0",
44
"description": "NodeJS Starter Project Kit",
5-
"private": false,
5+
"type": "module",
66
"main": "dist/index.js",
77
"types": "dist/index.d.ts",
8+
"private": false,
89
"bin": {
910
"create-nodejs-ts": "cmd/create-nodejs-ts/index.js"
1011
},
1112
"scripts": {
12-
"start": "nodemon --ext js,ts,json,env src/index.ts",
13+
"start": "node dist/index.js",
14+
"start:dev": "nodemon --ext js,ts,json,env --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' src/index.ts",
1315
"prepare": "husky install",
1416
"build": "tsc --project tsconfig.build.json",
1517
"release": "standard-version",
16-
"test": "jest --detectOpenHandles --logHeapUsage",
18+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles",
1719
"test:coverage": "npm run test -- --coverage",
1820
"test:ci": "npm run test -- --colors --coverage --ci",
1921
"lint": "eslint --ext .ts,.js .",
20-
"format": "prettier \"src/**\" --write --ignore-unknown",
21-
"format:check": "prettier --check \"src/**\" --ignore-unknown"
22-
},
23-
"engines": {
24-
"node": ">=16"
22+
"format": "prettier './**' --write --ignore-unknown",
23+
"format:check": "prettier './**' --ignore-unknown --check"
2524
},
2625
"author": {
2726
"name": "Vitor Hugo Salgado",
@@ -52,11 +51,13 @@
5251
"devDependencies": {
5352
"@commitlint/cli": "^16.2.3",
5453
"@commitlint/config-conventional": "^16.2.1",
54+
"@jest/globals": "^27.5.1",
5555
"@jest/types": "^27.5.1",
5656
"@types/jest": "^27.4.1",
5757
"@types/node": "^17.0.23",
5858
"@typescript-eslint/eslint-plugin": "^5.16.0",
5959
"@typescript-eslint/parser": "^5.16.0",
60+
"cross-env": "^7.0.3",
6061
"eslint": "^8.12.0",
6162
"eslint-plugin-import": "^2.25.4",
6263
"eslint-plugin-node": "^11.1.0",

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const Config = {
2-
port: parseInt(process.env.PORT || '8080')
2+
port: parseInt(process.env.PORT || '8080'),
33
}

src/index.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55

66
import { spawn } from 'child_process'
7-
import Path from 'path'
7+
import Path, { dirname } from 'path'
8+
import { fileURLToPath } from 'url'
89

910
describe('Example Test', function () {
1011
it('should init without errors', async function () {
1112
process.env.PORT = '0'
1213

13-
const index = Path.resolve(__dirname, 'index.ts')
14+
const dir = dirname(fileURLToPath(import.meta.url))
15+
const index = Path.resolve(dir, 'index.ts')
1416
const tsNodeExe = process.platform === 'win32' ? './node_modules/.bin/ts-node.cmd' : './node_modules/.bin/ts-node'
1517
const proc = await spawn(tsNodeExe, [index])
1618

src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import { createServer, IncomingMessage, ServerResponse } from 'http'
1010
import { Config } from './config.js'
1111

1212
const requestListener = (request: IncomingMessage, response: ServerResponse) => {
13-
response.writeHead(200)
14-
response.end('Hello, World!')
13+
response.setHeader('content-type', 'text/plain;charset=utf8')
14+
response.writeHead(200, 'OK')
15+
response.end('Olá, Hola, Hello!')
1516
}
1617

1718
const server = createServer(requestListener)
1819

1920
server.listen(Config.port)
21+
22+
// eslint-disable-next-line no-console
23+
console.log(`Listening on port: ${Config.port}`)

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"module": "CommonJS",
5-
"moduleResolution": "node",
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
66
"allowSyntheticDefaultImports": true,
77
"esModuleInterop": true,
88
"importHelpers": true,

0 commit comments

Comments
 (0)