Skip to content

Commit

Permalink
Add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pelicularities committed Apr 17, 2024
1 parent f847f59 commit 5242460
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 20 deletions.
Empty file added .prettierrc
Empty file.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# typescript-starter

This is a starter repo for TypeScript, containing everything needed to write, test, and build TypeScript for a Node.js runtime. It's intended as a simple command-line playground for TypeScript.

## How to use

1. `npm install`
2. To run tests: `npm run test`
3. To build and bundle: `npm run build`
4. To run the bundled
4. To run the bundled

## What happens when you run `npm run build`?

The `npm run build` script does two things:

1. Run `tsc`, transpiling all non-excluded files into JS and outputting them into the `build` folder
2. Run `esbuild` using `build/index.js` as the entry point and `node` as the target platform, bundling the result into `./out.js`.

## Why use esbuild?

I prefer using [ES modules](https://nodejs.org/api/esm.html#modules-ecmascript-modules) over [CommonJS modules](https://nodejs.org/api/modules.html#modules-commonjs-modules):

```ts
Expand All @@ -36,8 +41,9 @@ To work around this, I use TypeScript with the `bundler` module resolution confi
tl;dr: the sole reason for using `esbuild` in this repo is to allow me to use ESM instead of CJS.

## To-do list

- [x] Add TypeScript
- [x] Add `jest`
- [x] Add `esbuild`
- [x] Add `eslint`
- [ ] Add `prettier`
- [x] Add `prettier`
2 changes: 1 addition & 1 deletion addNumbers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import addNumbers from "./addNumbers";

it('should add 2 and 3 correctly', () => {
it("should add 2 and 3 correctly", () => {
expect(addNumbers(2, 3)).toBe(5);
});
2 changes: 1 addition & 1 deletion addNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const addNumbers = (firstNumber: number, secondNumber: number): number => {
return firstNumber + secondNumber;
};

export default addNumbers;
export default addNumbers;
7 changes: 4 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

import eslintConfigPrettier from "eslint-config-prettier";

export default [
{languageOptions: { globals: globals.browser }},
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
eslintConfigPrettier,
];
5 changes: 1 addition & 4 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const config = {
clearMocks: true,
coverageProvider: "v8",
preset: "ts-jest",
testPathIgnorePatterns: [
"/node_modules/",
"/dist/"
]
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
};

export default config;
29 changes: 29 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "jest",
"build": "tsc; esbuild build/index.js --bundle --platform=node --target=node20 --outfile=out.js",
"lint": "eslint *.ts"
"lint": "eslint *.ts; prettier --write ."
},
"author": "",
"license": "ISC",
Expand All @@ -16,7 +16,9 @@
"@types/jest": "^29.5.12",
"esbuild": "0.20.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"globals": "^15.0.0",
"prettier": "3.2.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.5",
"typescript-eslint": "^7.7.0"
Expand Down
16 changes: 8 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "esnext", /* Specify what module code is generated. */
"moduleResolution": "bundler", /* Specify how TypeScript looks up a file from a given module specifier. */
"outDir": "./build", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"module": "esnext" /* Specify what module code is generated. */,
"moduleResolution": "bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
"outDir": "./build" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["*.test.ts", "*.config.ts"]
}

0 comments on commit 5242460

Please sign in to comment.