-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
133 lines (120 loc) · 3.7 KB
/
build.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
var path = require('path');
var filesize = require('rollup-plugin-filesize');
var babel = require('rollup-plugin-babel');
var commonjs = require('rollup-plugin-commonjs');
var resolve = require('rollup-plugin-node-resolve');
var replace = require('rollup-plugin-replace');
var uglify = require('rollup-plugin-uglify');
var alias = require('rollup-plugin-alias');
var { rollup } = require('rollup');
var reactDomModulePath = require.resolve('react-dom');
var emptyModulePath = path.resolve(__dirname, 'empty.js');
function build(target, minify) {
var targetExt = minify ? '.min.js' : '.js';
var filename = (function () {
switch (target) {
case 'browser':
return 'index' + targetExt;
default:
throw new Error('Unexpected target: ' + target);
}
})();
var namedExports = {};
namedExports[emptyModulePath] = ['unstable_batchedUpdates'];
namedExports[reactDomModulePath] = ['unstable_batchedUpdates'];
var plugins = [
replace({
__TARGET__: JSON.stringify(target),
}),
// 编译es6
babel({
exclude: 'node_modules/**',
presets: ['es2015-rollup', 'react'],
plugins: ['transform-decorators-legacy', 'transform-class-properties'],
}),
alias({
'react-dom': reactDomModulePath,
}),
resolve({
module: true,
main: true,
}),
// 输出
commonjs({
exclude: [
'node_modules/react/**',
'node_modules/react-dom/**',
'node_modules/mobx/**',
],
namedExports: namedExports,
}),
];
if (minify) {
plugins.push(
uglify({
compress: {
warnings: false, //当删除没有用处的代码时,显示警告
drop_console: true, // 传true的话会干掉console.*函数。
dead_code: true,
drop_debugger:true,
if_return:true // 优化 if/return 和 if/continue
},
mangle: {
properties: true,
},
nameCache: null, // or specify a name cache object
ie8: true
})
);
} else {
plugins.push(
uglify({
output: {
beautify: true, // 格式化
preamble: "/* author:xizhi */", // 文件头
quote_style: 2
},
mangle: false, // 是否混淆
ie8: true//支持IE8
})
);
}
plugins.push(filesize());
var trueFn = function () {
return true;
};
var falseFn = function () {
return false;
};
return rollup({
input: 'src/index.js',
external: function (moduleId) {
return ({
'react': trueFn,
'react-dom': trueFn,
'mobx': trueFn,
}[moduleId] || falseFn)();
},
plugins: plugins,
})
.then(function (bundle) {
var options = {
file: path.resolve(__dirname, filename),
format: 'umd',
name: 'Moli',
exports: 'named',
globals: {
'react': 'React',
'react-dom': 'ReactDOM',
'mobx': 'mobx',
},
};
return bundle.write(options);
})
.catch(function (reason) {
console.error(reason);
process.exit(-1);
});
}
build('browser');
build('browser', true);