npm包釋出記錄

lifefriend_007發表於2018-12-30

心血來潮,寫個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 配置


.\node_modules\.bin\eslint --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 =
  '/*!\n' +
  ` * ${pJson.name} v${version}\n` +
  ` * (c) 2018-${new Date().getFullYear()}\n` +
  ` * Released under the ${license} License.\n` +
  ' */'

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

複製程式碼

相關文章