@nyce/config
v4.1.3
Published
Common configuration files used across projects.
Downloads
427
Readme
@nyce/config
https://www.npmjs.com/package/@nyce/config
Common and very opinionated configuration files for use across various projects. Import only what you need, extend to fill any gaps and keep some of that valuable time to focus on your code.
If you prefer a vanilla JavaScript/TypeScript experience, this probably isn't for you.
Installation
npm install @nyce/config
Requirements
The configuration files assume the following is enabled:
- ECMAScript modules; and
- The TC39 proposal 'Top-level await';
Add the following entry to (the top of) your package.json
file to use the Ecmascript Module Loader on your project files:
{
"type": "module"
}
Optional but recommended: Run NodeJS with the --experimental-specifier-resolution=node
flag forimports
without specific file extensions:
node --experimental-specifier-resolution=node dist/<YOUR-FILE-NAME-HERE>.js
Make sure to replace <YOUR-FILE-NAME-HERE>.js
with the file you want to run.
Usage
TSconfig
Create a file in your project root called tsconfig.json
and extend from the NYCE config.
{
"extends": "@nyce/config/tsconfig.json",
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
SWC compiler
Unlike TSC, SWC doesn't support config extension yet. ~~For now, you can load the config file with an absolute path to
node_modules
~~.~~Alternatively you could (automatically) copy the file over to your project.~~
UPDATE: In latest versions of SWC loading to an absolute NODE_MODULES path breaks the base url.
...Updated the section below to provide a working solution until SWC supports extending configuration files.
SHX is now a peerdependency, so make sure to install that with
bash
Add the following NPM scripts to copy the config post-install
Replace
pnpm
withnpm
in the postinstall script if you use npm instead of pnpm.
{
"postinstall": "pnpm copy-swc-config",
"copy-swc-config": "shx cp node_modules/@nyce/config/.swcrc .swcrc",
"build": "npx swc --config-file .swcrc ./src -d dist"
}
NPM build
script:
{
"build": "npx swc --config-file node_modules/@nyce/config/.swcrc ./src -d dist"
}
NPM start
script:
{
"start": "node --experimental-specifier-resolution=node dist/<YOUR-FILE-NAME-HERE>.js"
}
Make sure to replace <YOUR-FILE-NAME-HERE>.js
with the file you want to run.
jest
Create a file in your project root called jest.config.ts
with the following contents:
import type { Config } from "@jest/types";
import { pathsToModuleNameMapper } from "ts-jest";
export default async (): Promise<Config.InitialOptions> => {
const { compilerOptions } = await require("@nyce/config/tsconfig.json");
const nyceBaseOptions: Config.InitialOptions = await require("@nyce/config/jest.cjs");
return {
...nyceBaseOptions,
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: "<rootDir>/src/" }) ?? {},
};
};
NPM test
script:
{
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage"
}
Prettier and ESlint (package.json)
ESLINT forces you to use a prefix on your WHOLE DAMN NPM PACKAGE (seriously?!).
We serve multiple configuration files in this package so prefixing it with
eslint-config-
would be confusing. The ESlint configuration can be imported by using an absolute path.
{
"prettier": "@nyce/config/prettier",
"eslintConfig": {
"extends": "./node_modules/@nyce/config/eslint"
}
}
NPM lint
and format
scripts:
{
"lint": "eslint './src/**/*.ts' --ext .ts",
"lint:fix": "eslint './src/**/*.ts' --ext .ts --fix",
"format": "prettier --loglevel=warn --write src"
}
Nodemon (package.json)
Run Nodemon with a --config
parameter pointing to this packages' nodemon.json
file and specify a file to watch:
npx nodemon --config node_modules/@nyce/config/nodemon.json src/<YOUR-FILE-NAME-HERE>.ts"
Make sure to replace <YOUR-FILE-NAME-HERE>.ts
with the file you want to watch.
NPM watch
script:
{
"watch": "npx nodemon --config node_modules/@nyce/config/nodemon.json src/<YOUR-FILE-NAME-HERE>.ts"
}
Make sure to replace <YOUR-FILE-NAME-HERE>.ts
with the file you want to watch.