-
Notifications
You must be signed in to change notification settings - Fork 97
/
rollup.config.js
223 lines (212 loc) · 6.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* EGroupware - Rollup config file
*
* @link https://www.egroupware.org
* @copyright (c) 2021 by Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*
* @see http://rollupjs.org/guide/en
* @type {import('rollup').RollupOptions}
*/
import path from 'path';
import babel from '@babel/core';
import { readFileSync, readdirSync, statSync, unlinkSync } from "fs";
//import rimraf from 'rimraf';
import { minify } from 'terser';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
// Best practice: use this
//rimraf.sync('./dist/');
//rimraf.sync('./chunks/');
// remove only chunks older than 2 days, to allow UI to still load them and not require a reload / F5
const rm_older = Date.now() - 48*3600000;
readdirSync('./chunks').forEach(name => {
const stat = statSync('./chunks/'+name);
if (stat.atimeMs < rm_older) unlinkSync('./chunks/'+name);
});
// Turn on minification
const do_minify = false;
function isBareSpecifier (id) {
if (id.startsWith("./") || id.startsWith("../") || id.startsWith("/"))
return false;
try {
new URL(id);
return false;
}
catch {
return true;
}
}
const config = {
treeshake: false,
input: {
// Output : Input
// Note the .ts extension on the input - we build directly from the TypeScript when available
"pixelegg/js/fw_pixelegg.min": "pixelegg/js/fw_pixelegg.js",
"pixelegg/js/fw_mobile.min": "pixelegg/js/fw_mobile.js",
"api/js/jsapi/egw.min": "api/js/jsapi/egw_modules.js",
"api/js/etemplate/etemplate2": "api/js/etemplate/etemplate2.ts",
// app.ts/js are added automatic by addAppsConfig() below
},
external: function(id,parentId,isResolved) {
// core-js used require and needs to be run through RollupJS and NOT treated as external
if (id.includes("/node_modules/core-js/"))
{
return false;
}
if(!isResolved)
{
return;
}
if(id.includes("/vendor/"))
{
return true;
}
},
output: {
// TODO: Hashed entries, when server supports
//entryFileNames: '[name]-[hash].js',
entryFileNames: '[name].js',
chunkFileNames: 'chunks/[name]-[hash].js',
// Best practice: use this:
//dir: './dist',
dir: '.',
sourcemap: true
},
plugins: [{
resolveId (id, parentId) {
// Delegate bare specifiers to node_modules resolver
if (isBareSpecifier(id))
{
return;
}
if (!parentId || parentId.indexOf(path.sep + 'node_modules' + path.sep) !== -1)
{
return;
}
if (id.endsWith(".js"))
{
const tsPath = path.resolve(path.dirname(parentId), id.slice(0,-3) + '.ts');
try {
readFileSync(tsPath);
console.warn(id + " is a TS file loaded with wrong extension. Remove the extension on the import in " + parentId);
}
catch (e) {}
}
else if (!id.endsWith('.ts')) {
const tsPath =path.resolve(path.dirname(parentId), id + '.ts');
const jsPath =path.resolve(path.dirname(parentId), id + '.js');
try {
readFileSync(tsPath);
}
catch (e) {
return jsPath;
}
return tsPath;
}
}
},
// resolve (external) node modules from node_modules directory
resolve({
browser: true
}),
// core-js uses require, which needs to be transformed to es-modules
commonjs(),
{
transform (code, id) {
if (id.endsWith('.ts'))
return new Promise((resolve, reject) => {
return babel.transform(code, {
filename: id,
sourceMaps: true,
ast: false,
compact: false,
sourceType: 'module',
parserOpts: {
// plugins: stage3Syntax,
errorRecovery: true
},
plugins: [
['@babel/plugin-proposal-decorators', {legacy: true}],
['@babel/plugin-transform-class-properties', {loose: false}]
],
presets: [
['@babel/preset-typescript', {
//onlyRemoveTypeImports: true // seems not necessary and generates a lot of warnings about not exported symbols
}],
['@babel/preset-env', {
corejs: {
version: "3"
},
useBuiltIns: "usage",
modules: false,
targets : {
esmodules: true,
safari: "14"
}
}],
]
}, function (err, result) {
if (err)
return reject(err);
resolve(result);
});
});
}
},
{
transform (code,id) {
if(!do_minify || id.includes(".min"))
{
return;
}
return minify(code, {
mangle: false,
sourceMap: true,
output: {
preamble: `/*!
* EGroupware (https://www.egroupware.org/) minified Javascript
*
* full sources are available under https://github.com/EGroupware/egroupware/
*
* build ${Date.now()}
*/
`
}
});
}
}],
// Custom warning handler to give more information about circular dependencies
onwarn: function(warning,warn) {
console.warn(warning.toString());
}
};
/**
* Add existing app.ts/js endpoints to config.input and return it
*
* @return Promise<object>
*/
export default function addAppsConfig()
{
const conf = config;
const files = readdirSync('.', { withFileTypes: true});
for (const file of files)
{
if (file.isDirectory())
{
try {
statSync(file.name + '/js/app.ts');
config.input[file.name + '/js/app.min'] = file.name + '/js/app.ts';
}
catch (e) {
try {
statSync(file.name + '/js/app.js');
config.input[file.name + '/js/app.min'] = file.name + '/js/app.js';
}
catch (e) {
}
}
}
}
return conf;
}