以一個最基本的demo展現rollup的功能/用法。npm
mkdir exRollup cd exRollup npm init npm i -d rollup npm i -d rollup-plugin-json
{ ... "scripts": { ... "build:cjs": "rollup -c ./config/rollup.config.cjs.js", "build:esm": "rollup -c ./config/rollup.config.esm.js", "build:iife": "rollup -c ./config/rollup.config.iife.js", "build:umd": "rollup -c ./config/rollup.config.umd.js", "build": "npm run build.cjs & npm run build:esm & npm run build:iife & npm run build:cmd" } }
本示例中有4個配置文件。分別用於生成commonjs
規範代碼/esm
規範代碼/當即執行代碼/umd
規範代碼。json
// rollup.config.cjs.js import json from 'rollup-plugin-json'; export default { input: 'src/index.js', output: { file: 'dist/bundle.cjs.js', format: 'cjs' }, plugins: [ json() ] };
// rollup.config.esm.js import json from 'rollup-plugin-json'; export default { input: 'src/index.js', output: { file: 'dist/bundle.esm.js', format: 'esm' }, plugins: [ json() ] };
// rollup.config.iife.js import json from 'rollup-plugin-json'; export default { input: 'src/index.js', output: { file: 'dist/bundle.iife.js', format: 'iife' }, plugins: [ json() ] };
// rollup.config.umd.js import json from 'rollup-plugin-json'; export default { input: 'src/index.js', output: { file: 'dist/bundle.umd.js', format: 'umd' }, plugins: [ json() ] };
// src/foo.js export default function (a, b) { return a + b }
// src/index.js import {version} from '../package.json' import add from './foo.js' export default function () { console.log(`version: ${version}`) } export let sum = function (a, b) { return add(a, b) }
npm run build