资讯专栏INFORMATION COLUMN

npm包发布记录

Honwhy / 2731人阅读

摘要:下雪了,在家闲着,不如写一个包发布。简单的包的发布网上有很多教程,我就不记录了。这里记录下,一个复杂的包发布,复杂指的构建环境复杂。同时发布格式的版本以供外部调用。

下雪了,在家闲着,不如写一个npm 包发布。简单的 npm 包的发布网上有很多教程,我就不记录了。这里记录下,一个复杂的 npm 包发布,复杂指的构建环境复杂。

整个工程使用 rollup 来构建,其中会引进 babel 来转译 ES6,利用 Eslint 来规范代码的书写风格,最后代码的发布会经过 terser 压缩。同时发布 umd、es 格式的版本以供外部调用。

完整目录结构如下:

下面是整个过程的记录

一、初始化工程

yarn init -y

初始化后,修改 package.json 内容,如 name(项目名),description(项目描述)等信息。

二、安装 rollup

yarn add rollup@1.0.0 --dev

三、创建配置文件 rollup.config.js

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  }
}

四、安装 babel

yarn add rollup-plugin-babel@4.2.0 @babel/core@7.2.2 @babel/preset-env@7.2.3 --dev

五、配置 babel

1、创建配置文件 .babelrc

{
  "presets": [
   [
    "@babel/preset-env",
    {
     "modules": false
    }
   ]
  ]
}

2、与 rollup 集成,在 rollup.config.js 中配置 plugins

import babel from "rollup-plugin-babel"

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  },
  plugins: [
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    })
  ]
}

六、安装 eslint

yarn add eslint@5.11.1

七、配置 eslint

1、生成 eslint 配置

.
ode_modules.bineslint --init

2、与 rollup 集成,在 rollup.config.js 中配置 plugins

import babel from "rollup-plugin-babel"
import { eslint } from "rollup-plugin-eslint"

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  },
  plugins: [
    eslint({
      include: ["src/**"],
      exclude: ["node_modules/**"]
    }),
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    })
  ]
}

八、commonjs 兼容

yarn add rollup-plugin-commonjs@9.2.0 rollup-plugin-node-resolve@4.0.0 --dev

九、与 rollup 集成,在 rollup.config.js 中配置 plugins

import babel from "rollup-plugin-babel"
import { eslint } from "rollup-plugin-eslint"
import resolve from "rollup-plugin-node-resolve"
import commonjs from "rollup-plugin-commonjs"

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  },
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true
    }),
    commonjs(),
    eslint({
      include: ["src/**"],
      exclude: ["node_modules/**"]
    }),
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    })
  ]
}

十、安装 terser, 用来压缩代码

yarn add rollup-plugin-terser@4.0.0 --dev

十一、与 rollup 集成,在 rollup.config.js 中配置 plugins

import babel from "rollup-plugin-babel"
import { eslint } from "rollup-plugin-eslint"
import resolve from "rollup-plugin-node-resolve"
import commonjs from "rollup-plugin-commonjs"
import { terser } from "rollup-plugin-terser"

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  },
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true
    }),
    commonjs(),
    eslint({
      include: ["src/**"],
      exclude: ["node_modules/**"]
    }),
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    }),
    terser()
  ]
}

十二、引入环境变量,实践差异化打包

1、安装插件

yarn add rollup-plugin-replace@2.1.0 --dev

2、配置 plugins

import babel from "rollup-plugin-babel"
import { eslint } from "rollup-plugin-eslint"
import resolve from "rollup-plugin-node-resolve"
import commonjs from "rollup-plugin-commonjs"
import { terser } from "rollup-plugin-terser"
import replace from "rollup-plugin-replace"

export default {
  input: "src/index.js",
  output: {
    file: "index.common.js",
    format: "umd",
    name: "index"
  },
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true
    }),
    commonjs(),
    eslint({
      include: ["src/**"],
      exclude: ["node_modules/**"]
    }),
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    }),    
    replace({
      exclude: "node_modules/**",
      ENVIRONMENT: JSON.stringify(process.env.NODE_ENV)
    }),
    terser()
  ]
}

十三、参数化配置,加入版权说明,最终配置如下

import resolve from "rollup-plugin-node-resolve"
import commonjs from "rollup-plugin-commonjs"
import { eslint } from "rollup-plugin-eslint"
import babel from "rollup-plugin-babel"
import replace from "rollup-plugin-replace"
import { terser } from "rollup-plugin-terser"

const pJson = require("./package.json")

const version = pJson.version
const license = pJson.license

const banner =
  "/*!
" +
  ` * ${pJson.name} v${version}
` +
  ` * (c) 2018-${new Date().getFullYear()}
` +
  ` * Released under the ${license} License.
` +
  " */"

