webpack.config.js 1.95 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"];

var config = JSON.parse(fs.readFileSync('./package.json').toString());
var spmroot = path.join(__dirname, 'spm_modules');
var alias = {};
for (key in config.spm.dependencies) {

  var edition = path.join(key, config.spm.dependencies[key]);

  var json = path.join(spmroot, path.join(edition, 'package.json'));

  var mainjs = JSON.parse(fs.readFileSync(json).toString()).spm.main;
  if (!alias[key]) {
    alias[key] = path.join(edition, mainjs).replace('/\\/ig', '/');
  }

}
for (key in config.spm.dependencies) {
  commons.push(key);
}
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") {
        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: {
    root: [spmroot],
    extension: ['', '.js'],
    alias: alias
  },
  plugins: plugins
};