nitroge / memories

日常学习累积的点点滴滴滴滴点点
7 stars 0 forks source link

rollup打包js代码 #33

Open nitroge opened 1 year ago

nitroge commented 1 year ago

需要安装的依赖

"@babel/core": "^7.22.1",
"@babel/preset-env": "^7.22.2",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-node-resolve": "^15.0.2",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-latest": "^6.24.1",
"rollup": "^3.15.0",
"rollup-obfuscator": "^3.0.1", // 代码混淆工具,取决于是否需要对代码进行加密
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-terser": "^7.0.2"

rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import { terser } from "rollup-plugin-terser";
import { obfuscator } from "rollup-obfuscator";
import babel from "@rollup/plugin-babel";

export default {
  input: "src/main.js",
  output: [
    {
      file: "dist/captcha.es.js",
      format: "es",
    },
    {
      file: "dist/captcha.cjs.js",
      format: "cjs",
    },
  ],
  plugins: [
    resolve(),
    commonjs(),
    terser(),
    obfuscator(),
    babel({
      babelHelpers: "bundled",
      presets: [
        [
          "@babel/preset-env",
          {
            targets: "> 0.25%, last 1 version, not dead",
          },
        ],
      ],
    }),
  ],
  external: ["js-md5", "axios"],
};

package.json

"main": "dist/captcha.cjs.js",
"module": "dist/captcha.es.js",
// 导出哪些模块

"files": [
    "dist"
]