Skip to content
Snippets Groups Projects
rollup.config.js 3.63 KiB
Newer Older
import glob from 'glob';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import copy from 'rollup-plugin-copy';
import terser from '@rollup/plugin-terser';
import json from '@rollup/plugin-json';
import serve from 'rollup-plugin-serve';
Reiter, Christoph's avatar
Reiter, Christoph committed
import url from '@rollup/plugin-url';
import del from 'rollup-plugin-delete';
Reiter, Christoph's avatar
Reiter, Christoph committed
import emitEJS from 'rollup-plugin-emit-ejs';
import {getPackagePath, getDistPath} from '../../rollup.utils.js';

const pkg = require('./package.json');
Reiter, Christoph's avatar
Reiter, Christoph committed
const build = typeof process.env.BUILD !== 'undefined' ? process.env.BUILD : 'local';
console.log('build: ' + build);

function getBuildInfo() {
    const child_process = require('child_process');
    const url = require('url');

    let remote = child_process.execSync('git config --get remote.origin.url').toString().trim();
    let commit = child_process.execSync('git rev-parse --short HEAD').toString().trim();

    let parsed = url.parse(remote);
    // convert git urls
    if (parsed.protocol === null) {
Reiter, Christoph's avatar
Reiter, Christoph committed
        parsed = url.parse('git://' + remote.replace(':', '/'));
        parsed.protocol = 'https:';
    }
    let newPath = parsed.path.slice(0, parsed.path.lastIndexOf('.'));
    let newUrl = parsed.protocol + '//' + parsed.host + newPath + '/commit/' + commit;

    return {
        info: commit,
        url: newUrl,
        time: new Date().toISOString(),
Reiter, Christoph's avatar
Reiter, Christoph committed
        env: build,
    };
}

export default (async () => {
    return {
Reiter, Christoph's avatar
Reiter, Christoph committed
        input:
            build != 'test'
                ? ['src/dbp-provider.js', 'src/dbp-adapter.js', 'src/dbp-provider-demo.js']
                : glob.sync('test/**/*.js'),
        output: {
            dir: 'dist',
            entryFileNames: '[name].js',
            chunkFileNames: 'shared/[name].[hash].[format].js',
            format: 'esm',
Reiter, Christoph's avatar
Reiter, Christoph committed
            sourcemap: true,
        onwarn: function (warning, warn) {
            // keycloak bundled code uses eval
            if (warning.code === 'EVAL' && warning.id.includes('sha256.js')) {
                return;
            }
            warn(warning);
        },
Reiter, Christoph's avatar
Reiter, Christoph committed
                targets: 'dist/*',
            }),
            emitEJS({
                src: 'assets',
                include: ['**/*.ejs', '**/.*.ejs'],
                data: {
                    getUrl: (p) => {
                        return url.resolve(basePath, p);
                    },
                    getPrivateUrl: (p) => {
                        return url.resolve(`${basePath}local/${pkg.name}/`, p);
                    },
                    name: pkg.name,
                    environment: build,
Reiter, Christoph's avatar
Reiter, Christoph committed
                    buildInfo: getBuildInfo(),
                },
            }),
            resolve(),
            commonjs(),
            url({
                limit: 0,
Reiter, Christoph's avatar
Reiter, Christoph committed
                include: [await getPackagePath('select2', '**/*.css')],
                emitFiles: true,
Reiter, Christoph's avatar
Reiter, Christoph committed
                fileName: 'shared/[name].[hash][extname]',
Reiter, Christoph's avatar
Reiter, Christoph committed
            build !== 'local' && build !== 'test' ? terser() : false,
            copy({
                targets: [
                    {src: 'assets/index.html', dest: 'dist'},
Reiter, Christoph's avatar
Reiter, Christoph committed
                    {
                        src: await getPackagePath('@dbp-toolkit/common', 'assets/icons/*.svg'),
                        dest: 'dist/' + (await getDistPath('@dbp-toolkit/common', 'icons')),
                    },
                    {src: 'assets/favicon.ico', dest: 'dist'},
Reiter, Christoph's avatar
Reiter, Christoph committed
            process.env.ROLLUP_WATCH === 'true'
                ? serve({contentBase: 'dist', host: '127.0.0.1', port: 8002})
                : false,
        ],
Reiter, Christoph's avatar
Reiter, Christoph committed
})();