2021-06-24 00:29:53 +02:00
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
*/
|
|
|
|
|
2021-06-25 22:49:24 +02:00
|
|
|
import path from 'path';
|
|
|
|
import babel from '@babel/core';
|
2021-09-26 10:53:27 +02:00
|
|
|
import { readFileSync, readdirSync, statSync, unlinkSync } from "fs";
|
|
|
|
//import rimraf from 'rimraf';
|
2021-06-25 22:49:24 +02:00
|
|
|
import { minify } from 'terser';
|
2021-07-06 09:53:18 +02:00
|
|
|
import resolve from '@rollup/plugin-node-resolve';
|
2021-06-24 00:29:53 +02:00
|
|
|
|
2021-06-25 22:49:24 +02:00
|
|
|
// Best practice: use this
|
|
|
|
//rimraf.sync('./dist/');
|
2021-09-26 10:53:27 +02:00
|
|
|
//rimraf.sync('./chunks/');
|
|
|
|
|
|
|
|
// remove only chunks older then 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);
|
|
|
|
});
|
2021-06-25 22:49:24 +02:00
|
|
|
|
|
|
|
// Turn on minification
|
2021-08-16 13:46:42 +02:00
|
|
|
const do_minify = false;
|
2021-06-25 22:49:24 +02:00
|
|
|
|
2021-08-26 22:18:31 +02:00
|
|
|
function isBareSpecifier (id) {
|
|
|
|
if (id.startsWith("./") || id.startsWith("../") || id.startsWith("/"))
|
|
|
|
return false;
|
|
|
|
try {
|
|
|
|
new URL(id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 15:24:12 +02:00
|
|
|
const config = {
|
2021-06-25 22:49:24 +02:00
|
|
|
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",
|
2021-06-29 15:21:19 +02:00
|
|
|
"pixelegg/js/fw_mobile.min": "pixelegg/js/fw_mobile.js",
|
2021-06-25 22:49:24 +02:00
|
|
|
"api/js/jsapi/egw.min": "api/js/jsapi/egw_modules.js",
|
2021-07-13 20:54:08 +02:00
|
|
|
"api/js/etemplate/etemplate2": "api/js/etemplate/etemplate2.ts",
|
2021-06-25 22:49:24 +02:00
|
|
|
|
2021-07-02 15:24:12 +02:00
|
|
|
// app.ts/js are added automatic by addAppsConfig() below
|
2021-06-25 22:49:24 +02:00
|
|
|
},
|
|
|
|
external: function(id,parentId,isResolved) {
|
|
|
|
if(!isResolved)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(id.includes("/vendor/"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2021-06-24 00:29:53 +02:00
|
|
|
output: {
|
2021-06-25 22:49:24 +02:00
|
|
|
// 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) {
|
2021-08-26 22:18:31 +02:00
|
|
|
// Delegate bare specifiers to node_modules resolver
|
|
|
|
if (isBareSpecifier(id))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-07-09 21:07:23 +02:00
|
|
|
if (!parentId || parentId.indexOf(path.sep + 'node_modules' + path.sep) !== -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-08-26 22:18:31 +02:00
|
|
|
if (id.endsWith(".js"))
|
2021-06-25 22:49:24 +02:00
|
|
|
{
|
2021-08-26 22:18:31 +02:00
|
|
|
const tsPath = path.resolve(path.dirname(parentId), id.slice(0,-3) + '.ts');
|
2021-06-25 22:49:24 +02:00
|
|
|
try {
|
|
|
|
readFileSync(tsPath);
|
|
|
|
console.warn(id + " is a TS file loaded with wrong extension. Remove the extension on the import in " + parentId);
|
|
|
|
}
|
|
|
|
catch (e) {}
|
|
|
|
}
|
2021-07-09 21:07:23 +02:00
|
|
|
else if (!id.endsWith('.ts')) {
|
2021-06-25 22:49:24 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 09:53:18 +02:00
|
|
|
},
|
|
|
|
// resolve (external) node modules from node_modules directory
|
2021-08-26 22:21:18 +02:00
|
|
|
resolve({
|
|
|
|
browser: true
|
|
|
|
}),
|
2021-07-06 09:53:18 +02:00
|
|
|
{
|
2021-06-25 22:49:24 +02:00
|
|
|
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
|
|
|
|
},
|
2021-08-16 13:46:42 +02:00
|
|
|
presets: ['@babel/preset-typescript']
|
2021-06-25 22:49:24 +02:00
|
|
|
}, 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,
|
2021-07-05 16:24:22 +02:00
|
|
|
sourceMap: true,
|
2021-06-25 22:49:24 +02:00
|
|
|
output: {
|
|
|
|
preamble: `/*!
|
2021-07-02 15:24:12 +02:00
|
|
|
* EGroupware (https://www.egroupware.org/) minified Javascript
|
2021-06-25 22:49:24 +02:00
|
|
|
*
|
|
|
|
* full sources are available under https://github.com/EGroupware/egroupware/
|
|
|
|
*
|
|
|
|
* build ${Date.now()}
|
|
|
|
*/
|
|
|
|
`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
|
2021-07-02 15:24:12 +02:00
|
|
|
// Custom warning handler to give more information about circular dependencies
|
2021-06-25 22:49:24 +02:00
|
|
|
onwarn: function(warning,warn) {
|
2021-07-04 09:54:44 +02:00
|
|
|
console.warn(warning.toString());
|
2021-06-24 00:29:53 +02:00
|
|
|
}
|
2021-07-02 15:24:12 +02:00
|
|
|
};
|
2021-06-25 22:49:24 +02:00
|
|
|
|
2021-07-02 15:24:12 +02:00
|
|
|
/**
|
|
|
|
* Add existing app.ts/js endpoints to config.input and return it
|
|
|
|
*
|
|
|
|
* @return Promise<object>
|
|
|
|
*/
|
2021-07-13 08:30:51 +02:00
|
|
|
export default function addAppsConfig()
|
2021-07-02 15:24:12 +02:00
|
|
|
{
|
|
|
|
const conf = config;
|
2021-07-13 08:30:51 +02:00
|
|
|
const files = readdirSync('.', { withFileTypes: true});
|
2021-07-02 15:24:12 +02:00
|
|
|
for (const file of files)
|
|
|
|
{
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
try {
|
2021-07-13 08:30:51 +02:00
|
|
|
statSync(file.name + '/js/app.ts');
|
2021-07-19 08:31:06 +02:00
|
|
|
config.input[file.name + '/js/app.min'] = file.name + '/js/app.ts';
|
2021-07-02 15:24:12 +02:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
try {
|
2021-07-13 08:30:51 +02:00
|
|
|
statSync(file.name + '/js/app.js');
|
2021-07-02 15:24:12 +02:00
|
|
|
config.input[file.name + '/js/app.min'] = file.name + '/js/app.js';
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conf;
|
|
|
|
}
|