rollup.js
是一个模块打包工具,可使项目从一个入口文件开始,将全部使用到的模块文件都打包到一个最终的发布文件中,Rollup
极其适合构建一个工具库,Vue.js
源码就是经过Rollup
打包构建的。javascript
rollup
对代码模块使用新的标准化格式,这些标准都包含在JavaScript
的ES6
版本中,而不是之前的特殊解决方案,如CommonJS
和AMD
等,也就是说rollup
使用ES6
的模块标准,这意味着咱们能够直接使用import
和export
而不须要引入babel
,固然,在如今的项目中,babel
能够说是必用的工具,此外rollup
实现了另外一个重要特性叫作tree-shaking
,这个特性能够帮助你将无用代码,即没有使用到的代码自动去掉,这个特性是基于ES6
模块的静态分析的,也就是说,只有export
而没有import
的变量是不会被打包到最终代码中的。css
个人一个小油猴插件就是经过rollup
打包的,GreasyFork
地址为https://greasyfork.org/zh-CN/scripts/405130
,所有源码地址为https://github.com/WindrunnerMax/TKScript
,使用npm run build
便可打包构建,package.json
文件与rollup.config.js
文件配置以下。java
{ "name": "TKScript", "version": "1.0.0", "description": "Tampermonkey", "scripts": { "build": "rollup -c" }, "author": "Czy", "license": "MIT", "devDependencies": { "@babel/core": "^7.10.4", "@babel/preset-env": "^7.10.4", "install": "^0.13.0", "npm": "^6.14.5", "rollup": "^2.18.2", "rollup-plugin-babel": "^4.4.0", "rollup-plugin-postcss": "^3.1.2", "rollup-plugin-uglify": "^6.0.4", "rollup-plugin-userscript-metablock": "^0.2.5" } }
import postcss from "rollup-plugin-postcss"; import babel from "rollup-plugin-babel"; // import { uglify } from "rollup-plugin-uglify"; import metablock from "rollup-plugin-userscript-metablock"; const config = { postcss: { minimize: true, extensions: [".css"], }, babel: { exclude: ["node_modules/**"], presets: [ [ "@babel/env", { modules: false, targets: "last 2 versions, ie >= 10" } ] ] }, } export default [{ input: "./src/copy/src/index.js", output: { file: "./dist/copy.js", format: "iife", name: "copyModule" }, plugins: [ postcss(config.postcss), babel(config.babel), // uglify(), metablock({ file: "./src/copy/meta.json" }) ] },{ input: "./src/site-director/src/index.js", output: { file: "./dist/site-director.js", format: "iife", name: "linkModule" }, plugins: [ postcss(config.postcss), babel(config.babel), // uglify(), metablock({ file: "./src/site-director/meta.json" }) ] }];
npm install rollup -g
。npm install rollup --save-dev
、yarn add rollup -D
。-i, --input <filename>
: 要打包的文件(必须)。-o, --file <output>
: 输出的文件(若是没有这个参数,则直接输出到控制台)。-f, --format <format>
: 输出的文件类型。
amd
: 异步模块定义,用于像RequestJS
这样的模块加载器。cjs
: CommonJS,
适用于Node
或Browserify/webpack
。es
: 将软件包保存为ES
模块文件。iife
: 一个自动执行的功能,适合做为script
标签这样的,只能在浏览器中运行。umd
: 通用模块定义,以amd
、cjs
和iife
为一体。system
: SystemJS
加载器格式。-e, --external <ids>
: 将模块ID
的逗号分隔列表排除。-g, --globals <pairs>
: 以moduleID:Global
键值对的形式,用逗号分隔开任何定义在这里模块ID
定义添加到外部依赖。-n, --name <name>
: 生成UMD
模块的名字。-m, --sourcemap
: 生成sourcemap
。--amd.id
: AMD
模块的ID
,默认是个匿名函数。--amd.define
: 使用Function
来代替define
。--no-strict
: 在生成的包中省略use strict;
。--no-conflict
: 对于UMD
模块来讲,给全局变量生成一个无冲突的方法。--intro
: 在打包好的文件的块的内部(wrapper
内部)的最顶部插入一段内容。--outro
: 在打包好的文件的块的内部(wrapper
内部)的最底部插入一段内容。--banner
: 在打包好的文件的块的外部(wrapper
外部)的最顶部插入一段内容。--footer
: 在打包好的文件的块的外部(wrapper
外部)的最底部插入一段内容。--interop
: 包含公共的模块(这个选项是默认添加的)。-w, --watch
: 监听源文件是否有改动,若是有改动,从新打包。--silent
: 不要将警告打印到控制台。-h, --help
: 输出帮助信息。-v, --version
输出版本信息。// rollup.config.js export default { // 核心选项 input, // 必须 external, plugins, // 额外选项 onwarn, // danger zone acorn, context, moduleContext, legacy output: { // 必须 (若是要输出多个,能够是一个数组) // 核心选项 file, // 必须 format, // 必须 name, globals, // 额外选项 paths, banner, footer, intro, outro, sourcemap, sourcemapFile, interop, // 高危选项 exports, amd, indent strict }, };
input
、rollup -i,--input
,打包入口文件路径,参数类型为String | String [] | { [entryName: string]: string }
。
使用数组或者字符串做为选项值的时候的时候,默认使用的是文件的原始名称,做为文件的basename
,能够在output:entryFileNames = entry-[name].js
配置选项做为[name]
动态参数传递进去。node
input: "./src/index.js"; input: ["./src/index.js", "./other/index.js"];
用键值对{key: value}
的选项值做为参数,使用的对象的键做为文件的basename
,用来在output:entryFileNames
配置选项做为[name]
动态参数传递进去。jquery
input: { main: "./src/index.js", vendor: "./other/index.js" }
external
、rollup -e,--external
, 维持包文件指定id
文件维持外链,不参与打包构建 参数类型为String[] | (id: string, parentId: string, isResolved: boolean) => boolean
。webpack
format
类型为iife
或者umd
格式的时候须要配置output.globals
选项参数以提供全局变量名来替换外部导入。external
是一个函数的时候,各个参数表明的含义分别是: id
,全部导入的文件id
,即import
访问的路径;parent
,import
所在的文件绝对路径;isResolved
,表示文件id
是否已经过插件处理过。{ // ..., external: [ 'some-externally-required-library', 'another-externally-required-library' ] } // or { // ..., external: (id, parent, isResolved) => { return true; } }
能够提供rollup
不少插件选项,参数类型为Plugin | (Plugin | void)[]
。git
{ // ..., plugins: [ resolve(), commonjs(), isProduction && (await import("rollup-plugin-terser")).terser() ] }
拦截警告信息,若是没有提供,警告将被复制并打印到控制台,警告是至少有一个code
和message
属性的对象,咱们能够控制如何处理不一样类型的警告。github
onwarn (warning) { // 跳过某些警告 if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return; // 抛出异常 if (warning.code === 'NON_EXISTENT_EXPORT') throw new Error(warning.message); // 控制台打印一切警告 console.warn(warning.message); }
许多警告也有一个loc
属性和一个frame
,能够定位到警告的来源。web
onwarn ({ loc, frame, message }) { // 打印位置(若是适用) if (loc) { console.warn(`${loc.file} (${loc.line}:${loc.column}) ${message}`); if (frame) console.warn(frame); } else { console.warn(message); } }
这是danger zone
,修改rollup
解析js
配置,rollup
内部使用的acorn
库解析js, acorn
库提供了解析js
的相关配置api
,通常不多须要修改。在下面这个例子中,这个acorn-jsx
插件和使用babel
并非同一个意思,这个插件的左右是让acornjs
解析器能认识jsx
语法,通过rollup
打包后展现的仍是jsx
语法,而babel
会直接修改jsx
结构成为普通js
语法。npm
import jsx from "acorn-jsx"; export default { // ... acornInjectPlugins: [ jsx() ] };
默认状况下,模块的上下文,即顶级的this
的值为undefined
,在极少数状况下,可能须要将其更改成其余内容,例如window
。
和context
同样,可是每一个模块能够是id:context
对的对象,也能够是id=>context
函数。
为了增长对诸如IE8
之类的旧版环境的支持,经过剥离更多可能没法正常工做的现代化的代码,其代价是偏离ES6
模块环境所需的精确规范。
output
是输出文件的统一配置入口, 包含不少可配置选项 参数类型为Object | Array
,单个输出为一个对象,要输出多个,能够是一个数组。
output.file
、rollup -o,--file
,必填,对于单个文件打包可使用该选项指定打包内容写入带路径的文件,参数类型为String
。
output: { file: "./dist/index.js" }
output.format
、rollup -f,--format
,必填,打包格式类型 ,配置可选项有amd
、cjs
、es
、iife
、umd
、system
,选项说明同命令行配置选项,参数类型为String
。
output: { format: "iife" }
output.format
,rollup -f,--format
生成包名称,参数类型为String
。
export default { // ..., output: { name: "bundle" } };
output.globals
,rollup -g,--globals
,配合配置external
选项指定的外链在umd
和iife
文件类型下提供的全局访问变量名参数类型,参数类型为{ [id: String]: String } | ((id: String) => String)
。
export default { // ..., globals: { jquery: "$" } };
它获取一个ID
并返回一个路径,或者id: path
对的Object
,在提供的位置,这些路径将被用于生成的包而不是模块ID
,从而容许从CDN
加载依赖关系。
// app.js import { selectAll } from 'd3'; selectAll('p').style('color', 'purple'); // ... // rollup.config.js export default { input: 'app.js', external: ['d3'], output: { file: 'bundle.js', format: 'amd', paths: { d3: 'https://d3js.org/d3.v4.min' } } }; // bundle.js define(['https://d3js.org/d3.v4.min'], function (d3) { d3.selectAll('p').style('color', 'purple'); // ... });
字符串前置到文件束bundle
,banner
选项不会破坏sourcemaps
,参数类型为String
。
export default { // ..., output: { banner: "/* library version " + version + " */", } };
字符串前置到文件束bundle
,footer
选项不会破坏sourcemaps
,参数类型为String
。
export default { // ..., output: { footer: "/* follow me on Github! */", } };
相似于output.banner
,若是说banner
和footer
是在文件开始和结尾添加字符串,那么intro
和outro
就是在被打包的代码开头和结尾添加字符串了。
export default { // ..., output: { intro: "/* library version " + version + " */", } };
相似于output.footer
,若是说banner
和footer
是在文件开始和结尾添加字符串,那么intro
和outro
就是在被打包的代码开头和结尾添加字符串了。
export default { // ..., outro: { footer: "/* follow me on Github! */", } };
sourcemap
、rollup -m,--sourcemap, --no-sourcemap
,若是true
,将建立一个单独的sourcemap
文件,若是inline, sourcemap
将做为数据URI
附加到生成的output
文件中。
生成的包的位置,若是这是一个绝对路径,sourcemap
中的全部源代码路径都将相对于它,map.file
属性是sourcemapFile
的基本名称basename
,由于sourcemap
的位置被假定为与bundle
相邻,若是指定output
,sourcemapFile
不是必需的,在这种状况下,将经过给bundle
输出文件添加.map
后缀来推断输出文件名,通常应用场景不多,在特殊场景须要改变sourcemap
的指向文件地址时才会用到。
是否添加interop
块,默认状况下interop: true
,为了安全起见,若是须要区分默认和命名导出,则rollup
会将任何外部依赖项default
导出到一个单独的变量,这一般只适用于您的外部依赖关系,例如与Babel
,若是肯定不须要它,则可使用interop: false
来节省几个字节。
使用什么导出模式,默认为auto
,它根据entry
模块导出的内容猜想你的意图。
default
: 若是使用export default...
仅仅导出一个文件,那适合用这个。named
: 若是导出多个文件,适合用这个。none
: 若是不导出任何内容,例如正在构建应用程序,而不是库,则适合用这个。打包amd
模块相关定义。
amd.id
: 用于AMD/UMD
软件包的ID
。amd.define
: 要使用的函数名称,而不是define
。是要使用的缩进字符串,对于须要缩进代码的格式amd
、iife
、umd
,也能够是false
无缩进或true
默认自动缩进。
true
或false
,默认为true
,是否在生成的非ES6
软件包的顶部包含usestrict pragma
,严格来讲ES6
模块始终都是严格模式,因此应该没有很好的理由来禁用它。
https://github.com/WindrunnerMax/EveryDay
https://www.rollupjs.com/ https://segmentfault.com/a/1190000010628352 https://github.com/JohnApache/rollup-usage-doc