如何使Webpack不输出多个文件

时间:2017-02-21 09:46:52

标签: webpack

我使用webpack打包文件并发生有线事情:它总是输出多个文件。 但我只想要一个输出文件。 结果如下所示:

D:\Projects\cdn\branch\m2015\js>webpack --watch --config webpack.channel1.js
Hash: 2ecf74a2b5c317094da9
Version: webpack 1.13.1
Time: 3297ms
             Asset     Size  Chunks             Chunk Names
  channels.pack.js  12.8 kB       0  [emitted]  channels
1.channels.pack.js   131 kB    1, 3  [emitted]
2.channels.pack.js  56.1 kB    2, 3  [emitted]
3.channels.pack.js  31.8 kB       3  [emitted]

这是我的webpack配置代码:

var path=require("path");
var webpack=require("webpack");
var aliasConfig = require('../loader/alias-loader');
var channelsAliasConfig = require('../loader/channels-alias-loader');
var publicPath = '';

switch (process.env.NODE_ENV) {
    case 'dev':
        publicPath = 'http://localhost:8000';
        break;
    case 'branch':
        publicPath = 'https://img1-t.tuniucdn.com';
        break;
    case 'test':
        publicPath = 'https://mcdn.tuniu-sst.org';
        break;
    case 'mp':
        publicPath = 'https://img1-p.tuniucdn.com';
        break;
    case 'production':
        break;
    default:
        publicPath = 'https://img1.tuniucdn.com';
        break;
}

Object.assign(aliasConfig, channelsAliasConfig);

module.exports={
    entry:{
        "channels":[path.join(__dirname, "./modules/channels/index_new.js"), path.join(__dirname, "./components/layout/new_201604/header_simple.min.js"),path.join(__dirname, "./modules/footer/footer.js")]
    },
    output:{
        filename:"channels.pack.js",
        path:path.join(__dirname,"/modules/channels/dist"),
        publicPath: publicPath + "/site/m2015/js/modules/channels/dist/"
    },
    module:{
        loaders:[{
            test:/\.js$/,
            loader:path.join(__dirname,"./cmd.js")
        }]
    },
    plugins: [
        new webpack.optimize.LimitChunkCountPlugin({maxChunks: 1,chunkOverhead:200000}),
        new webpack.optimize.UglifyJsPlugin({
            compress:{
                warnings:false
            },
            mangle:{
                except:['require']
            }
        })
    ],
    resolve:{
        root:path.join(__dirname,"/"),
        alias: aliasConfig
    }
};

` 我试图删除这些插件,但它仍然发生。请给我一些提示,非常感谢你!

1 个答案:

答案 0 :(得分:0)

尝试更改您的条目配置:

entry:{
    "channels":[path.join(__dirname, "./modules/channels/index_new.js"), path.join(__dirname, "./components/layout/new_201604/header_simple.min.js"),path.join(__dirname, "./modules/footer/footer.js")]
},

类似于:

entry:{
        "channels": path.join(__dirname, "./modules/channels/index_new.js")
    },

要包含页眉和页脚,请在index_new中使用import或require:

var header = require("./components/layout/new_201604/header_simple.min.js");
var footer = require("./modules/footer/footer.js");

或者

import "./components/layout/new_201604/header_simple.min.js";
import "./modules/footer/footer.js";

希望有所帮助!