node 项目常见配置文件

来源:互联网 发布:mac最新系统下载地址 编辑:程序博客网 时间:2024/06/06 00:11

每个配置文件下都附带有我的配置。
项目目录结构:

根目录├─ config├─ lib├─ middlewares├─ models├─ node_modules├─ public├─ routes├─ test├─ views├─ .editorconfig├─ .eslintignore├─ .eslintrc├─ .gitignore├─ .npmignore├─ index.js├─ package.json└─ README.md

.gitignore

告诉git在做代码管理时哪些文件应该被“忽略”。比如项目在本地开发时产生的临时文件/日志,本地安装的node依赖模块产生的node_modules目录等,都是.gitignore里的“常客”

# Logslogs*.lognpm-debug.log*···

.editconfig

是EditConfig的配置文件,EditConfig是一套用于统一代码风格的解决方案,帮助开发者在不同编辑器和IDE间定义和维护一致的代码风格。此外editconfig还有一些结合编辑器的插件(基本覆盖主流编辑器),这些插件可以让编辑器读取配置文件并依此格式化代码。

# EditorConfig is awesome: http://EditorConfig.org# top-most EditorConfig fileroot = true# Unix-style newlines with a newline ending every file[*]end_of_line = lfinsert_final_newline = truecharset = utf-8trim_trailing_whitespace = truemax_line_length = 120indent_style = spaceindent_size = 4[*.md]trim_trailing_whitespace = false[*.json]indent_style = spaceindent_size = 4

.npmignore

用于Node.js模块发布时,如果不想把源码下某些文件发布到npm上,可以配置该文件即可。其配置方式类似文章开头介绍的.gitignore配置文件。如果没有.npmignore,发布时npm会搜索当前目录下有没有.gitignore配置文件,并把.gitignore配置看做是.npmignore配置,如果.npmignore和.gitignore都存在,.npmignore配置内容会覆盖.gitignore内容。

node_modules# idea.idea/*.ipr*.iml*.iws/test

.eslintrc

主流的Javascript代码格式检查工具,可以校验 js 语法,或者设置一些规则如换行符、分号等。ESLint 附带有大量的规则。你可以使用注释或配置文件修改你项目中要使用哪些规则。改变一个规则设置,你必须设置规则 ID 等于这些值之一:
1. “off” 或 0 - 关闭规则
2. “warn” 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
3. “error” 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)
具体参考ESLint配置

{    "extends": "eslint:recommended",    "env": {        "amd": true,        "es6": true,        "node": true,        "mocha": true    },    "rules": {        "comma-dangle": 2,        "quotes": [1, "single"],        "no-undef": 1,        "global-strict": 0,        "semi": [1, "always"],        "no-extra-semi": 1,        "no-unused-vars": 0,        "no-mixed-spaces-and-tabs": 2,        "curly": [1],        "indent": [0, 4, {            "SwitchCase": 1        }],        "no-underscore-dangle": 0,        "no-console": 0,        "no-trailing-spaces": [1, {            "skipBlankLines": true        }],        "no-unreachable": 1,        "no-alert": 0,        "linebreak-style": [2, "unix"]    }}

.eslintrcignore

告诉 ESLint 去忽略特定的文件和目录。.eslintignore 文件是一个纯文本文件,其中的每一行都是一个 glob 模式表明哪些路径应该忽略检测当 ESLint 运行时,在确定哪些文件要检测之前,它会在当前工作目录中查找一个 .eslintignore 文件。如果发现了这个文件,当遍历目录时,将会应用这些偏好设置。一次只有一个 .eslintignore 文件会被使用,所以,不是当前工作目录下的 .eslintignore 文件将不会被用到。常见规则:
1. 以 # 开头的行被当作注释,不影响忽略模式。
2. 路径是相对于 .eslintignore 的位置或当前工作目录。这也会影响通过 –ignore-pattern传递的路径。
3. 忽略模式同 .gitignore 规范
4. 以 ! 开头的行是否定模式,它将会重新包含一个之前被忽略的模式。
5. 总是忽略 /node_modules/* 和 /bower_components/* 中的文件

# /node_modules/* and /bower_components/* ignored by defaultbuild/*!build/index.jsviews/**public/**dist/**test/****/*.map**/*.css**/*.html**/*.md**/*.svg**/*.png**/*.iml**/*.ejs**/*.txt**/*.json

最后放一张 package.json

{  "name": "node",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "lintfix": "eslint --fix src/",    "start": "supervisor --harmony index",    "publish:snapshot": "npm publish --registry=url",    "publish:release": "npm publish --registry=url"  },  "author": "",  "license": "ISC",  "dependencies": {    "config-lite": "^2.1.0",    "connect-flash": "^0.1.1",    "connect-mongo": "^1.3.2",    "cors": "^2.8.4",    "ejs": "^2.5.7",    "express": "^4.15.4",    "express-formidable": "^1.0.0",    "express-session": "^1.15.5",    "express-winston": "^2.4.0",    "eslint": "^4.5.0",    "marked": "^0.3.6",    "moment": "^2.18.1",    "mongodb": "^2.2.31",    "mongolass": "^3.1.5",    "mongoose": "^4.11.10",    "objectid-to-timestamp": "^1.3.0",    "sha1": "^1.1.1",    "winston": "^2.3.1"  }}

跨平台开发的时候,可以执行 npm run lintfix ,他会自动按你写好的规则格式化整个项目下的 js 文件。

原创粉丝点击