webpack.config.js 1.57 KB
var path = require('path');
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var fs=require('fs'),
path=require('path');

var commons=["./js/common/common.js","jquery"];

var config = JSON.parse(fs.readFileSync('./package.json').toString());
var alias={
  jquery:path.join(__dirname, './js/util/jquery.min.js')
};

var entry={index:[],libs:[]};

entry.libs=commons;


var readfile=function(dir,callback){
  var files = fs.readdirSync(dir);
  files.forEach(function (file) {
      var filePath = path.join(dir, file);
      if (fs.statSync(filePath).isFile() && /.*\.js$/i.test(file)) {
          callback && callback(filePath);
      }else{

        if(file!="common"&&file!="util"&&file!="partials" && file!='.DS_Store'){
          readfile(filePath,callback);
        }
      }
  });
}

var index=[];
readfile('./js',function(src){
  index.push(src);
});

//区分不同系统环境
var __reg=/(js\\)|\\|(\.js)/g;
if(path.sep=="/"){
    __reg=/(js\/)|\/|(\.js)/g;
}

for(var i in index){
  var __name=index[i].replace(__reg,'');
  entry[__name]='.'+path.sep+index[i];
}

var plugins=[
    //获取公共js
    new webpack.optimize.CommonsChunkPlugin('libs','libs.js')
    //全局引入jquery
    // new webpack.ProvidePlugin({
    //   $: "jquery",
    //   jQuery: "jquery",
    //   "window.jQuery": "jquery"
    // })
];

module.exports = {
    entry: entry,
    output: { 
        path:path.join("../public","dist"),
        filename:'[name].js'
    },
     resolve:{
        extension:['','.js'],
        alias:alias
    },
    plugins:plugins
};