其他打包工具 Rollup && Parcel
Rollup
rollup與webpack十分相似 Rollup更為小巧,只是ESM的打包器 不支援HMR(模組熱替換)這種高階特性
是為了提供ESM各種特性的高效打包器
快速上手
yarn add rollup --dev 安裝依賴 會在 node-modules 下 bin/ rollup 提供一個cli程式
執行 yarn rollup yarn rollup ./src/index.js 打包入口檔案是index.js yarn
rollup ./src/index.js --format iife 輸出的是瀏覽器支援的iife格式 yarn rollup
./src/index.js --format iife --file dist/bundle.js 輸出到dist資料夾下
配置檔案
rollup.config.js
export default {
input: 'src/index.js', //
output: {
file: 'dist/bundle.js',
format: 'iife'
}
}
打包命令
yarn rollup --config 啟動打包配置檔案
yarn rollup --config rollup.config.js 啟動不同配置檔名稱
使用外掛
匯入commonJS模組,或邊緣ECMAScript的新特性
外掛是Rollup唯一的擴充套件路徑
rollup-plugin-json
yarn add rollup-plugin-json --dev
import json from 'rollup-plugin-json'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json()
]
}
src/index.js
// 匯入模組成員
import { log } from './logger'
import messages from './messages'
import { name, version } from '../package.json'
// 使用模組成員
const msg = messages.hi
log(msg)
log(name)
log(version)
載入npm 模組
yarn add rollup-plugin-node-resolve --dev
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json(),
resolve()
]
}
src/index.js
// 匯入模組成員
import _ from 'lodash-es' //匯入lodash模組
import { log } from './logger'
import messages from './messages'
import { name, version } from '../package.json'
// 使用模組成員
const msg = messages.hi
log(msg)
log(name)
log(version)
log(_.camelCase('hello world'))
載入CommonJS模組
yarn add rollup-plugin-commonjs --dev
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json(),
resolve(),
commonjs()
]
}
cjs-module.js
module.exports = {
foo: 'bar'
}
index.js
// 匯入模組成員
import _ from 'lodash-es'
import { log } from './logger'
import messages from './messages'
import { name, version } from '../package.json'
import cjs from './cjs-module'
// 使用模組成員
const msg = messages.hi
log(msg)
log(name)
log(version)
log(_.camelCase('hello world'))
log(cjs)
程式碼拆分
yarn rollup --config --format amd
import('./logger').then(({ log }) => {
log('code splitting~')
})
export default {
input: 'src/index.js',
output: {
// file: 'dist/bundle.js',
// format: 'iife'
dir: 'dist',
format: 'amd'
}
}
多入口打包
export default {
// input: ['src/index.js', 'src/album.js'],
input: {
foo: 'src/index.js',
bar: 'src/album.js'
},
output: {
dir: 'dist',
format: 'amd'
}
}
yarn rollup --config
選用原則 Rollup/Webpack
輸出結果更扁平
自動移除未引用程式碼
打包結果依然完全可讀
缺點
載入非ESM的第三方模組比較複雜
模組最終都被打包到一個函式中,
無法實現HMR(模組熱替換) 瀏覽器環境中,
程式碼拆分功能依賴AMD庫
如果不適合大量引用第三方庫,引用html,分包
如果開發框架或者一個類庫,比較適合,大多知名框架、庫打包使用rollup
Parcel
零配置的前端應用打包器
只需要瞭解簡單幾個命令就可以使用打包方案
yarn init
yarn add parcel-bundler --dev
yarn pracel src/index.html 開啟一個開發伺服器,並開始執行打包
模組熱替換
自動安裝依賴
直接匯入寫,儲存檔案後自動安裝 支援動態匯入
// import $ from 'jquery'
import foo from './foo'
import './style.css'
import logo from './zce.png'
foo.bar()
import('jquery').then($ => {
$(document.body).append('<h1>Hello Parcel</h1>')
$(document.body).append(`<img src="${logo}" />`)
})
if (module.hot) {
module.hot.accept(() => {
console.log('hmr')
})
}
生產模式打包
yarn parcel build src/index.html
相關文章
- 打包工具-Parcel 的使用
- 全新打包工具-Parcel介紹
- parcel打包vueVue
- 打包工具 rollup.js 入門教程JS
- 使用Rollup打包JavaScriptJavaScript
- 使用 rollup 打包 JSJS
- Parcel 打包示例 – React HelloWorldReact
- Vue嚐鮮快速、零配置的打包工具—parcel~Vue
- Vue嚐鮮快速、零配置的打包工具---parcel~Vue
- 詳解Parcel:快速,零配置web應用打包工具Web
- 使用 web 應用打包工具 Parcel 實現程式碼分割Web
- 全新打包工具parcel零配置vue開發腳手架Vue
- 記錄從vuecli打包庫遷移到rollup打包Vue
- Parcel 打包器簡單使用記錄
- 元件庫rollup打包體積優化元件優化
- 深入學習rollup來進行打包
- 模組化打包工具-Webpack外掛與其他功能Web
- 使用rollup打包庫的一種基本配置
- vite的專案,使用 rollup 打包的方法Vite
- 學習rollup.js模組檔案打包JS
- Rollup處理並打包JS檔案專案例項JS
- 更優雅的使用 Parcel 進行前端專案的打包前端
- [譯]關於 Parcel 你所需要知道的一切:快速的 Web 應用打包工具Web
- 為什麼說rollup比webpack更適合打包庫Web
- 其他工具的使用
- 從 webpack 到全面擁抱 Parcel #1 探索 ParcelWeb
- 實用程式包utils - 基於Rollup打包輸出各模組檔案(二)
- 冗餘程式碼都走開——前端模組打包利器 Rollup.js 入門前端JS
- Parcel Vs WebpackWeb
- cordova打包工具
- [Developer] RollupDeveloper
- 【ROLLUP】Oracle分組函式之ROLLUP魅力Oracle函式
- 10分鐘快速精通rollup.js——Vue.js原始碼打包原理深度分析Vue.js原始碼
- 從 webpack 到全面擁抱 Parcel #2 讓 Parcel 支援 vueWebVue
- Parcel上手實踐
- Terraform與其他工具對比ORM
- rollup函式函式
- DMG Canvas for macDMG打包工具CanvasMac