手动打包
2023年03月04日
Rollup
提供了可在 Node.js
中使用的 JavaScript API
。一般情况下不需要使用它,而应使用命令行 API
,除非你要扩展 Rollup
本身或者使用它进行一些高级操作,比如通过编程生成 bundle
。 下面为大家展示如何以编程的方式生成bundle
。
构建
src/index.ts
import "./index.scss";
import $ from "jquery";
import { getName } from "util/index";
import { getName1 } from "util/index1";
import cloneDeep from "lodash/cloneDeep";
import underscore from "underscore";
const test = () => {
return process.env.NODE_ENV;
};
const envStr = test();
console.log(envStr);
const nameStr = getName();
console.log(nameStr);
const nameStr1 = getName1();
console.log(nameStr1);
const promiseFunc = () => {
return new Promise((resolve, reject) => {
resolve(100);
});
};
const asyncFunc = async () => {
const result = await promiseFunc();
console.log(result);
};
asyncFunc();
const data = {
name: "dataName",
};
const dataCopy = cloneDeep(data);
dataCopy.name = "dataCopyName";
console.log(data);
console.log(dataCopy);
const dataCopy1 = underscore.clone(data);
dataCopy1.name = "dataCopy1Name";
console.log(data);
console.log(dataCopy1);
console.log($(".box"));
package.json
{
"name": "auto",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "node scripts/build.js"
},
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.21.0",
"@rollup/plugin-alias": "^4.0.3",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-html": "^1.0.2",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.14.6",
"babel-plugin-lodash": "^3.3.4",
"node-sass": "^8.0.0",
"postcss": "^8.4.21",
"rollup": "^3.18.0",
"rollup-plugin-livereload": "^2.0.5",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-serve": "^2.0.2",
"sass": "^1.58.3",
"tslib": "^2.5.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@types/jquery": "^3.5.16",
"@types/lodash": "^4.14.191",
"@types/underscore": "^1.11.4",
"jquery": "^3.6.3",
"lodash": "^4.17.21",
"underscore": "^1.13.6"
}
}
tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"strict": true,
"newLine": "LF",
"allowJs": true,
"types": ["node"],
"jsx": "preserve",
"target": "es2015",
"module": "ESNext",
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"lib": ["es2015", "esnext", "dom"],
"forceConsistentCasingInFileNames": true,
"paths": {
"util/*": ["src/util/*"],
}
},
"include": ["src/*"],
"exclude": ["node_modules", "dist"],
}
babel.config.js
const presets = [
[
"@babel/preset-env",
{
modules: false,
},
],
"@babel/preset-typescript",
];
const plugins = [["lodash"]];
module.exports = {
presets,
plugins,
};
scripts/build.js
const rollup = require("rollup");
const rollupOptions = require("./config");
const inputOptions = { ...rollupOptions };
const outputOptions = { ...rollupOptions.output };
const watchOptions = {
...inputOptions,
output: [outputOptions],
};
async function build() {
const bundle = await rollup.rollup(inputOptions);
await bundle.write(outputOptions);
rollup.watch(watchOptions);
}
build();
scripts/config.js
const path = require("path");
const RollupPluginHtml = require("@rollup/plugin-html");
const RollupPluginJson = require("@rollup/plugin-json");
const RollupPluginServe = require("rollup-plugin-serve");
const RollupPluginAlias = require("@rollup/plugin-alias");
const RollupPluginBabel = require("@rollup/plugin-babel");
const RollupPluginTerser = require("@rollup/plugin-terser");
const RollupPluginPostcss = require("rollup-plugin-postcss");
const RollupPluginReplace = require("@rollup/plugin-replace");
const RollupPluginCommonjs = require("@rollup/plugin-commonjs");
const RollupPluginLiverLoad = require("rollup-plugin-livereload");
const RollupPluginTypescript = require("@rollup/plugin-typescript");
const RollupPluginNodeResolve = require("@rollup/plugin-node-resolve");
const resolve = (p) => {
return path.resolve(__dirname, "../", p);
};
const version = require("../package.json").version;
const extensions = {
babel: [".ts", ".js"],
common: [".ts", ".tsx", ".js", ".jsx", ".json", ".css", ".sass", ".scss"],
};
const customResolver = RollupPluginNodeResolve({
extensions: extensions.common,
});
const customHtmlTemplate = async ({
attributes,
files,
meta,
publicPath,
title,
}) => {
const scripts = (files.js || [])
.map(({ fileName }) => {
const attrs = RollupPluginHtml.makeHtmlAttributes(attributes.script);
return `<script src="${publicPath}${fileName}"${attrs}></script>`;
})
.join("\n");
const links = (files.css || [])
.map(({ fileName }) => {
const attrs = RollupPluginHtml.makeHtmlAttributes(attributes.link);
return `<link href="${publicPath}${fileName}" rel="stylesheet"${attrs}>`;
})
.join("\n");
const metas = meta
.map((input) => {
const attrs = RollupPluginHtml.makeHtmlAttributes(input);
return `<meta${attrs}>`;
})
.join("\n");
return `
<!doctype html>
<html${RollupPluginHtml.makeHtmlAttributes(attributes.html)}>
<head>
${metas}
<title>${title}</title>
${links}
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/underscore.js/1.13.6/underscore-umd.min.js"></script>
</head>
<body>
${scripts}
</body>
</html>`;
};
const rollupPlugins = [
RollupPluginJson(),
RollupPluginAlias({
entries: [
{ find: "util", replacement: resolve("src/util") },
],
customResolver,
}),
RollupPluginBabel.babel({
babelHelpers: "bundled",
exclude: ["node_modules/**"],
extensions: extensions.babel,
}),
RollupPluginCommonjs(),
RollupPluginNodeResolve.nodeResolve({
extensions: extensions.common,
}),
RollupPluginTypescript({
tsconfig: resolve("tsconfig.json"),
cacheDir: ".rollup.tscache",
}),
RollupPluginReplace({
___VERSION__: version,
preventAssignment: true,
__DEV__: process.env.NODE_ENV === "development",
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
}),
RollupPluginTerser({
maxWorkers: 4,
}),
RollupPluginServe({
open: true,
port: 4000,
contentBase: ["dist"],
}),
RollupPluginLiverLoad({
watch: "dist",
}),
RollupPluginPostcss(),
RollupPluginHtml({
fileName: "index.html",
title: "Rollup 通用开发环境",
template: customHtmlTemplate,
}),
];
module.exports = {
cache: true,
watch: {
include: "src/**",
exclude: "node_modules/**"
},
input: resolve("src/index.ts"),
output: {
file: path.resolve("dist/index.js"),
format: "umd",
banner: `/** Rollup 通用开发环境 版本 v${version} **/`,
name: "RollupName",
exports: "auto",
globals: {
jquery: "$",
underscore: "_",
},
},
plugins: rollupPlugins,
external: ["jquery", "underscore"],
};
命令行
-
初始化项目: 通过
yarn init -y
初始化项目 -
配置命令行: 通过
package.json - scripts
配置命令行 -
配置
rollup
命令行
"scripts": {
"build": "rollup --watch --config rollup.config.js --environment NODE_ENV:development"
}
替换变量
Rollup
通过 @rollup/plugin-replace
构建产物时替换变量
相关代码
const RollupPluginReplace = require("@rollup/plugin-replace");
RollupPluginReplace({
___VERSION__: version,
preventAssignment: true,
__DEV__: process.env.NODE_ENV === "development",
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
})