const config = require("./webpack.config");
const merge = require("webpack-merge");
const webpack = require('webpack');
//105231 app.js->index.js
var pages = require('./plugins/entryFile').getEntryFile().pages;

let webpackConfig = merge(config, {
    module: {
        rules: [{
            test: /\.vue(\?[^?]+)?$/,
            loader: 'weex-loader',
            options: {
            }
        }],
    },
    output: {
        path: light.config.dist,
        filename: '[name].native.js',

        //87143 lighting框架支持jsn视图的异步视图和单独处理
        chunkFilename: 'lightdist/[name].native.js?[chunkhash]',
    },
    plugins: [
        //144595 type-vue异步视图移除对global的依赖,global->lightGlobal
        new webpack.BannerPlugin({
            banner: '// { "framework": "Vue" }\n var lightGlobal = {};',
            raw: true
        }),
        new webpack.DefinePlugin({
            'process.env.RUNTIME': JSON.stringify("native"),
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
            '__ENV__': require("./env")(light.options.env || 'default')
        }),
    ],
    externals: {
        vue: 'Vue'
    },
});

if (light.options.watch) {
    webpackConfig = merge({
        watch: true,
        watchOptions: {
            ////105231 多页工程的每个.native.js文件都不监听变化
            ignored: (function () {
                pages.forEach(function (page) {
                    page = pages.length == 1 && pages[0] == 'index' && light.options.page != 'index' ? 'app' : 'index';
                    page += '.native.js';
                })
                return pages;
            })()
        }
    }, webpackConfig)
}

module.exports = {
    build() {
        light.util.log("info", "准备编译weex环境下的jsnative文件...");


        let distConfig = webpackConfig;
        // 59972 【lighting】可以考虑在现有约定的基础上开放自定义配置接口（Webpack和Babel）

        let buildJS = require("path").join(light.config.src, "build.js");
        if (require("fs").existsSync(buildJS)) {
            distConfig = require(buildJS).build(distConfig, merge, webpack)
        }

        return distConfig;
    }
};