Repository navigation
Expand file tree
/
Copy pathrollup.config.js
More file actions
149 lines (141 loc) · 4.64 KB
/
Copy pathrollup.config.js
File metadata and controls
149 lines (141 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import fs from "node:fs/promises";
import path from "node:path";
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
// 1. 核心替换:引入rollup-plugin-postcss(纯CSS+PostCSS专用)
import postcss from "rollup-plugin-postcss";
import replace from "@rollup/plugin-replace";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import { glob } from "glob";
import { stripIndent } from "common-tags";
// 保留PostCSS插件引入(已安装,直接复用)
import autoprefixer from "autoprefixer";
// 引入cssnano(PostCSS生态,纯CSS压缩专用)
import cssnano from "cssnano";
// Plugins 遍历逻辑【完全不变】
const pluginPaths = await glob(["src/*/index.js"]);
const pluginConfigs = pluginPaths.map((pluginPath) => {
const dir = path.basename(path.dirname(pluginPath));
const name = path.basename(pluginPath, ".js");
const outputName = name === "index" ? dir : name;
return {
inputPath: pluginPath,
outputDir: `dist/${outputName}`, // CSS/JS仍输出到该同层级目录
outputName:`docsify-${outputName}`, // 文件名保持一致,仅后缀不同
title: `Docsify Plugin: ${outputName}`,
};
});
// Rollup 基础配置【完全不变】
const currentYear = new Date().getFullYear();
const { homepage, license, version } = JSON.parse(
await fs.readFile(path.join(import.meta.dirname, "package.json"), "utf8"),
);
const baseConfig = {
output: {
format: "iife",
},
plugins: [
resolve(),
commonjs(),
replace({
preventAssignment: true,
values: {
__VERSION__: version,
},
}),
babel({
babelHelpers: "bundled",
}),
],
watch: {
clearScreen: false,
},
};
const bundleConfigs = [];
// 生成打包配置【仅修改CSS处理部分,其余完全不变】
[...pluginConfigs].forEach((bundleConfig) => {
const { inputPath, outputDir, outputName, title } = bundleConfig;
const banner = stripIndent`
/*!
* ${title} v${version}
* ${homepage}
* (c) 2017-${currentYear}
* ${license} license
*/
`;
// 压缩版配置(${outputName}.min.js + ${outputName}.min.css,纯CSS+PostCSS处理)
const minifiedConfig = {
...baseConfig,
input: inputPath,
output: {
...baseConfig.output,
banner,
file: path.join(outputDir, `${outputName}.min.js`),
sourcemap: true,
},
plugins: [
...baseConfig.plugins,
// 2. 配置压缩版CSS:纯CSS+PostCSS+压缩,与min.js同层级
postcss({
extract: `${outputName}.min.css`, // 核心:提取独立CSS,路径与min.js完全同层级
sourceMap: true, // 生成CSS SourceMap(与JS保持一致)
plugins: [
autoprefixer({
// PostCSS核心插件:自动补全浏览器前缀(配置可保留/移至postcss.config.js)
overrideBrowserslist: ["last 2 versions", "> 1%", "not dead"],
grid: true,
}),
// 纯CSS压缩:使用cssnano(PostCSS生态,比通用压缩更适配CSS)
cssnano({
preset: "default", // 默认压缩配置,兼顾体积和兼容性
}),
],
minimize: false, // 关闭插件内置压缩,改用cssnano精准控制(推荐)
}),
terser({
output: {
comments: /^!/,
},
}),
],
};
// 未压缩版配置(${outputName}.js + ${outputName}.css,纯CSS+PostCSS处理,不压缩)
const unminifiedConfig = {
...baseConfig,
input: inputPath,
output: {
...baseConfig.output,
banner,
file: path.join(outputDir, `${outputName}.js`),
},
plugins: [
...baseConfig.plugins,
// 3. 配置未压缩版CSS:纯CSS+PostCSS,格式化输出,与js同层级
postcss({
extract: `${outputName}.css`, // 核心:提取独立CSS,路径与js完全同层级
plugins: [
autoprefixer({
// 未压缩版与压缩版PostCSS插件配置完全一致,保证样式处理逻辑统一
overrideBrowserslist: ["last 2 versions", "> 1%", "not dead"],
grid: true,
}),
// 未压缩版:不添加cssnano,保留格式化CSS
],
minimize: false, // 关闭压缩
sourceMap: false, // 未压缩版可选关闭SourceMap(也可开启,根据需求调整)
}),
terser({
compress: false,
mangle: false,
output: {
beautify: true,
comments: /^!/,
},
}),
],
};
bundleConfigs.push(minifiedConfig, unminifiedConfig);
});
// 导出配置【完全不变】
export default [...bundleConfigs];