mwts
v2.0.4
Published
MidwayJS TypeScript Style
Readme
mwts
MidwayJS TypeScript Style
mwts is Alibaba MidwayJS TypeScript style guide, and the configuration for our formatter, linter, and automatic code fixer. No lint rules to edit, no configuration to update, no more bike shedding over syntax.
To borrow from standardjs:
- No configuration. The easiest way to enforce consistent style in your project. Just drop it in.
- Automatically format code. Just run
mwts fixand say goodbye to messy or inconsistent code. - Catch style issues & programmer errors early. Save precious code review time by eliminating back-and-forth between reviewer & contributor.
- Opinionated, but not to a fault. We recommend you use the default configuration, but if you need to customize compiler or linter config, you can.
Under the covers, we use eslint to enforce the style guide and provide automated fixes, and prettier to re-format code.
Getting Started
The easiest way to get started is to run:
npx mwts initUse --formatter stylistic to opt into ESLint + Stylistic instead of the default Prettier + ESLint.
Use --formatter biome to use Biome as formatter while keeping mwts lint/check commands.
For legacy mwts 1.x projects, run:
npx mwts migrateHow it works
When you run the npx mwts init command, it's going to do a few things for you:
- Adds an opinionated
tsconfig.jsonfile to your project that uses the MidwayJS TypeScript Style. - Adds the necessary devDependencies to your
package.json. - Adds scripts to your
package.json:lint/check: Lints and checks for formatting problems.fix: Automatically fixes formatting and linting problems (if possible).clean: Removes output files.build: Compiles the source code using TypeScript compiler.pretest,posttestandprepare: convenience integrations.
- If a source folder is not already present it will add a default template project.
Individual files
The commands above will all run in the scope of the current folder. Some commands can be run on individual files:
mwts check index.ts
mwts check one.ts two.ts three.ts
mwts check *.tsmwts lint/check/fix all support file arguments.
If you run them without mwts init, mwts will fall back to its built-in default ESLint config.
Built-in formatter modes
mwts has built-in formatter modes:
- default:
Prettier + ESLint --formatter stylistic:ESLint + Stylistic--formatter biome:Biome + ESLint(Biome handles formatting)
Custom formatter example
If you want a formatter outside built-in modes, install and wire it in your project yourself.
Example:
npm i -D dprint{
"scripts": {
"format": "dprint fmt",
"check:format": "dprint check"
}
}You can keep mwts lint/check/fix for linting and use another formatter only for formatting.
Working with eslint
Under the covers, we use eslint to enforce the style guide and provide automated fixes, and prettier to re-format code. To use the shared eslint configuration, create an eslint.config.js in your project directory, and extend the shared config:
module.exports = [
...require('mwts'),
];If you don't want to use the mwts CLI, you can drop down to using the module as a basic eslint config, and just use the eslint cli:
$ eslint --fixThis opens the ability to use the vast eslint ecosystem including custom rules, and tools like the VSCode plugin for eslint:
- https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
Badge
Show your love for mwts and include a badge!
[](https://github.com/midwayjs/mwts)Supported Node.js Versions
mwts 2.x requires Node.js 20+.
Migrating from mwts 1.x to 2.x
- Upgrade runtime/tooling baseline:
- Node.js >= 20
- TypeScript >= 5
mwts initnow generateseslint.config.js(flat config), instead of.eslintrc.json/.eslintignore.- Default formatter mode stays
Prettier + ESLint. - Optional formatter mode:
mwts init --formatter stylistic. - Optional formatter mode:
mwts init --formatter biome. mwts lint/check/fixsupport per-file arguments and can run withoutmwts initby falling back to mwts built-in config.- You can run
mwts migrateonce to generate a flateslint.config.jsfrom legacy.eslintrc.json.
License
Made with ❤️ by the Alibaba Node.js team.
A derived project from the awesome gts.
