From ab67c0aa2b7e608398617063ed478693535504c8 Mon Sep 17 00:00:00 2001 From: Hamdi LAADHARI Date: Tue, 7 Jan 2025 19:50:46 +0100 Subject: [PATCH] update build script and refactor code for ES module compatibility --- package.json | 2 +- src/index.ts | 12 ++++++------ src/lib/greeter.ts | 6 ++++-- tsconfig.json | 9 +++++---- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index db88ad8..2aed1b7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "tsc", + "build": "rm -rf dist/* && tsc", "start": "node dist/index.js", "dev": "ts-node src/index.ts", "lint": "eslint .", diff --git a/src/index.ts b/src/index.ts index e26b0fb..7395b8e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ -import { greet } from './lib/greeter' -import { Person } from './types' +import { Person } from './types.js'; +import { greet } from './lib/greeter.js'; const person: Person = { - firstName: 'Jane', - lastName: 'Doe', -} + firstName: 'John', + lastName: 'Doe' +}; -console.log(greet(`${person.firstName} ${person.lastName}`)) +console.log(greet(person)); diff --git a/src/lib/greeter.ts b/src/lib/greeter.ts index d50420c..cf40297 100644 --- a/src/lib/greeter.ts +++ b/src/lib/greeter.ts @@ -1,3 +1,5 @@ -export function greet(name: string): string { - return `Hello, ${name}!` +import { Person } from '../types.js'; + +export function greet(person: Person): string { + return `Hello ${person.firstName} ${person.lastName}!`; } diff --git a/tsconfig.json b/tsconfig.json index 362d691..c6d9386 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,10 +25,10 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs" /* Specify what module code is generated. */, + "module": "NodeNext" /* Specify what module code is generated. */, "outDir": "./dist" /* Specify an output folder for all emitted files. */, - // "rootDir": "./", /* Specify the root folder within your source files. */ - // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + "rootDir": "./src", /* Specify the root folder within your source files. */ + "moduleResolution": "NodeNext", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ @@ -108,5 +108,6 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } + }, + "include": ["src/**/*"] }