-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.js
146 lines (125 loc) · 3.21 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
134
135
136
137
138
139
140
141
142
143
144
145
146
const rollup = require('rollup');
const { eslint } = require('rollup-plugin-eslint');
const { uglify } = require('rollup-plugin-uglify');
const buble = require('@rollup/plugin-buble');
const typescript = require('@rollup/plugin-typescript');
const polyfill = require('rollup-plugin-polyfill');
const resolve = require('@rollup/plugin-node-resolve').default;
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const tsconfig = require('./src/tsconfig.json');
const pkg = require('./package.json');
const serverFactory = require('spa-server');
const args = process.argv.splice(2);
delete tsconfig.compilerOptions.declaration;
delete tsconfig.compilerOptions.declarationDir;
delete tsconfig.compilerOptions.outDir;
const banner = `
/*!
* mdclub-sdk ${pkg.version} (${pkg.homepage})
* Copyright 2018-${new Date().getFullYear()} ${pkg.author}
* Licensed under ${pkg.license}
*/
`.trim();
const input = './src/index.ts';
const plugins = [
resolve(),
commonjs(),
typescript(Object.assign(
tsconfig.compilerOptions,
{ include: './src/**/*' }
)),
];
const outputOptions = {
strict: true,
name: 'mdclubSDK',
banner,
};
// 编译成 ES6 模块化文件
async function buildEsm() {
const bundle = await rollup.rollup({ input, plugins });
await bundle.write(Object.assign({}, outputOptions, {
sourcemap: true,
format: 'es',
file: './dist/mdclub-sdk.esm.js',
}));
}
// 编译成 umd 文件
async function buildUmd() {
plugins.push(
buble(),
polyfill([
'mdn-polyfills/MouseEvent',
'mdn-polyfills/CustomEvent',
'promise-polyfill/src/polyfill',
]),
);
const bundle = await rollup.rollup({ input, plugins });
await bundle.write(Object.assign({}, outputOptions, {
sourcemap: true,
format: 'umd',
file: './dist/mdclub-sdk.js',
}));
}
// 编译成 umd 文件,并压缩
async function buildUmdUglify() {
plugins.push(
uglify({
output: {
preamble: banner,
}
})
);
const bundle = await rollup.rollup({ input, plugins });
await bundle.write(Object.assign({}, outputOptions, {
sourcemap: true,
format: 'umd',
file: './dist/mdclub-sdk.min.js',
}));
}
async function build() {
await buildEsm();
await buildUmd();
await buildUmdUglify();
}
async function test() {
const bundle = await rollup.rollup({
input: './test/index.ts',
plugins: [
resolve({ mainFields: ["jsnext", "preferBuiltins", "browser"] }),
commonjs(),
json(),
eslint({
fix: true,
}),
typescript({
include: './test/**/*',
module: "ES6",
target: "ES6"
}),
buble(),
polyfill([
'mdn-polyfills/MouseEvent',
'mdn-polyfills/CustomEvent',
'promise-polyfill/src/polyfill',
]),
],
});
await bundle.write({
strict: true,
name: 'MDClubSDKTest',
format: 'umd',
file: './test/dist.js',
});
const server = serverFactory.create({
path: './',
port: 8889
});
server.start();
console.log('打开 http://127.0.0.1:8889/test/index.html 开始测试');
}
if (args.indexOf('--build') > -1) {
build().catch(e => console.log(e));
} else if (args.indexOf('--test') > -1) {
test();
}