const ENV = process.env.NODE_ENV.trim()

const paths = {
  input: {
    root: "src/index.js"
  },
  output: {
    root: "dist/"
  }
}

const fileNames = {
  development: "index.common.js",
  production: "index.common.js",
  production6: "index.esm.js"
}
const fileName = fileNames[ENV]

export default {
  input: `${paths.input.root}`,
  output: {
    file: `${paths.output.root}${fileName}`,
    format: ENV === "production6" ? "es" : "umd",
    name: "index",
    banner
  },
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true
    }),
    commonjs(),
    eslint({
      include: ["src/**"],
      exclude: ["node_modules/**"]
    }),
    babel({
      exclude: "node_modules/**",
      runtimeHelpers: true
    }),
    replace({
      exclude: "node_modules/**",
      ENVIRONMENT: JSON.stringify(process.env.NODE_ENV)
    }),
    ENV && ENV.includes("production") && terser({ output: { comments: /^!/ } })
  ]
}

三、业务代码编写

在 src/index.js 中编写具体业务代码

四、打包

在 package.json 中添加

"scripts": {
    "dev": "set NODE_ENV=development && rollup -c",
    "build": "yarn run buildcjs && yarn run buildesm",
    "buildcjs": "set NODE_ENV=production && rollup -c",
    "buildesm": "set NODE_ENV=production6 && rollup -c"
}

运行命令

yarn run build

五、发布

npm publish

发布前记得记得 注册 帐号,记得修改 package.json 中 private 字段为 false

"private": false

后记:rollup-plugin-uglify@6.0.0 在 rollup@1.0.0 时有警告,文章中原 rollup-plugin-uglify 被替换成 rollup-plugin-terser

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/61372.html

相关文章

  • npm发布记录

    摘要:下雪了,在家闲着,不如写一个包发布。简单的包的发布网上有很多教程,我就不记录了。这里记录下,一个复杂的包发布,复杂指的构建环境复杂。同时发布格式的版本以供外部调用。 下雪了,在家闲着,不如写一个npm 包发布。简单的 npm 包的发布网上有很多教程,我就不记录了。这里记录下,一个复杂的 npm 包发布,复杂指的构建环境复杂。 整个工程使用 rollup 来构建,其中会引进 babel ...

    李文鹏 评论0 收藏0
  • npm入手笔记0x003-一些常见问题记录

    摘要:如何选择就如果上面所有,需要打包进生产环境就保存到,只是在开发或者打包的时候使用的就保存到即可。提示不能发布当前版本解决方案不能发布已经发布的版本,修改一下版本号就可以了想不到了,想到了再写资源项目 0x001 概述 本篇文章承接上文,记录的是一些使用过程中的疑惑 0x001 墙的原因使得包下载太慢 解决方案:使用淘宝cnpm,推荐使用cnpm,因为如果修改npm仓库,将会导致无法发布...

    luffyZh 评论0 收藏0
  • 如何设计npm的开发和发布流程

    摘要:所以此版本号在这里的作用并不是用来区分版本的,小版本号才是真正用来做版本区分的,那么在引用这个就要这么来控制版本号,举个栗子锁定大版本号和小版本号,不管我们开发过程中提交了多少次,我们引用都是最新的。 最近在把公司内部用的一个库发布到内网的npm私服上,仅仅是发布的话是比较简单的,但这个库是由多个人一起维护的,而且npm私服只有一套,所以生产环境和开发环境,用的是同一个,那么,我们的需...

    qieangel2013 评论0 收藏0
  • 生成自己的js工具括打webpack、测试mocha、生成文档jsdoc、发布npm的操作

    摘要:包说明包实际是一个存档文件,即一个目录直接打包为或格式的文件,安装后解压还原为目录。完全符合规范的包目录应该包含如下这些文件包描述文件。用于存放单元测试用例的代码。 keepsmiling说明 一些常用的函数集合,主要用到的技术如下: ES6的包处理方式; webpack打包方式; BDD测试用例,只写了部分; 使用jsdoc生成注释文档; 你用eslint优化代码格式; 主...

    Code4App 评论0 收藏0
  • 发布 react 组件到 npm

    摘要:我发布了我的第一个组件,一个基于的标签云组件。然后将这个编译命令写到里,如下那么以后要编译下面的代码,只需要执行现在我们已经有编译好的代码了,接下来就可以发布到供其他人使用了。 我发布了我的第一个 npm 组件,一个基于 react 的 3d 标签云组件。在这途中我也是遇到了很多的坑,花在完善整个发布流程的时间远多于写这个组件本身的时间,所以我记录下我觉得一个正常的 react 组件的...

    Yi_Zhi_Yu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<