-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
167 lines (157 loc) · 4.55 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from "rollup-plugin-terser";
import copy from 'rollup-plugin-copy';
import { promisify } from "util";
import { exec } from "child_process";
const execAsync = promisify(exec);
/** @type {() => import('rollup').RollupOptions[]} */
const rollupConfig = () => ([
{
input: './src/main.ts',
output: {
file: './dist/bundle.js',
format: 'umd',
name: 'mcloud',
plugins: [
terser({
keep_classnames: true,
keep_fnames: true,
}),
],
sourcemap: true,
// sourcemapExcludeSources: true,
// sourcemapPathTransform: transformSourcemapPath(),
},
plugins: [
buildInfo(),
resolve(),
typescript(),
myText(),
jsonLoader(),
copy({
targets: [
{
src: [
'index.html',
],
dest: './dist/',
},
{
src: [
'resources/app_icon.svg',
'resources/manifest.json',
],
dest: './dist/resources/',
}
]
})
],
context: 'window'
},
{
input: './src/ServiceWorker/sw.ts',
output: {
file: './dist/sw.bundle.js',
format: 'iife',
plugins: [
// terser({
// keep_classnames: true,
// keep_fnames: true,
// }),
],
sourcemap: true,
// sourcemapExcludeSources: true,
sourcemapPathTransform: transformSourcemapPath(),
},
plugins: [
buildInfo(),
resolve(),
typescript(),
],
},
]);
let _buildInfo = null;
async function getBuildInfo() {
return _buildInfo || (_buildInfo = JSON.stringify({
version: require('./package.json').version,
buildDate: new Date().toISOString(),
commits: await getCommits(),
}));
}
async function getCommits() {
var execResult = await execAsync('git log --pretty=format:"%h %cI %s" -n 20');
return execResult.stdout
.split('\n')
.map(x => {
var s = x.split(' ', 2);
return {
id: s[0],
date: s[1],
message: x.substr(s[0].length + 1 + s[1].length + 1)
};
});
}
function buildInfo() {
return {
name: 'gen-build-info',
resolveId(source, importer) {
if (source === './buildInfo') {
return source;
}
return null;
},
async load(id) {
if (id === './buildInfo') {
const info = await getBuildInfo();
console.info('buildInfo: ' + info);
return 'export default ' + info + ';';
}
return null;
}
};
}
function transformSourcemapPath() {
return (rel, path) => {
rel = rel.replace(/\\/g, '/');
if (rel.startsWith('node_modules')) {
var mat = rel.match(/node_modules\/((?:@[\w\-_]+\/)?[\w\-_]+)\/(.*)$/);
if (!mat) {
console.warn(['sourcemapPathTransform', rel]);
}
var version = require(mat[1] + '/package.json').version;
return `https://cdn.jsdelivr.net/npm/${mat[1]}@${version}/${mat[2]}`;
}
return `https://github.com/lideming/MusicCloud/raw/dev/${rel}`;
};
}
function myText() {
function match(id) {
return /\.(css|svg)$/.test(id);
}
return {
name: 'my-text-loader',
transform(code, id) {
if (match(id)) {
return {
code: 'export default ' + JSON.stringify(code),
map: { mappings: '' }
};
}
}
};
}
function jsonLoader() {
return {
name: 'json-loader',
transform(code, id) {
if (id.endsWith('.json')) {
return {
code: 'export default JSON.parse(' + JSON.stringify(JSON.stringify(JSON.parse(code))) + ');',
map: { mappings: '' }
};
}
}
};
}
export default rollupConfig();