-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
218 lines (205 loc) · 7.42 KB
/
webpack.config.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const pathTo = require('path');
const fs = require('fs-extra');
const webpack = require('webpack');
// var ExtractTextPlugin = require('extract-text-webpack-plugin') // 分离css文件的插件
// var CopyWebpackPlugin = require('copy-webpack-plugin') // 复制文件和目录的插件
// var CleanWebpackPlugin = require('clean-webpack-plugin') // 清理dist目录插件
const entry = { index: pathTo.resolve('src', 'entry.js') };
const weexEntry = { index: pathTo.resolve('src', 'entry.js') };
const vueWebTemp = 'dist';
const hasPluginInstalled = fs.existsSync('./web/plugin.js');
var isWin = /^win/.test(process.platform);
function getEntryFileContent(entryPath, vueFilePath) {
let relativePath = pathTo.relative(pathTo.join(entryPath, '../'), vueFilePath);
let contents = '';
if (hasPluginInstalled) {
const plugindir = pathTo.resolve('./web/plugin.js');
contents = 'require(\'' + plugindir + '\') \n';
}
if (isWin) {
relativePath = relativePath.replace(/\\/g, '\\\\');
}
contents += 'var App = require(\'' + relativePath + '\')\n';
contents += 'App.el = \'#root\'\n';
contents += 'new Vue(App)\n';
return contents;
}
var fileType = '';
function walk(dir) {
dir = dir || '.';
const directory = pathTo.join(__dirname, 'src', dir);
fs.readdirSync(directory)
.forEach((file) => {
const fullpath = pathTo.join(directory, file);
const stat = fs.statSync(fullpath);
const extname = pathTo.extname(fullpath);
const basename = pathTo.basename(fullpath);
if (stat.isFile() && extname === '.vue' && basename != 'App.vue') {
if (!fileType) {
fileType = extname;
}
if (fileType && extname !== fileType) {
console.log('Error: This is not a good practice when you use ".we" and ".vue" togither!');
}
const name = pathTo.join(dir, pathTo.basename(file, extname));
if (extname === '.vue') {
const entryFile = pathTo.join(vueWebTemp, dir, pathTo.basename(file, extname) + '.js');
fs.outputFileSync(pathTo.join(entryFile), getEntryFileContent(entryFile, fullpath));
entry[name] = pathTo.join(__dirname, entryFile) + '?entry=true';
}
weexEntry[name] = fullpath + '?entry=true';
} else if (stat.isDirectory() && ['build', 'include', 'assets', 'filters', 'mixins'].indexOf(file) == -1) {
const subdir = pathTo.join(dir, file);
walk(subdir);
}
});
}
walk();
// web need vue-loader
const plugins = [
// new webpack.optimize.CommonsChunkPlugin('vendor'),
// new webpack.NoErrorsPlugin(), // 允许错误不打断程序
// new webpack.optimize.UglifyJsPlugin({ //压缩代码
// output: {
// comments: false, //不移除注释
// },
// compress: {
// warnings: false //忽略警告,某些没有用到的组件在打包时会被移除,这时会产生警告,无需在意,webpack1默认true,webpack2默认false
// },
// }),
new webpack.DefinePlugin({
'process.env': '"production"' //这样
}),
// new webpack.ProvidePlugin({
// jQuery: "jquery",
// $: "jquery"
// }),
new webpack.BannerPlugin({
banner: '// { "framework": ' + (fileType === '.vue' ? '"Vue"' : '"Weex"') + '} \n',
raw: true,
exclude: 'Vue'
})
];
const webConfig = {
context: pathTo.join(__dirname, ''),
entry: entry,
output: {
path: pathTo.join(__dirname, 'dist'),
filename: '[name].web.js',
},
// resolve: {
// extensions: ['', '.js', '.vue', '.less', '.css', '.scss'],
// // fallback: [pathTo.join(__dirname, '../node_modules')],
// alias: {
// 'vue$': 'vue/dist/vue.common.js',
// 'src': pathTo.resolve(__dirname, '../src'),
// 'assets': pathTo.resolve(__dirname, '../src/assets'),
// 'components': pathTo.resolve(__dirname, '../src/components')
// }
// },
// resolveLoader: {
// fallback: [pathTo.join(__dirname, '../node_modules')]
// },
// devtool: 'source-map',
module: {
// webpack 2.0
rules: [{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: {
presets: ['es2015']
},
}],
exclude: /node_modules(?!(\/|\\).*(weex).*)/
},
{
test: /\.(css)$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.(scss|sass)$/,
loader: "vue-style-loader!css-loader!sass-loader", //这样写样式和脚本会混在一个文件,也就是不分离样式,webpack有插件可以实现分离样式
exclude: /node_modules/
},
{
test: /\.(eot(|\?v=.*)|woff(|\?v=.*)|woff2(|\?v=.*)|ttf(|\?v=.*)|svg(|\?v=.*))$/,
loader: 'file-loader',
options: { name: 'fonts/[name].[ext]' },
},
{
test: /\.vue(\?[^?]+)?$/,
use: [{
loader: 'vue-loader'
}]
}
]
},
plugins: plugins
};
const weexConfig = {
entry: weexEntry,
output: {
path: pathTo.join(__dirname, 'dist'),
filename: '[name].js',
},
module: {
rules: [{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: {
presets: ['es2015']
},
}],
exclude: /node_modules(?!(\/|\\).*(weex).*)/
},
{
test: /\.vue(\?[^?]+)?$/,
use: [{
loader: 'weex-loader',
options: {
loaders: {
scss: 'style-loader!css-loader!sass-loader',
sass: 'style-loader!css-loader!sass-loader?indentedSyntax'
},
},
}],
},
{
test: /\.(css)$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.(scss|sass)$/,
loader: "vue-style-loader!css-loader!sass-loader", //这样写样式和脚本会混在一个文件,也就是不分离样式,webpack有插件可以实现分离样式
exclude: /node_modules/
},
{
test: /\.(eot(|\?v=.*)|woff(|\?v=.*)|woff2(|\?v=.*)|ttf(|\?v=.*)|svg(|\?v=.*))$/,
loader: 'file-loader',
options: { name: 'fonts/[name].[ext]' },
},
{
test: /\.we(\?[^?]+)?$/,
use: [{
loader: 'weex-loader'
}]
}
],
},
node: {
setImmediate: false
// See "Other node core libraries" for additional options.
},
plugins: plugins,
};
var exports = [webConfig, weexConfig];
//var exports = [weexConfig];
module.exports = exports;