-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
183 lines (162 loc) · 5.29 KB
/
rollup.config.js
File metadata and controls
183 lines (162 loc) · 5.29 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
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
import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import common from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import babel from 'rollup-plugin-babel';
import typescript from 'rollup-plugin-typescript2';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
const GenRollupConfig = task => {
const {
input,
output,
format,
name,
packageName,
useBabel,
} = task;
return {
input : input,
plugins: [
postcss({
extract : true,
modules : false,
minimize: false,
plugins : [
autoprefixer({
env : 'production',
grid : 'autoplace',
overrideBrowserslist: [ 'last 2 version' ],
}),
],
}),
typescript({
tsconfigOverride: {
compilerOptions: {
module: 'ES2015',
target: 'ES2015',
outDir: path.resolve(__dirname, `packages/${ packageName }/lib`),
rootDir: path.resolve(__dirname, `packages/${ packageName }/src`),
// rootDirs: [ path.resolve(__dirname, `packages/*/src`) ],
paths: null,
},
include: [ `packages/${ packageName }/src/*.ts` ],
},
}),
resolve({
mainFields: [
'browser',
'module',
'main',
],
// customResolveOptions: { moduleDirectory: 'node_modules' },
browser: true, // 适配需要加载browser 模块的包
}),
json(),
useBabel && babel({
runtimeHelpers: true,
extensions : [
'.js',
'.ts',
],
}),
common({
include: [
'./node_modules/**',
`./packages/${ packageName }/node_modules/**`,
], // 包括
exclude : [], // 排除
extensions: [
'.js',
'.ts',
],
}),
replace({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }),
],
output: {
file : output,
format : format,
name : name,
compact : false,
banner : '/* Created By @dking/hasaki-cli */',
footer : '/* hasaki-cli git: https://github.com/JohnApache/hasaki-cli */',
extend : false,
sourcemap: false,
},
treeshake: { moduleSideEffects: true },
};
};
const USE_BABEL = true;
const Packages = [
{
name : 'movable',
libName: 'Movable',
umd : true,
esm : true,
},
// 'utils',
// 'core',
// 'svg-icons',
// 'video',
// 'video-play-button',
// 'video-control',
// {
// name : 'ttplayer',
// libName: 'TTPlayer',
// umd : true,
// esm : false,
// },
// {
// name: 'resize-svg-path-by-viewbox',
// umd : true,
// esm : false,
// },
];
const PascalCase = str => {
const regex = /-(\w)/g;
const newStr = str.replace(regex, (match, group1) => group1.toUpperCase());
return newStr.charAt(0).toUpperCase() + newStr.slice(1);
};
const GenTask = pkgInfo => {
const {
packageName, isEsm, libName,
} = pkgInfo;
const LibraryName = libName && libName.length > 0 ? PascalCase(libName) : `TTPlayer${ PascalCase(packageName) }`;
return {
packageName: packageName,
input : path.resolve(__dirname, `./packages/${ packageName }/src/index.ts`),
output : path.resolve(__dirname, `./packages/${ packageName }/lib/index${ isEsm ? '.esm' : '' }.js`),
name : LibraryName,
format : isEsm ? 'es' : 'umd',
useBabel : USE_BABEL && !isEsm,
};
};
const BuildTasks = Packages.reduce((prev, cur) => {
let packageName = '';
let libName = ''; // umd格式库名
let BuildESMTask = true; // 是否开启监视 输出 esm 模式任务
let BuildUMDTask = false; // 是否开启监视 输出 umd 模式任务
if (typeof cur === 'string') {
packageName = cur.trim();
} else {
packageName = (cur.name || '').trim();
libName = (cur.libName || '').trim();
BuildESMTask = typeof cur.esm === 'undefined' ? true : !!cur.esm;
BuildUMDTask = typeof cur.umd === 'undefined' ? false : !!cur.umd;
}
const pkgOption = {
packageName: packageName,
libName : libName,
};
if (BuildESMTask) {
const esmTask = GenTask({ ...pkgOption, isEsm: true });
prev.push(GenRollupConfig(esmTask));
}
if (BuildUMDTask) {
const umdTask = GenTask({ ...pkgOption, isEsm: false });
prev.push(GenRollupConfig(umdTask));
}
return prev;
}, []);
export default BuildTasks;