const webpack = require('webpack') const path = require('path') const ExtractTextPlugin = require('extract-text-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') const CopyWebpackPlugin = require('copy-webpack-plugin') const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin') const WebpackDevServer = require('webpack-dev-server') const entries = require('./webpack/entry.config.js') const version = require('./package.json').version const argv = require('yargs').argv const dirVars = require('./webpack/dir.config.js') let theme = { '@primary-color': '#58a3ff', '@link-color': '#58a3ff', } let isWatch = argv.env.watch == 'watch' let isDev = isWatch || argv.env.deploy == 'dev' process.traceDeprecation = true process.noDeprecation = true module.exports = (function () { let jumlApi = '' switch (argv.env.deploy) { case 'dev': jumlApi = 'http://sb.jishutao.com' break case 'test': jumlApi = 'http://uat.jishutao.com' break case 'prod': jumlApi = 'http://bm.jishutao.com' break default: break } let plugins = [ //把入口文件里面的数组打包成verdors.js new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', }), /* 配置好Dll */ new webpack.DllReferencePlugin({ context: dirVars.staticRootDir, // 指定一个路径作为上下文环境,需要与DllPlugin的context参数保持一致,建议统一设置为项目根目录 manifest: require('./dll/' + argv.env.deploy + '/manifest.json'), // 指定manifest.json }), // 把dll文件复制到打包后的文件中 new CopyWebpackPlugin([ { from: path.resolve('./dll/' + argv.env.deploy), to: (__dirname, './dll/'), ignore: ['.*'], }, ]), // 将 dll.js 插入HTML里 new HtmlWebpackIncludeAssetsPlugin({ assets: [(__dirname, 'dll/dll.js'), (__dirname, 'dll/dll.css')], append: false, }), new ExtractTextPlugin({ filename: '[name].css', disable: false, allChunks: true, }), //定义全局变量区分本地测试生产做跳转 new webpack.DefinePlugin({ jumpUrl: `'${jumlApi}'`, }), //admin new HtmlWebpackPlugin({ title: '管理员-首页', filename: 'admin/index.html', template: './template/template.html', chunks: ['admin/index', 'vendors'], }), new HtmlWebpackPlugin({ title: '管理员-登录', filename: 'admin/login.html', template: './template/template.html', chunks: ['admin/login', 'vendors'], }), ] if (!isDev) { //这个使用uglifyJs压缩你的js代码 plugins.unshift( new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production'), }, }) ) plugins.unshift( new webpack.optimize.UglifyJsPlugin({ minimize: true, compress: { warnings: true, }, }) ) } let staticHost = 'http://172.16.1.187:80' switch (argv.env.deploy) { case 'test': staticHost = '//static.jishutao.com' break case 'prod': staticHost = '//s.jishutao.com' break default: break } staticHost = staticHost + '/RD/' + version + '/' theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"' return { //devtool:"source-map", entry: isWatch ? entries.watch : entries.prod, output: { path: path.resolve( __dirname, './build/' + argv.env.deploy + '/' + version ), filename: '[name].js', publicPath: staticHost, chunkFilename: 'chunks/[name].[hash:8].js', }, module: require('./webpack/module.config.js')(theme), resolve: { alias: { '@': __dirname + '/js/component', }, extensions: ['.js', '.jsx'], }, plugins: plugins, // devServer: { // disableHostCheck: true, // allowedHosts: ['bm.kedexinxi.com'], // headers: { // "Access-Control-Allow-Origin": "*" // } // } devServer: { disableHostCheck: true, host: '172.16.1.187', port: 80, allowedHosts: ['127.0.0.1', '192.168.0.20', '192.168.0.99'], headers: { 'Access-Control-Allow-Origin': '*', }, }, } })()