-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
115 lines (112 loc) · 2.37 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
import resolve from 'rollup-plugin-node-resolve'; // 帮助寻找node_modules里的包
import replace from '@rollup/plugin-replace'; // 替换待打包文件里的一些变量,如 process在浏览器端是不存在的,需要被替换
import commonjs from 'rollup-plugin-commonjs'; // 将非ES6语法的包转为ES6可用
import babel from 'rollup-plugin-babel'; // rollup 的 babel 插件,ES6转ES5
import typescriptPlugin from 'rollup-plugin-typescript2';
import { terser } from 'rollup-plugin-terser'; // 压缩包
import typescript from 'typescript';
import pkg from './package.json';
const isProd = process.env.NODE_ENV === 'production';
const env = process.env.NODE_ENV;
const globals = {
react: 'React',
'react-dom': 'ReactDOM',
};
const extensions = ['tsx', '.ts', 'js', 'jsx'];
const getBabelOptions = ({ useESModules }) => ({
include: ['src/**/*'],
exclude: '**/node_modules/**',
runtimeHelpers: true,
presets: [
[
'@babel/preset-typescript',
{
isTSX: true,
allExtensions: true,
},
],
],
extensions,
plugins: [
[
'@babel/transform-runtime',
{
regenerator: false,
useESModules,
},
],
],
});
export default [
isProd && {
input: 'src/index.ts',
output: {
file: `dist/${pkg.module}`,
exports: 'named',
format: 'esm',
},
external: Object.keys(globals),
plugins: [
resolve({ extensions }),
commonjs(),
typescriptPlugin({
exclude: 'node_modules/**',
typescript,
}),
babel(
getBabelOptions({
useESModules: true,
}),
),
],
},
isProd && {
input: 'src/index.ts',
output: {
file: `dist/${pkg.main}`,
exports: 'named',
format: 'cjs',
},
external: Object.keys(globals),
plugins: [
resolve({ extensions }),
commonjs(),
typescriptPlugin({
exclude: 'node_modules/**',
typescript,
}),
babel(
getBabelOptions({
useESModules: false,
}),
),
],
},
{
input: 'src/index.ts',
output: {
file: `dist/${pkg.unpkg}`,
name: 'ReactLazyLoadImage',
format: 'umd',
globals,
},
external: Object.keys(globals),
plugins: [
resolve({ extensions }),
commonjs(),
typescriptPlugin({
exclude: 'node_modules/**',
typescript,
}),
babel(
getBabelOptions({
useESModules: true,
}),
),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
}),
isProd && terser(),
].filter(Boolean),
},
].filter(Boolean);