yoho-entry-loader.js 1.68 KB
const shelljs = require('shelljs');
const _ = require('lodash');
const path = require('path');

module.exports = function(source) {
    let transforSource = '';
    const options = eval(source); //eslint-disable-line

    transforSource += _.join(_.map(options.common, common => `import '${common}';`), '\r\n');

    //     transforSource += `
    // window.loadStatic = (name) => {
    //     switch (name) {
    //         case 'product.new-detail':
    //             return import(/* webpackChunkName: "page.product.new-detail" */ './js/product/new-detail.page.js')
    //     }
    // }`;

    transforSource += `
window.loadStatic = (name) => {
    switch (name) {${
    _.join(_.map(options.page.regx, regx => {
        return _.join(_.map(shelljs.ls(path.join(this.context, options.page.context, regx)), file => {
            const dir = _.slice(file.split('/'), -3);
            let moduleName, pageName;

            if (_.last(dir) === 'index.js') { // [modulename, page, index.js]
                moduleName = _.nth(dir, 0);
                pageName = _.nth(dir, 1);
            } else { // [xxx, modulename, xx.page.js]
                moduleName = _.nth(dir, 1);
                pageName = _.nth(dir, 2).match(/(.*).page.js/)[1];
            }
            return `
        case '${moduleName}.${pageName}':
            return  import(/* webpackChunkName: "page.${moduleName}.${pageName}" */ './${path.relative(this.context, file)}');`; //eslint-disable-line
        }), '');
    }), '')
}
${
    _.join(_.map(options.dll, (stat, dll) => {
        return `
        case '${dll}':
            return import(/* webpackChunkName: "${dll}" */ '${stat}');`;
    }))
}
    }
}`;
    return transforSource;
};