-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
141 lines (129 loc) · 3.67 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
140
141
// import fs from 'fs-extra';
const fs = require('fs-extra');
// import path from 'path';
const path = require('path');
// import commonjs from '@rollup/plugin-commonjs';
const commonjs = require('@rollup/plugin-commonjs');
// import json from '@rollup/plugin-json';
const json = require('@rollup/plugin-json');
// import { terser } from 'rollup-plugin-terser';
const { terser } = require('rollup-plugin-terser');
// import replace from '@rollup/plugin-replace';
const replace = require('@rollup/plugin-replace');
// import svelte from 'rollup-plugin-svelte';
const svelte = require('rollup-plugin-svelte');
// import resolve from '@rollup/plugin-node-resolve';
const resolve = require('@rollup/plugin-node-resolve');
const ignorePackage = ['recommended']
const packages = fs.readdirSync(path.resolve(__dirname, 'packages')).filter(key => !ignorePackage.includes(key));
const production = !process.env.ROLLUP_WATCH;
const umd = process.env.UMD;
const configs = packages.map(key => {
const pkg = fs.readJsonSync(`./packages/${key}/package.json`);
if (pkg.private) return [];
const inputFile = path.resolve('packages', key, 'lib/index.js');
/** @type {import('rollup').RollupOptions} */
const common = {
input: inputFile,
sourcemap: false,
plugins: [
commonjs(),
json(),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development')
})
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
console.error(`\n[rollup message]: ${warning.message}\n`);
}
};
if (pkg.svelte) {
common.plugins.concat([
svelte({
compilerOptions: {
// 在非生产环境中开启运行时检查
dev: !production
}
}),
resolve({
browser: true,
dedupe: ['svelte']
})
]);
}
/** @type {import('rollup').RollupOptions} */
const config = {
...common,
output: [
{
format: 'es',
sourcemap: false,
file: path.resolve('packages', key, pkg.module)
},
{
format: 'cjs',
sourcemap: false,
file: path.resolve('packages', key, pkg.main),
exports: 'auto'
},
],
external: [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
};
/** @type {import('rollup').OutputOptions} */
const umdOutputOption = {
format: 'umd',
name: pkg.globalName || pkg.name.replace(/-/g, '_'),
sourcemap: false,
inlineDynamicImports: true,
};
/** @type {import('rollup').RollupOptions} */
const umdConfig = {
...common,
output: [
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.js')
},
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.min.js'),
plugins: [terser()]
}
],
external: Object.keys(pkg.peerDependencies || {})
};
/** @type {import('rollup').RollupOptions} */
const es5Config = {
...common,
input: path.resolve('packages', key, 'lib/index.js'),
output: [
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.es5.js'),
},
{
...umdOutputOption,
file: path.resolve('packages', key, 'dist/index.es5.min.js'),
plugins: [terser()]
}
],
plugins: [
...common.plugins,
// babel({
// babelHelpers: 'runtime',
// extensions: ['.js'],
// })
]
};
if (umd) {
return [config, umdConfig, es5Config];
} else {
return [config];
}
}).flat();
// export default [...configs];
module.exports = [...configs];