webpack.extra.common.js 2.8 KB
Newer Older
prova's avatar
prova committed
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
const lodash = require('lodash');

const externals = [
    libExternalFactory(),
    ngExternalsFactory(),
    rxjsExternalsFactory(),
    mggExternalsFactory(),
    // kendoExternalsFactory(),
]

function libExternalFactory() {
    return function (_context, request, callback) {

        const value = lodash.toString(request)

        if ((/(zone|jquery|lodash|moment)/i).test(value)) {
            return callback(null, externalsObject(value))
        }

        return callback()
    }
}

function ngExternalsFactory() {
    return function (_context, request, callback) {
        if (lodash.startsWith(request, '@angular/')) {

            const temp = lodash.map(lodash.split(request, '/'), camelCase)
            const value = lodash.join(['ng', ...temp.slice(1)], '.')

            if (value === 'ng.common.locales.it') { return callback() }
            if (value === 'ng.common.locales.extra.it') { return callback() }

            return callback(null, externalsObject(value))
        }

        return callback()
    }
}

function rxjsExternalsFactory() {
    return function rxjsExternals(_context, request, callback) {
        if (request.match(/^rxjs(\/|$)/)) {

            const parts = lodash.split(request, '/')
            const value = lodash.join(parts, '.')

            if (lodash.size(parts) <= 2) {
                return callback(null, externalsObject(value))
            }
        }

        return callback();
    };
}

function mggExternalsFactory() {
    return function mggExternals(_context, request, callback) {
        if (request.startsWith('@maggioli/')) {

            const value = lodash.join(lodash.map(lodash.split(request, '/'), camelCase), '.')

            if (value === 'maggioli.ctlNg') { return callback() }
            if (value === 'maggioli.grdNg') { return callback() }

            return callback(null, externalsObject(value))
        }

        return callback();
    };
}

function kendoExternalsFactory() {
    return function (_context, request, callback) {
        if (request.startsWith('@progress/')) {

            const value = lodash.join(lodash.tail(lodash.split(request, /\/|\@|-/)), '.')

            return callback(null, externalsObject(value))
        }

        return callback()
    }
}

function camelCase(val) { return (val === 'a11y') ? val : lodash.camelCase(val) }

function externalsObject(value) { return { var: value } }

function noParseExternals(content) {

    if (/(zone|jquery|lodash|moment)/i.test(content)) { return true }

    if (lodash.startsWith(content, '@angular')) { return true }

    if (lodash.startsWith(content, '@maggioli')) { return true }

    return false
}

module.exports = {
    mode: 'production', externals, optimization: { minimize: false },
    output: { libraryTarget: 'var', library: 'it_maggioli_suite_spawc' },
    module: { noParse: noParseExternals },
}