yo.prod.conf.js 2.82 KB
const _ = require('lodash');
const ManifestPlugin = require('webpack-manifest-plugin');
const yoBase = require('./yo.base.conf.js');
const yo = require('yo-cli');
const pkg = require('../package.json');

module.exports = yo(yoBase)
    .style({
        extract: true
    })
    .sourceMap()
    .merge({
        plugins: [
            new ManifestPlugin({
                fileName: `${pkg.version}/manifest.json`,
                generate(seed, files) {
                    const result = {
                        common: {},
                        pages: {},
                    };

                    _.each(files, (file => {
                        const {name, path: filePath} = file;
                        const match = /page\.([^\.]+)\.([^\.]+)\.js$/.exec(name);

                        if (match) {
                            const [, moduleName, pageName] = match;

                            if (!result.pages[moduleName]) {
                                result.pages[moduleName] = {};
                            }
                            if (result.pages[moduleName][pageName]) {
                                throw Error(`页面文件名称冲突:${moduleName}.${pageName}`);
                            }
                            result.pages[moduleName][pageName] = {
                                js: filePath
                            };
                            const cssFile = _.find(files,
                                ({name: fname}) => fname === `page.${moduleName}.${pageName}.css`);

                            if (cssFile) {
                                result.pages[moduleName][pageName].css = cssFile.path;
                            }
                        } else if (name === 'main.js') {
                            result.common.main = {
                                js: filePath,
                                css: _.get(_.find(files, ({name: fname}) => fname === 'main.css'), 'path')
                            };
                        } else if (name === 'runtime-main.js') {
                            result.common.runtime = {
                                js: filePath
                            };
                        } else if (name === 'vendors.js') {
                            result.common.vendors = {
                                js: filePath,
                                css: _.get(_.find(files, ({name: fname}) => fname === 'vendors.css'), 'path')
                            };
                        } else if (name === 'index.css') {
                            result.common.index = {
                                css: _.get(_.find(files, ({name: fname}) => fname === 'index.css'), 'path')
                            };
                        }
                    }));
                    return result;
                }
            }),
        ]
    })
    .run();