-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
139 lines (132 loc) · 3.62 KB
/
rollup.config.js
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
// rollup.config.js
import typescript from '@rollup/plugin-typescript';
import {defineConfig} from "rollup";
import alias from "@rollup/plugin-alias";
import path from "path";
import {fileURLToPath} from "url";
import copy from "rollup-plugin-copy";
import watchAssets from "rollup-plugin-watch-assets";
import terser from '@rollup/plugin-terser';
import {execSync} from "child_process";
import fs from "fs";
import babel from '@rollup/plugin-babel';
import postcss from 'rollup-plugin-postcss';
import postcssImport from 'postcss-import';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
// 将 import.meta.url 转换为当前文件的绝对路径
const __filename = fileURLToPath(import.meta.url);
// 使用 path.dirname 获取当前文件所在目录的路径,类似于 __dirname
const __dirname = path.dirname(__filename);
const isProd = !process.env.ROLLUP_WATCH;
export default defineConfig([{
input: 'src/index.ts',
watch: {
include: ['src/**'],
exclude: ['src/assets/**']
},
output: {
dir: 'dist',
format: 'es',
sourcemap: isProd ? false : 'inline'
},
plugins: [
isProd && {
name: 'clear-dist',
buildStart() {
const distPath = path.resolve(__dirname, 'dist');
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, {recursive: true, force: true});
console.info('cleared dist directory');
}
}
},
typescript({
compilerOptions: {
sourceMap: !isProd
}
}),
alias({
entries: [
{find: '@', replacement: path.resolve(__dirname, 'src')}
]
}),
!isProd && watchAssets({
assets: ['src/package.json']
}),
isProd && terser({
compress: {
pure_funcs: ['console.debug']
}
}),
copy({
targets: [
{src: 'src/configs/DefaultConfig.js', dest: 'dist', rename: 'default.config.js'}
]
}),
isProd && {
name: 'generate-package-lock',
async generateBundle() {
// 把本项目中的 package.json 中的 dependencies 复制到 dist 目录的 package.json 中
const packageJson = (await import('./package.json', {with: {type: 'json'}})).default;
const srcPackageJson = (await import('./src/package.json', {with: {type: 'json'}})).default;
['description', 'engines', 'repository', 'bugs', 'author', 'license', 'keywords', 'dependencies'].forEach(key => {
srcPackageJson[key] = packageJson[key];
});
fs.writeFileSync('dist/package.json', JSON.stringify(srcPackageJson, null, 2));
execSync('npm install --package-lock-only', {cwd: 'dist'});
console.info('generated package-lock.json');
}
}
].filter(Boolean),
external: [
// nodejs内建模块
'url', 'path', 'child_process', 'http', 'crypto', 'events', 'stream', 'fs', 'readline',
// 第三方模块
'express', 'body-parser', 'multer', 'range-parser', 'mime/lite', 'commander', 'chokidar', 'tinyqueue', 'ws'
] // 外部依赖,不会被打包
}, {
input: [
'src/assets/index.js',
'src/assets/preload.js',
'src/assets/service-worker.js'
],
watch: {
include: ['src/assets/**']
},
output: {
dir: 'dist/assets',
format: 'esm',
sourcemap: isProd ? false : 'inline'
},
plugins: [
!isProd && watchAssets({
assets: [
'src/assets/favicon.ico',
'src/assets/tmpl',
'src/assets/png'
]
}),
resolve(),
commonjs(),
postcss({
plugins: [
postcssImport()
],
extract: true,
minimize: isProd
}),
isProd && babel({
babelHelpers: 'bundled'
}),
isProd && terser(),
copy({
targets: [
{src: 'src/assets/favicon.ico', dest: 'dist/assets'},
{src: 'src/assets/tmpl', dest: 'dist/assets'},
{src: 'src/assets/png', dest: 'dist/assets'},
{src: 'README.md', dest: 'dist'}
]
})
]
}]);