Und*_*ion 8 javascript npm package.json npm-scripts
我已将所有linting配置和相关的包/插件/预设(用于更漂亮,stylelint,eslint,commitlint)移到npm包中.然后,我在多个项目中使用此包,并将配置扩展或合并到项目的本地配置文件中,以确保一致性,并且无需安装和保持我的开发依赖项是最新的.
除了配置,我还有许多有用的npm脚本,它们运行连接并执行各种其他与开发相关的功能,例如:
"lint:prettier": "prettier 'src/**/*.{js,json}' --write",
"lint:eslint": "eslint 'src/**/*.js'",
"lint:compatibilityCheck": "eslint --print-config .eslintrc.js | eslint-config-prettier-check",
"lint": "npm run lint:compatibilityCheck && npm run lint:prettier && npm run lint:eslint"
Run Code Online (Sandbox Code Playgroud)
这些目前在我的所有项目中都是重复的,但我想将这些脚本与我的共享包一起分发,以便在一个位置定义它们.我该怎么做?
从npm 博客来看,似乎没有“直接方法”来公开 npm 包中的开发脚本。该博客文章建议创建 JavaScript 文件,使用shelljsmodule.js 运行您喜欢的脚本。
示例:假设你想暴露lint:prettier": "prettier 'src/**/*.{js,json}' --write"
将调用包装在 bin/lintprettier.js 中:
#! /usr/bin/env node
var shell = require("shelljs");
const path = require("path")
process.env.PATH += (path.delimiter + path.join(process.cwd(), 'node_modules', '.bin'));
shell.exec("prettier 'src/**/*.{js,json}' --write");
Run Code Online (Sandbox Code Playgroud)
然后将其添加到 package.json 中导出的控制台脚本中:
...
"bin": {
"lint-prettier": "bin/lintprettier.js"
}
...
Run Code Online (Sandbox Code Playgroud)
最后,您可以在项目中重用脚本:
"scripts": {
"build": "...",
"lint:prettier": "lint-prettier"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |