-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
88 lines (74 loc) · 2.09 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
import path from 'path';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import filesize from 'rollup-plugin-filesize';
// eslint-disable-next-line import/extensions
import pkg from './package.json';
const deps = [...Object.keys(pkg.peerDependencies || {}), ...Object.keys(pkg.dependencies || {})];
const external = (id) => deps.includes(id) || /@babel\/runtime\//.test(id);
const inputPath = path.join(__dirname, 'src', 'index.ts');
const OUTPUT_DIR = 'dist';
const getBasicConf = () => ({
input: inputPath,
external,
});
const getDistDir = (moduleType) => {
switch (moduleType) {
case 'umd':
case 'cjs':
return OUTPUT_DIR;
case 'es':
return path.join(OUTPUT_DIR, 'es');
default:
throw Error('Unknown Module Type');
}
};
const getConf = (env, moduleType) => {
const distLib = getDistDir(moduleType);
const fileName = moduleType === 'umd' ? `${pkg.name}.browser.js` : `${pkg.name}.${env}.js`;
const outputPath = path.join(distLib, fileName);
const isProd = env === 'production';
const outputDeclaration = moduleType === 'cjs'; // for only declaration once
const plugins = [
typescript({
compilerOptions: {
declaration: outputDeclaration, // only es format output declaration
declarationDir: outputDeclaration ? path.join(OUTPUT_DIR, 'types') : undefined,
},
exclude: [
// exclude test files
'**/__tests__/**',
'**/*.test.*',
'**/*.spec.*',
// exclude stories files
'**/stories/**',
'**/*.stories.*',
'**/storybook-decorators/**',
],
}),
filesize(),
];
if (isProd) {
plugins.push(terser());
}
return {
...getBasicConf(),
output: {
file: outputPath,
format: moduleType,
sourcemap: true,
// for umd
name: 'AMapReact',
globals: {
react: 'React',
'@amap/amap-jsapi-loader': 'AMapLoader',
},
},
plugins,
};
};
export default [
getConf('production', 'cjs'),
getConf('production', 'es'),
getConf('production', 'umd'),
];