UNPKG

8.38 kBJavaScriptView Raw
1var ExtractTextPlugin = require("extract-text-webpack-plugin");
2var HtmlWebpackPlugin = require("html-webpack-plugin");
3var fs=require("fs");
4var path=require("path");
5var webpack=require("webpack");
6var _=require("underscore");
7// 打包路径设置
8var packUrl="";
9
10
11function SetOptions(options){
12 this.options=options;
13 return this.init(options);
14};
15_.extend(SetOptions.prototype,{
16 init:function(options){
17 this.config={
18 entry:this.genEntries(),
19 module:{
20 //加载loader处理文件
21 rules:[
22 {
23 test:/\.css$/,
24 use: ExtractTextPlugin.extract({
25 fallback: "style-loader",
26 use: "css-loader"
27 })
28 },
29 {
30 test:/\.less$/,
31 use: ExtractTextPlugin.extract({
32 fallback: "style-loader",
33 use: ['css-loader', 'less-loader']
34 })
35 },
36 {
37 test:/\.scss$/,
38 use: ExtractTextPlugin.extract({
39 fallback: "style-loader",
40 use: ['css-loader','sass-loader']
41 })
42 },
43 {
44 test: /\.md$/,
45 use: [
46 {
47 loader: "html-loader"
48 },
49 {
50 loader:path.resolve(__dirname,"tool/markdown.js")
51 }
52 ]
53 },
54 {
55 test:/\.vue$/,
56 use:['vue-loader']
57 },
58 {
59 test:/\.hbs$/,
60 use:['handlebars-loader']
61 },
62 {
63 test:/\.(js|jsx)$/,
64 exclude: /(node_modules|bower_components)/,
65 use:[
66 {
67 loader: 'babel-loader',
68 options: {
69 presets:["react","env"],
70 plugins:[]
71 }
72 }
73 ]
74 },
75 {
76 test:/\.(jpg|png|gif)$/,
77 use:{
78 loader:"file-loader",
79 options:{
80 limit:8129,
81 name:"images/[name]-[hash:8].[ext]"
82 }
83 }
84 }
85 ]
86 },
87 //开发工具--追踪错误源位置
88 devtool:"eval",
89 resolve: {
90 //查找模块的路径
91 modules: [path.resolve(process.cwd(),"node_modules")],
92 //模块别名列表
93 alias: {
94 'vue': 'vue/dist/vue.js',
95 "tool":path.resolve(__dirname,"tool/"),
96 "images":path.resolve(__dirname,"images/")
97 },
98 //自行补全路径中文件的后缀, 第一个是空字符串,对应不需要后缀的情况
99 extensions: ['.vue','.js', '.html', '.css', '.jsx','.swig','.less','.sass','.scss','.png','.gif','.jpg']
100 },
101 mode:"development",
102 // 处理loader
103 resolveLoader:{
104 modules: [path.resolve(process.cwd(),"node_modules")],
105 // 自动补全loader后缀
106 moduleExtensions: ['-loader']
107 },
108 externals: {
109 'react': 'React',
110 'react-dom': 'ReactDOM',
111 "vue":"Vue",
112 "vueRouter":"vueRouter",
113 'zepto': 'Zepto',
114 "echarts":"echarts",
115 'jQuery':'window.jQuery',
116 '$':'window.jQuery',
117 'jquery': 'window.jQuery'
118 },
119 plugins:[
120 // 输出独立的css文件,如果有多个入口JS共用的CSS,会生成commons.css
121 new ExtractTextPlugin({filename:"css/[name].css",allChunks:true})
122 ],
123 }
124
125 this.htmlReg=/(.+)\.(html|swig)$/;
126 this.htmlFiles=fs.readdirSync("./html");
127
128 if(options.isServer){
129 this.server();
130 }else{
131 this.pack();
132 }
133
134 return this.config;
135 },
136 // 打包项
137 pack:function(){
138 var self=this;
139 self.config.output={
140 path:__dirname+"/build",
141 publicPath:packUrl,
142 filename:"js/[name].min.js"
143 }
144
145 self.htmlFiles.forEach(function(item){
146 var m=item.match(self.htmlReg);
147 if(self.htmlReg.test(item)){
148 self.config.plugins.push(
149 new HtmlWebpackPlugin({
150 template: 'html-withimg-loader!./html/'+item,
151 inject: 'body',
152 chunks:["common",m[1]],
153 filename:""+item,
154 title: '最美奥运脸'
155 })
156 )
157 }
158 })
159 },
160 // 开启服务项
161 server:function(){
162 var self=this;
163 self.config.output={
164 // path:__dirname+"/public",
165 publicPath:"/public/",
166 filename:"js/[name].js"
167 }
168
169 var links=[];
170 self.htmlFiles.forEach(function(item){
171 var m=item.match(self.htmlReg);
172 if(m&&m[0]){
173 var fileContent = fs.readFileSync(
174 path.resolve("./html", item), "utf-8");
175 var title = fileContent?(/<title>[^<>]*<\/title>/i).exec(fileContent)[0]:"";
176 title = title.replace(/<\/?title>/g, '');
177 links.push('<li><a href="'+self.config.output.publicPath+item+'">'+title+'--'+item+'</a></li>');
178 self.config.plugins.push(
179 new HtmlWebpackPlugin({
180 template: 'html-withimg-loader!./html/'+item,
181 // inject: 'body',
182 filename:""+item,
183 chunks:["common",m[1]],
184 title: '最美奥运脸'
185 })
186 )
187 }
188 })
189 self.config.plugins.push(
190 new HtmlWebpackPlugin({
191 templateContent: '<!DOCTYPE html>'
192 +'<html lang="en">'
193 +'<head>'
194 +'<meta charset="UTF-8">'
195 +'<meta name="description" content="">'
196 +'<meta name="keywords" content="">'
197 +'<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">'
198 +'<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />'
199 +'<title>开发首页</title>'
200 +'</head>'
201 +'<body>'
202 +'<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>'
203 +'<ul>'+links.join("")+'</ul>'
204 +'</body>'
205 +'</html>',
206 inject: 'body',
207 chunks:[],
208 filename:"menu.html",
209 title: '最美奥运脸'
210 })
211 )
212 },
213 // 设置入口文件
214 genEntries:function(){
215 var jsReg=/(.+)\.(js)$/;
216 var jsFiles=fs.readdirSync("./js");
217 var map={};
218 jsFiles.forEach(function(item){
219 var m=item.match(jsReg);
220 if(m){
221 var entryName=m?m[1]:"";
222 map[entryName]="./js/"+m[0];
223 }
224 });
225 return map;
226 }
227})
228
229module.exports=SetOptions;
\No newline at end of file