使用rollup 开发专业js library

rollup 是一个不错的javascript 模块打包器,一般我们用来构建library

安装

npm install -g rollup

参考集成jquey && shortid 的library

使用es6 语法

  • 项目结构
├── index.html
├── package.json
├── rollup.config.js
├── src
│ └── user.js
└── yarn.lock
  • 代码说明
index.html :
测试构建的library
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <script src="app.js"></script>
</body>
</html>
 rollup.config.js:
rpllup 使用config 方式构建,使用了几个插件(npm commonjs babel uglify)
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import { uglify } from 'rollup-plugin-uglify';
export default {
    input: 'src/user.js',
    output: [{
            file: 'bundle.js',
            format: 'cjs',
            banner:"// license under apache dalaongrong@qq.com",
        },
        {
            file: 'app.js',
            format: 'umd',
            name:"appdemo",
            banner:"// license under apache dalaongrong@qq.com",
        }
    ],
    plugins: [
        uglify(),
        nodeResolve({
          jsnext: false,
          main: true,
          browser: true
        }),
        babel({
            exclude: 'node_modules/**' // only transpile our source code
        }),
        commonjs({
          // non-CommonJS modules will be ignored, but you can also
          // specifically include/exclude files
          include: 'node_modules/**', // Default: undefined
          exclude: [ 'node_modules/foo/**', 'node_modules/bar/**' ], // Default: undefined
          // these values can also be regular expressions
          // include: /node_modules/

          // search for files other than .js files (must already
          // be transpiled by a previous plugin!)
          extensions: [ '.js', '.coffee' ], // Default: [ '.js' ]

          // if true then uses of `global` won't be dealt with by this plugin
          ignoreGlobal: false, // Default: false

          // if false then skip sourceMap generation for CommonJS modules
          sourceMap: false, // Default: true

          // explicitly specify unresolvable named exports
          // (see below for more details)
          namedExports: { './module.js': ['foo', 'bar' ] }, // Default: undefined

          // sometimes you have to leave require statements
          // unconverted. Pass an array containing the IDs
          // or a `id => boolean` function. Only use this
          // option if you know what you're doing!
          ignore: [ 'conditional-runtime-dependency' ]
        })
      ]
};

package.json:
引用依赖的包
{
"name": "first",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"jquery": "^3.3.1",
"shortid": "^2.2.12"
},
"scripts": {
"build": "rollup -c",
"live": "live-server"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"live-server": "^1.2.0",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.1.4",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-uglify": "^4.0.0"
}
}


src/user.js:
业务处理代码
import shortid from "shortid"
import jq from "jquery"
const user ={
    name:"dalong",
    age:343
}
export default {
    id:shortid.generate(),
    version:"appv1",
    ...user,
    $:jq
}
src/.babelrc:
babel 编译配置
{
  "presets": [
    ["env", {
      "modules": false
    }]
  ],
  "plugins": ["external-helpers", "transform-object-rest-spread"]
}

构建&&运行

  • 构建
yarn build
  • 运行
yarn live
  • 效果

参考资料

https://rollupjs.org/guide/en
https://github.com/rongfengliang/rollup-babel-demolibrary

posted on   荣锋亮  阅读(891)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-08-01 centos7通过yum安装mysql,并授权远程连接
2016-08-01 查看mysql主从配置的状态及修正 slave不启动问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示