前情提要:自动 Import 工具,前端打字员的自我救赎 javascript
github: smart-importhtml
根据配置文件,在目标文件中自动导入规定目录下自定义模块,并监听规定目录下文件的变更,自动更新前端
尚在测试中vue
安装工具java
npm install smart-import -g
编写配置文件smart-import.json
node
{ "extname": ".vue", "from": "demo/pages", "to": "demo/router/index.js", "template": "const moduleName = () => import(modulePath)", "ignored": [ "demo/pages/pageA.vue", "demo/pages/**/*.js" ] }
extname:须要自动导入的模块的后缀名git
from:自动导入的模块的来源目录github
to:目标文件npm
template:导入方式的模版json
ignored:须要忽略的模块
启动工具
在命令行输入
simport
smart-import 的诞生
smart-import做为命令行工具,和日常写网站仍是有些不一样的。
一样的部分,github建仓库,npm init
经过npm init
会生成package.json
文件,其中main字段的做用在于,若是你的代码最终做为一个模块被其余人import/require,那么这个文件就是这个模块的入口文件,能够参考node加载模块的机智
摘自npm官方文档
The main field is a module ID that is the primary entry point to your program. That is, if your package is namedfoo
, and a user installs it, and then doesrequire("foo")
, then your main module's exports object will be returned.This should be a module ID relative to the root of your package folder.
For most modules, it makes the most sense to have a main script and often not much else.
因为smart-import是一个命令行工具,并不会被其余人import/require,因此main字段能够忽略;而要注意的是bin字段
"bin": { "simport": "./bin/index.js" },
摘自npm官方文档
A lot of packages have one or more executable files that they'd like to install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the "npm" executable.)To use this, supply a
bin
field in your package.json which is a map of command name to local file name. On install, npm will symlink that file intoprefix/bin
for global installs, or./node_modules/.bin/
for local installs.
简单来讲,就是将你的脚本放到环境变量中
而在你的脚本的第一行务必要加上
#!/usr/bin/env node
用于告诉计算机用 node 来运行这段脚本
在测试本身的脚本以前要把运行
npm install -g
把本身的脚本连接到环境变量中,否则会被告知该命令不存在
smart-import 的发布
先要有npm的帐号
而后给package添件帐号
npm adduser
以后能够经过npm whoami
来核实本身的帐号信息
最后就是
npm publish
版本更新
npm version patch npm publish
https://docs.npmjs.com/files/...
https://developer.atlassian.c...