-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
126 lines (111 loc) · 3.19 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
import path from 'path';
import replace from 'rollup-plugin-replace';
import json from '@rollup/plugin-json';
import typescript from 'rollup-plugin-typescript2';
import {terser} from 'rollup-plugin-terser';
const packageDir = path.resolve(__dirname);
const resolve = p => path.resolve(packageDir, p);
const entryFile = resolve('src/index.ts');
const pkg = require(resolve(`package.json`));
const outputConfigs = {
esm: {
file: resolve(`dist/plugin.esm.js`),
format: 'es',
},
cjs: {
file: resolve(`dist/plugin.cjs.js`),
format: 'cjs',
},
};
// ts检查优化
let hasTypesChecked = false;
const defaultFormats = ['esm', 'cjs'];
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split('-');
const packageFormats = inlineFormats || defaultFormats;
const packageConfigs = [];
if (!process.env.PROD_ONLY) {
packageFormats.forEach(format => {
if (!outputConfigs[format]) return;
packageConfigs.push(createConfig(format, outputConfigs[format]));
});
}
// 为生产环境创建rollup配置
if (process.env.NODE_ENV === 'production') {
packageFormats.forEach(format => {
if (!outputConfigs[format]) return;
if (format === 'cjs') {
packageConfigs.push(createCjsProductionConfig(format));
}
});
}
function createConfig(format, output, plugins = []) {
if (!output) {
console.log(require('chalk').yellow(`invalid format: "${format}"`));
process.exit(1);
}
output.exports = 'auto';
output.sourcemap = !!process.env.SOURCE_MAP;
output.externalLiveBindings = false;
const shouldEmitDeclaration = process.env.TYPES != null && !hasTypesChecked;
const tsPlugin = typescript({
check: process.env.NODE_ENV === 'production' && !hasTypesChecked,
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
objectHashIgnoreUnknownHack: true,
tsconfigOverride: {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitDeclaration,
declarationMap: shouldEmitDeclaration,
},
exclude: ['**/__tests__'],
},
});
hasTypesChecked = true;
let external = [];
if (format === 'esm' || format === 'cjs') {
external = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})];
} else {
const evaDependencies = Array.from(Object.keys(pkg.dependencies || {})).filter(dep => dep.startsWith('@eva'));
external = ['pixi.js', ...evaDependencies];
}
return {
input: entryFile,
output: output,
external,
plugins: [
json({preferConst: true}),
replace({
__TEST__: false,
__DEV__: process.env.NODE_ENV === 'development',
}),
tsPlugin,
...plugins,
],
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
treeshake: {
moduleSideEffects: false,
},
};
}
function createCjsProductionConfig(format) {
return createConfig(
format,
{
file: resolve(`dist/plugin.${format}.prod.js`),
format: outputConfigs[format].format,
},
[
terser({
toplevel: true,
mangle: true,
compress: true,
}),
],
);
}
export default packageConfigs;