entry.js 1.6 KB
Newer Older
B
BingBlog 已提交
1
'use strict';
B
BingBlog 已提交
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
const path = require('path');
const projectPath = path.resolve(__dirname, '..');
const HtmlWebpackPlugin = require('html-webpack-plugin');

/**
 * default apps
 *
 * @type {Array}
 */
let defaultApps = [
    {
        name: 'index',
        feRoot: '/dist'
    }
];

/**
 * get entry js file
 *
 * @param  {Array} apps  appname
 * @return {string}      file path
 */
function getModules(apps) {

    let modules = {};
    apps.forEach(function (item) {
        let app = item.name;
        modules[app] = path.join(projectPath, 'src/' + app + '.js');
    });

    return modules;
}

/**
 * get HtmlWebpackPlugin
 *
 * @param  {string} app appname
 * @param  {boolan} template use template
 * @return {HtmlWebpackPlugin}     HtmlWebpackPlugin
 */
function getTemplate(app, template) {
    let templateUrl = 'template/index.html';
    if (template) {
        templateUrl = `ejs-render-loader!template/${template}.ejs`;
    }
    return new HtmlWebpackPlugin({
        filename: app + '.html',
        template: templateUrl
    });
}

/**
 * get entry config
 *
 * @param  {string} app appname
 * @param  {boolan} template use template
 * @return {Object}     config
 */
function getEntry(app, template) {

    let buildApps = defaultApps.filter(function (item) {
        let name = item.name;
        return name === app;
    });

    buildApps = buildApps.length > 0 ? buildApps : defaultApps;

    return {
        module: getModules(buildApps),
        template: buildApps.map(item => getTemplate(item.name, template))
    };
}


module.exports.get = getEntry;
module.exports.entry = defaultApps;