webpack.config.js
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
};