-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollup.config.js
More file actions
125 lines (116 loc) · 3.4 KB
/
rollup.config.js
File metadata and controls
125 lines (116 loc) · 3.4 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
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import babel from "@rollup/plugin-babel";
import terser from "@rollup/plugin-terser";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import postcss from "rollup-plugin-postcss";
import alias from "@rollup/plugin-alias";
import svelte from "rollup-plugin-svelte";
import sveltePreprocess from "svelte-preprocess";
import image from "@rollup/plugin-image";
const isProduction = process.env.NODE_ENV === "production";
/**
* Rollup 配置文件
*/
export default {
onwarn(warning, warn) {
// 忽略 d3-selection 的循环依赖警告
if (
warning.code === "CIRCULAR_DEPENDENCY" &&
warning.message.includes("node_modules")
) {
return;
}
// 其他警告照常处理
warn(warning);
},
// 入口文件
input: "src/main.js",
// 输出配置
output: [
{
file: "public/dist/smart-finereport.esm.min.js",
format: "esm",
sourcemap: true,
},
{
file: "public/dist/smart-finereport.cjs.min.js",
format: "cjs",
sourcemap: true,
},
],
// 插件列表
plugins: [
// 解析 node_modules 中的模块
resolve(),
// 配置别名
alias({
entries: [{ find: "@", replacement: process.cwd() + "/src" }],
}),
// 图片内联打包插件
image(),
// 转换 CommonJS 模块为 ES6
commonjs(),
// Babel 转换
babel({
babelHelpers: "bundled",
exclude: "node_modules/**",
}),
// Svelte 插件
svelte({
preprocess: sveltePreprocess({
postcss: true,
}),
emitCss: true, // 提取 Svelte 组件中的 CSS
compilerOptions: {
dev: !isProduction,
hydratable: false,
enableSourcemap: !isProduction,
},
onwarn: (warning, handler) => {
// 忽略一些常见的警告
if (warning.code === "css-unused-selector") return;
handler(warning);
},
}),
// PostCSS plugin to handle Tailwind CSS
postcss({
extract: (outputChunk) => {
// outputChunk.fileName will be like 'smart-finereport.esm.min.js' or 'smart-finereport.cjs.min.js'
// We want to replace '.js' with '.css'
return outputChunk.fileName.replace(/\.js$/, ".css");
},
minimize: isProduction, // Minimize CSS in production
sourceMap: !isProduction,
}),
// 生产环境下启用代码压缩
isProduction && terser(),
// 开发服务器配置 (仅在开发环境下生效)
!isProduction &&
serve({
open: true, // 自动在浏览器中打开
contentBase: ["public"], // 静态文件目录
host: "localhost",
port: 8080,
// 将 bundle.js 映射到正确的输出文件
onListening: function (server) {
console.log("Server listening at http://localhost:8080/");
const { address, port } = server.address();
const host = address === "0.0.0.0" ? "localhost" : address;
const url = `http://${host}:${port}`;
console.log(`Open your browser and go to ${url}/index.html`);
},
}),
// 实时重新加载 (仅在开发环境下生效)
!isProduction &&
livereload({
watch: ["public"],
}),
],
// 监视文件变化 (仅在开发环境下生效)
watch: {
include: "src/**",
exclude: "node_modules/**",
},
};