vue3 Vite 進階rollup命令行使用詳解

rollup介紹

  • 開源類庫優先選擇
  • 以 ESM 標準為目標的構建工具
  • Tree Shaking

以命令行方式打包

  • 安裝 rollup
npm install -g rollup
  • 創建 index.js 文件
import path from "path";
console.log("hello rollop", path.join("", "hello"));
  • 打包
rollup -i index.js --file dist.js --format umd
  • –file:打包輸出文件
  • –format:打包格式(umd, cjs, es, iife)

Tree Shaking

隻會打包我們用到的代碼,沒有用到的不會打包

  • 新建 a.js 文件
export const funA = () => {
  console.log("a");
};
export const funB = () => {
  console.log("b");
};
  • index.js 引入 a.js
import { funA } from "./a";
funA();
console.log("hello rollup");
  • 打包文件
rollup -i index.js --file dist.js --format es

輸出代碼,代碼進行瞭合並,並且隻打包瞭用到的代碼

const funA = () => {
  console.log("a");
};
funA();
console.log("hello rollop");

Rollup 的命令行使用

index.js 文件

import path from "path";
import { funA } from "./a";
funA();
console.log("hello rollop", path.join(__dirname, "/hello"));
export const x = 12;

a.js 文件

export const funA = () => {
  console.log("a");
};
export const funB = () => {
  console.log("b");
};

命令行

rollup [options] <entry file>  選項 輸入文件
--help 幫助文檔
-v, --version 查看版本
-i, --input <filename> 輸入單個文件
-f, --format <format> 輸出格式
-o, --file <output>  輸出單個文件
-d, --dir <dirname>  輸出多個文件
-w, --watch 監聽文件改變,並重新打包
-c, --config <filename> 指定配置文件使用
--environment <values>  指定環境變量
  • 輸出多個文件
rollup -i index.js -i a.js --dir dist

format 格式

  • iife 輸出自執行函數
rollup -i index.js --format iife
index.js → stdout...
Creating a browser bundle that depends on "path". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
var index = (function (exports, path) {
  'use strict';
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
  const funA = () => {
    console.log("a");
  };
  funA();
  console.log("hello rollop", path__default["default"].join(__dirname, "/hello"));
  const x = 12;
  exports.x = x;
  Object.defineProperty(exports, '__esModule', { value: true });
  return exports;
})({}, path);
  • cjs 輸出 commonJs 格式
rollup -i index.js --format cjs
index.js → stdout...
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
const funA = () => {
  console.log("a");
};
funA();
console.log("hello rollop", path__default["default"].join(__dirname, "/hello"));
const x = 12;
exports.x = x;
  • es 輸出 esModule 格式
rollup -i index.js --format es
index.js → stdout...
import path from 'path';
const funA = () => {
  console.log("a");
};
funA();
console.log("hello rollop", path.join(__dirname, "/hello"));
const x = 12;
export { x };
  • umd 輸出兼容 iife、cjs、es 格式的文件
rollup -i index.js --format umd --name index
index.js → stdout...
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path')) :
  typeof define === 'function' && define.amd ? define(['exports', 'path'], factory) :
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.index = {}, global.path));
})(this, (function (exports, path) { 'use strict';
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
  const funA = () => {
    console.log("a");
  };
  funA();
  console.log("hello rollop", path__default["default"].join(__dirname, "/hello"));
  const x = 12;
  exports.x = x;
  Object.defineProperty(exports, '__esModule', { value: true });
}));
  • umd 格式要指明 全局變量名 –name
 rollup -i index.js --file dist.js --format umd --name index

rollup.config.js

export default {
  input: "index.js",
  output: {
    file: "dist.js",
    format: "umd",
    name: "index",
  },
};
  • 執行配置文件
rollup --config rollup.config.js

設置/獲取環境變量

在配置文件中獲取

// rollup.config.js
console.log(process.env.MODE);
const mode = process.env.MODE;
const isLocal = mode === "local";
export default {
  input: "index.js",
  output: {
    file: "dist.js",
    format: isLocal ? "es" : "umd",
    name: "index",
  },
};
  • 執行命令
rollup --config rollup.config.js --environment MODE:local

插件 plugins

插件官網:github.com/rollup/plug…

  • 修改 index.js
import path from "path";
import { funA } from "./a";
import pkg from "./package.json";
console.log(pkg);
funA();
console.log("hello rollop", path.join(__dirname, "/hello"));
export const x = 12;
  • json 文件轉為 esModule
npm install @rollup/plugin-json --save-dev 
npm install rollup
  • 由於 json 插件是安裝在本地,所以執行本地的 rollup 來找到對應的插件
./node_modules/.bin/rollup --config rollup.config.js --plugin json

以上就是vue3 Vite 進階rollup命令行使用詳解的詳細內容,更多關於vue3 Vite進階rollup命令行的資料請關註WalkonNet其它相關文章!

推薦閱讀: