var webpack = require('webpack');
var path = require('path');
var viewDir = path.join(__dirname, "..", "web", "view");
var fs = require('fs');
const i18nPlguin = require('./core/I18NPackage')
    //获取src目录下面的所有.js文件
let devViewDir = __dirname + "/src/view";
let files = fs.readdirSync(devViewDir);
let entry = {};
let regexp = new RegExp("v_\\w+[.]js", "g");

getAllViewJS(devViewDir);

function getAllViewJS(dir) {
    let files = fs.readdirSync(dir);
    for (let file of files) {
        let filePath = dir + "/" + file;
        let stat = fs.lstatSync(filePath);
        if (stat.isDirectory()) {
            getAllViewJS(filePath);
        } else if (stat.isFile()) {
            let fileName = path.basename(filePath);
            if (fileName.match(regexp)) {
                let fPath = filePath.replace(__dirname, "");
                let name = filePath.replace(devViewDir + "/", "").replace('v_', '').replace('.js', '');
                entry[name] = ["." + fPath];
            }
        }
    }
}

//entry['vendor'] = ['vue','vue-router','vue-i18n','vue-touch','vuex','underscore','es6-promise','es6-symbol'];
module.exports = {
    entry: entry,
    output: {
        path: viewDir,
        filename: '[name].js',
        chunkFilename: "[name].js"
    },
    module: {
        loaders: [{
            test: /\.vue$/,
            loader: 'vue-loader'
        }, {
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel-loader'
        }, {
            test: /\.css$/,
            loader: 'style!css'
        }]
    },
    node: {
        Buffer: false
    },
    resolve: {
        alias: {
            'vue-i18n$': 'vue-i18n/dist/vue-i18n.common.js'
        }
    },
    plugins: [
        new webpack.ProvidePlugin({
            'Promise': 'es6-promise',
            Vue: ['vue/dist/vue.esm.js', 'default'],
        }),
        new webpack.optimize.CommonsChunkPlugin({name: "../lib/js/p"}),
        new i18nPlguin()
    ],
    devtool: 'source-map'
};
