记第一次发布npm包经历,smart-import

故事背景

前情提要自动 Import 工具,前端打字员的自我救赎 javascript

github: smart-importhtml

smart-import 的功能

根据配置文件,在目标文件中自动导入规定目录下自定义模块,并监听规定目录下文件的变更,自动更新前端

尚在测试中vue

smart-import 的使用

安装工具java

npm install smart-import -g

编写配置文件smart-import.jsonnode

{
    "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 named foo, and a user installs it, and then does require("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 into prefix/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...

https://javascriptplayground....

https://www.sitepoint.com/jav...

相关文章
相关标签/搜索