Rollup.js打包代码

举报
彭世瑜 发表于 2021/08/14 00:16:27 2021/08/14
5.2k+ 0 0
【摘要】 Rollup 是一个 JavaScript 模块打包器 中文网:https://www.rollupjs.com/ 英文网:https://www.rollupjs.org/ 安装 npm install --global rollup 1 简单示例 main.js console.log("hello rollup"); 1 打包 rollup --h...

Rollup 是一个 JavaScript 模块打包器

中文网:https://www.rollupjs.com/
英文网:https://www.rollupjs.org/

安装

npm install --global rollup

  
 

简单示例

main.js

console.log("hello rollup");

  
 

打包

rollup --help # 可以查看

# 浏览器
rollup main.js --file bundle.js --format iife

  
 

打包结果

bundle.js

(function () {
  "use strict"; console.log("hello rollup");
})();

  
 

第一个 Buddle

src/main.js

// src/main.js
import foo from "./foo.js";

export default function () {
  console.log(foo);
}

  
 

src/foo.js

// src/foo.js
export default "hello world!";

  
 

打包

rollup src/main.js -o bundle.js -f cjs

  
 

使用配置文件

rollup.config.js

// rollup.config.js
export default {
  input: "src/main.js",
  output: { file: "bundle.js", format: "cjs",
  },
};

  
 

打包

# 默认使用rollup.config.js
rollup -c

  
 

插件

https://github.com/rollup/plugins/

npm init -y

npm install --save-dev rollup-plugin-json

  
 

从 json 文件中读取数据

package.json

{
  "name": "rollup-demo",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "dependencies": {},
  "devDependencies": { "rollup-plugin-json": "^4.0.0"
  },
  "scripts": { "build": "rollup -c"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

  
 

src/main.js

// src/main.js
import { version } from "../package.json";

export default function () {
  console.log("version " + version);
}

  
 

配置文件加入 JSON 插件
rollup.config.js

// rollup.config.js
import json from "rollup-plugin-json";

export default {
  input: "src/main.js",
  output: { file: "bundle.js", format: "cjs",
  },
  plugins: [json()],
};

  
 

编译

npm run build

  
 

编译结果

"use strict";

var version = "1.0.0";

// src/main.js

function main() {
  console.log("version " + version);
}

module.exports = main;

  
 

总结

配置打包和开发环境启动项

package.json

{
  "name": "rollup-demo",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "dependencies": {},
  "devDependencies": { "rollup-plugin-json": "^4.0.0"
  },
  "scripts": { "dev": "rollup --config --watch", "build": "rollup --config"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

  
 

配置即使打包

rollup.config.js

// rollup.config.js
import json from "rollup-plugin-json";

export default {
  input: "src/main.js", output: { file: "bundle.js", format: "iife",
  },
  plugins: [json()], watch: { exclude: "node_modules/**",
  },
};

  
 

不错的文章
深入学习rollup来进行打包

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/107379105

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。