|
| 1 | +/** |
| 2 | + * @reference https://www.rollupjs.com/guide/introduction/ |
| 3 | + */ |
| 4 | +import commonjs from '@rollup/plugin-commonjs'// 用来将 CommonJS 转换成 ES2015 模块的 |
| 5 | +import json from '@rollup/plugin-json' |
| 6 | +import resolve from '@rollup/plugin-node-resolve'// 告诉 Rollup 如何查找外部模块 |
| 7 | +import babel from '@rollup/plugin-babel' |
| 8 | +import {uglify} from 'rollup-plugin-uglify' |
| 9 | +import {name, version, author} from './package.json' |
| 10 | +const banner = `/*! |
| 11 | + * ${name} v${version} |
| 12 | + * (c) ${new Date().getFullYear()} ${author} |
| 13 | + * @license MIT |
| 14 | + */` |
| 15 | +export default { |
| 16 | + input: 'src/index.js', |
| 17 | + output: [ |
| 18 | + { |
| 19 | + banner, |
| 20 | + file: `dist/${name}.amd.js`, |
| 21 | + format: 'amd', // 浏览器 |
| 22 | + name |
| 23 | + }, |
| 24 | + { |
| 25 | + file: `dist/${name}.cjs.js`, |
| 26 | + format: 'cjs', // compile to a CommonJS module ('cjs') node环境 |
| 27 | + name |
| 28 | + }, |
| 29 | + { |
| 30 | + file: `dist/${name}.esm.js`, |
| 31 | + format: 'esm', // 浏览器 |
| 32 | + name |
| 33 | + }, |
| 34 | + { |
| 35 | + file: `dist/${name}.iife.js`, |
| 36 | + format: 'iife', // 浏览器 |
| 37 | + name |
| 38 | + }, |
| 39 | + { |
| 40 | + file: `dist/${name}.umd.js`, |
| 41 | + format: 'umd', // UMD format requires a bundle name 浏览器和 Node.js |
| 42 | + name |
| 43 | + }, |
| 44 | + // min |
| 45 | + { |
| 46 | + file: `dist/${name}.amd.min.js`, |
| 47 | + format: 'amd', // 浏览器 |
| 48 | + name, |
| 49 | + plugins: [uglify()] |
| 50 | + }, |
| 51 | + { |
| 52 | + file: `dist/${name}.cjs.min.js`, |
| 53 | + format: 'cjs', // compile to a CommonJS module ('cjs') node环境 |
| 54 | + name, |
| 55 | + plugins: [uglify()] |
| 56 | + }, |
| 57 | + { |
| 58 | + file: `dist/${name}.esm.min.js`, |
| 59 | + format: 'esm', // 浏览器 |
| 60 | + name, |
| 61 | + plugins: [uglify()] |
| 62 | + }, |
| 63 | + { |
| 64 | + file: `dist/${name}.iife.min.js`, |
| 65 | + format: 'iife', // 浏览器 |
| 66 | + name, |
| 67 | + plugins: [uglify({ |
| 68 | + output:{ |
| 69 | + comments: function (node, comment){ |
| 70 | + //以!开头部分的注视进行保留 |
| 71 | + return /^!/.test(comment.value) |
| 72 | + } |
| 73 | + } |
| 74 | + })] |
| 75 | + }, |
| 76 | + { |
| 77 | + file: `dist/${name}.umd.min.js`, |
| 78 | + format: 'umd', // UMD format requires a bundle name 浏览器和 Node.js |
| 79 | + name, |
| 80 | + plugins: [uglify({ |
| 81 | + output:{ |
| 82 | + preamble: '/** \r\r 版本所有 \r\n 填写日期 \r\n 填写作者信息 */' |
| 83 | + } |
| 84 | + })] |
| 85 | + } |
| 86 | + ], |
| 87 | + plugins: [ |
| 88 | + commonjs(), |
| 89 | + json(), |
| 90 | + resolve({ |
| 91 | + // 将自定义选项传递给解析插件 |
| 92 | + customResolveOptions: { |
| 93 | + moduleDirectory: 'node_modules' |
| 94 | + } |
| 95 | + }), |
| 96 | + babel({ |
| 97 | + // 为避免编译三方脚本,通过设置exclude属性忽略node_modules目录。 |
| 98 | + // babelHelpers:'bundled', |
| 99 | + exclude: ['node_modules/**', 'dist/**', 'test/**'], // 只编译我们的源代码 |
| 100 | + include: ['src/**'] |
| 101 | + }), |
| 102 | + // default |
| 103 | + // ["lodash"], |
| 104 | + // Set plugin options using an array of [pluginName, optionsObject]. |
| 105 | + // ["lodash", { "id": "lodash-compat", "cwd": "some/path" }] |
| 106 | + // The options.id can be an array of ids. |
| 107 | + ['lodash', {id: ['async', 'lodash-bound']}], |
| 108 | + |
| 109 | + // uglify() //只生产压缩的库 |
| 110 | + ], |
| 111 | + // 指出应将哪些模块视为外部模块: |
| 112 | + // 你可以微调哪些导入是想要打包的,哪些是外部的引用(externals)。 |
| 113 | + // 对于这个例子,我们认为 lodash 是外部的引用(externals),而不是 the-answer 。 |
| 114 | + external: ['lodash'], |
| 115 | + watch: { |
| 116 | + // 限制文件监控至某些文件: |
| 117 | + include: 'src/**', |
| 118 | + // 防止文件被监控: |
| 119 | + exclude: 'node_modules/**' |
| 120 | + }, |
| 121 | + // treeshake: true, |
| 122 | + presets: [['@babel/env', {targets: {node: 6}}]] |
| 123 | +} |
0 commit comments