-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
190 lines (183 loc) · 6.98 KB
/
rollup.config.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import type { RollupOptions } from 'rollup';
import yargs from "yargs";
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json';
import camelCase from 'lodash.camelcase';
import typescript from 'rollup-plugin-typescript2';
import sourceMaps from "rollup-plugin-sourcemaps";
import vue from "rollup-plugin-vue";
import serve from 'rollup-plugin-serve';
import livereload from "rollup-plugin-livereload";
import postcss from "rollup-plugin-postcss";
import autoprefixer from "autoprefixer";
import postcssImport from "postcss-import";
import postcssUrl from "postcss-url";
import url from "@rollup/plugin-url";
import cssnano from "cssnano";
import vueJsx from '@vitejs/plugin-vue-jsx'
import copy from "rollup-plugin-copy";
import path from 'path';
const pkg = require('./package.json')
// 使用yargs解析命令行执行时的添加参数
const commandLineParameters = yargs(process.argv.slice(1)).options({
// css文件独立状态,默认为内嵌
splitCss: { type: "string", alias: "spCss", default: "false" },
// 打包格式, 默认为 umd,esm,common 三种格式
packagingFormat: {
type: "string",
alias: "pkgFormat",
default: "umd,esm,common"
},
// 打包后的js压缩状态
compressedState: { type: "string", alias: "compState", default: "false" },
// 显示每个包的占用体积, 默认不显示
showModulePKGInfo: { type: "string", alias: "showPKGInfo", default: "false" },
// 是否开启devServer, 默认不开启
useDevServer: { type: "string", alias: "useDServer", default: "false" }
}).parseSync();
// 需要让rollup忽略的自定义参数
// const ignoredWarningsKey = [...Object.keys(commandLineParameters)];
const splitCss = commandLineParameters.splitCss;
const packagingFormat = commandLineParameters.packagingFormat.split(",");
const compressedState = commandLineParameters.compressedState;
const showModulePKGInfo = commandLineParameters.showModulePKGInfo;
const useDevServer = commandLineParameters.useDevServer;
const libraryName = 'Context Menu';
const config: RollupOptions = {
input: [`package/vue3/index.ts`],
output: [
{
file: pkg.main,
name: camelCase(libraryName),
format: 'umd',
sourcemap: true,
globals: {
vue: "Vue" // 告诉rollup全局变量Vue即是vue
}
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
globals: {
vue: "Vue" // 告诉rollup全局变量Vue即是vue
}
},
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: ["vue"],
watch: {
include: 'package/**',
},
plugins: [
vueJsx(),
vue({
target: "browser",
preprocessStyles: true,
compilerOptions: {
comments: false // 删除注释
},
}),
postcss({
// 内联的css
extract: splitCss === "true" ? "style/css/contextmenu.css" : false,
minimize: true,
sourceMap: false,
extensions: [".css", ".less"],
// 当前正在处理的CSS文件的路径, postcssUrl在拷贝资源时需要根据它来定位目标文件
to: path.resolve(__dirname, "dist/assets/*"),
use: ["less"],
// autoprefixer: 给css3的一些属性加前缀
// postcssImport: 处理css文件中的@import语句
// cssnano: 它可以通过移除注释、空格和其他不必要的字符来压缩CSS代码
plugins: [
autoprefixer(),
postcssImport(),
// 对scss中的别名进行统一替换处理
postcssUrl({
filter: "**/*.*",
url(asset) {
return asset.url.replace(/~@/g, ".");
}
}),
// 再次调用将css中引入的图片按照规则进行处理
postcssUrl({
basePath: path.resolve(__dirname, "package"),
url: "inline",
maxSize: 8, // 最大文件大小(单位为KB),超过该大小的文件将不会被编码为base64
fallback: (
asset: {
url: string; pathname?: string | undefined;
absolutePath?: string | undefined;
relativePath?: string | undefined;
search?: string | undefined;
hash?: string | undefined;
},
dir: {
from?: string | undefined;
to?: string | undefined;
file?: string | undefined;
}) => {
return "copy"
}, // 如果文件大小超过最大大小,则使用copy选项复制文件
useHash: true, // 进行hash命名
encodeType: "base64" // 指定编码类型为base64
} as any),
cssnano({
preset: "default" // 使用默认配置
})
]
}),
// 处理通过img标签引入的图片
url({
include: ["**/*.jpg", "**/*.png", "**/*.svg"],
// 输出路径
destDir: "dist/assets",
// 超过10kb则拷贝否则转base64
limit: 10 * 1024 // 10KB
}),
// Allow json resolution
json(),
// Compile TypeScript files
typescript({
tsconfig: "tsconfig.json",
tsconfigOverride: {
compilerOptions: {
// dev模式下不生成.d.ts文件
declaration: useDevServer !== "true",
// 指定目标环境为es5
target: "es5"
}
},
clean: true
}),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),
// Resolve source maps to the original source
sourceMaps(),
serve({
// open: true,
// openPage: "/index.html",
port: 8123,
contentBase: "dist"
}),
// watch dist目录,当目录中的文件发生变化时,刷新页面
livereload({
watch: ["dist", "public"]
}),
copy({
targets: [
{
src: "public/**",
dest: "dist"
}
]
})
],
};
export default config;