- 支持js打包
yarn add rollup -D
module.exports = {
input: './src/main.js',
output: {
file: 'lib/demo.js',
format: 'es',
},
}
yarn add rollup-plugin-terser --dev
import { terser } from 'rollup-plugin-terser'
module.exports = {
input: './src/main.js',
output: {
file: 'lib/demo.js',
format: 'es',
},
plugins: [terser()],
}
yarn add @rollup/plugin-commonjs @rollup/plugin-eslint @rollup/plugin-node-resolve -D
-
@rollup/plugin-commonjs
A Rollup plugin to convert CommonJS modules to ES6, so they can be included in a Rollup bundle
-
@rollup/plugin-eslint
A Rollup plugin to lint entry points and all imported files with ESLint.
-
@rollup/plugin-node-resolve
A Rollup plugin which locates modules using the Node resolution algorithm, for using third party modules in node_modules
The @rollup/plugin-node-resolve plugin teaches Rollup how to find external modules. Install it…
import path from 'path'
import { nodeResolve } from '@rollup/plugin-node-resolve' // 依赖引用插件
import commonjs from '@rollup/plugin-commonjs' // commonjs模块转换ES6插件
import eslint from '@rollup/plugin-eslint' // eslint插件
const getPath = (_path) => path.resolve(__dirname, _path)
import packageJSON from './package.json'
const extensions = ['.js', '.ts', '.tsx']
// eslint
const esPlugin = eslint({
throwOnError: true,
include: ['src/**/*.ts'],
exclude: ['node_modules/**', 'lib/**'],
})
// 基础配置
const commonConf = {
input: getPath('./src/main.js'),
plugins: [nodeResolve(extensions), commonjs(), esPlugin],
}
// 需要导出的模块类型
const outputMap = [
{
file: packageJSON.main, // 通用模块
format: 'umd',
},
{
file: packageJSON.module, // es6模块
format: 'es',
},
]
const buildConf = (options) => Object.assign({}, commonConf, options)
export default outputMap.map((output) => {
return buildConf({
output: {
name: packageJSON.name,
...output,
},
})
})
yarn add @rollup/plugin-typescript typescript tslib -D
rollup.config.js
import ts from '@rollup/plugin-typescript'
// ts
const tsPlugin = ts({
tsconfig: getPath('./tsconfig.json'), // 导入本地ts配置
})
plugins: [
tsPlugin
]
yarn add @rollup/plugin-babel @babel/preset-env @rollup/plugin-commonjs -D
选择性的安装core-js版本
npm install --save core-js@2 npm install --save core-js@3
yarn add core-js@2 yarn add core-js@3
module.exports = {
presets: [['@babel/preset-env', {
"targets": "> 85%, not dead",
"useBuiltIns": "usage"
}]],
}
import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
module.exports = {
input: './src/main.js',
output: {
file: 'lib/demo.js',
format: 'es',
},
plugins: [commonjs(), babel({ babelHelpers: 'bundled' })],
}
yarn add rollup-plugin-css-only -D
import css from 'rollup-plugin-css-only' // 提取css
plugins: [
css({
output(style) {
// 压缩 css 写入 lib/base-ui.css
if (!existsSync('lib')) {
mkdirSync('lib')
}
writeFileSync('lib/yc-ui.css', style)
},
}),
],
yarn add clean-css -D
import css from 'rollup-plugin-css-only' // 提取css
import CleanCSS from 'clean-css' // 压缩css
plugins: [
css({
output(style) {
// 压缩 css 写入 lib/base-ui.css
if (!existsSync('lib')) {
mkdirSync('lib')
}
writeFileSync('lib/base-ui.css', new CleanCSS().minify(style).styles)
}
})
]
yarn add postcss rollup-plugin-postcss -D
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
module.exports = {
input: './src/main.js',
output: {
file: 'lib/demo.js',
format: 'es',
},
plugins: [
postcss({
extract: true,
plugins: [autoprefixer()],
minimize: true, //压缩
modules: true, // scoped
}),
],
}
yarn add less -D
yarn add node-sass -D
yarn add stylus -D
import vuePlugin from 'rollup-plugin-vue'
vuePlugin({
target: 'browser',
exclude: ['node_modules/**'],
include: /\.vue$/,
}),
import esbuild from 'rollup-plugin-esbuild' // ts ==> ks
import vueJsx from 'rollup-plugin-vue-jsx-compat' // jsxFactory
plugins: [
vueJsx(),
esbuild({
jsxFactory: 'vueJsxCompat',
})
]
import replace from 'rollup-plugin-replace'
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('development'),
}),
]
import serve from 'rollup-plugin-serve'
plugins: [
serve({
open: false,
openPage: '/site/index.html',
port: 3000,
contentBase: '',
}),
]
- rollup-plugin-jsx 有问题,无法正确处理jsx, Fragment无法处理,一些写法也不支持