-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
279 lines (266 loc) · 8.24 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"use strict";
const path = require('path');
const webpack = require('webpack');
const fs = require('fs-extra')
//const pkg = require('./package.json');
const glob = require("glob");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const argv = require('yargs').argv;
var defaultCleanFolder = ['dist']
if (argv.env) {
defaultCleanFolder = ['dist/'+argv.env]
}
const plugins = [
new CleanWebpackPlugin(defaultCleanFolder, {
verbose: true
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production"),
BUILD_TIME: JSON.stringify(new Date())
},
'global': '{}'
}),
new webpack.BannerPlugin({
banner: '// { "framework": "Vue" }\n',
raw: true
}),
new CopyWebpackPlugin([
{ from: './src/img', to: "./img" }
])
];
console.log('Building..., Please wait a moment.');
/*const getEntry = dir => {
const entryFiles = glob.sync(`./src/${dir}/*.js`, {});
var entries = {};
for (var i = 0; i < entryFiles.length; i++) {
var filePath = entryFiles[i];
//console.log(filePath);
//var filename = filePath.split('${dir}/')[1];
var filename = filePath.substr(0, filePath.lastIndexOf('.'));
console.log(filePath);
entries[filename] = filePath;
}
return entries;
};*/
var pluginObj = {};
var subProjectAssets = []
var commonImagePath = path.join(__dirname, './src/img')
const entryPath = path.resolve(__dirname, './entry/')
var webEntry = {
'index': './src/web-entry.js'
};
var weexEntry = {
'weex': './src/weex-entry.js'
}
const FILE_TYPE = '.vue'
// Wraping the entry file for web.
const getWebEntryFileContent = path => {
return `// 自动生成的入口文件
import Vue from 'vue'
import weex from 'weex-vue-render'
weex.init(Vue)
import App from '${path}${FILE_TYPE}'
new Vue({
el: '#root',
render: h => h(App)
})
`
}
// Wraping the entry file for native.
const getNativeEntryFileContent = path => {
return `// 自动生成的入口文件
import App from '${path}${FILE_TYPE}'
new Vue({
el: '#root',
render: h => h(App)
})
`
}
//默认编译T0x和midea-开关的文件夹, 目标文件夹如需要编译可添加到includeFiles
var includeFiles = ['midea-demo', 'midea-rooms','midea-card'];
function walk() {
if (argv.env) {
runWalk(argv.env)
} else {
let directory = path.join(__dirname, './src')
fs.readdirSync(directory)
.forEach(file => {
var fileStr = includeFiles.join(",") + ",";
if (file.indexOf("T0x") != -1 || fileStr.indexOf(file + ",") != -1) {
runWalk(file)
}
})
}
}
function runWalk(dir) {
console.log(dir);
dir = dir || ".";
let directory = path.join(__dirname, './src', dir)
let distPath = path.join(__dirname, 'dist')
let hasTargetFile = false
fs.readdirSync(directory)
.forEach(file => {
let fullpath = path.join(directory, file)
let stat = fs.statSync(fullpath)
let extname = path.extname(fullpath)
if (stat.isFile() && extname === FILE_TYPE) {
let targetDir = dir
if (dir.indexOf("midea-card")==0){
targetDir = dir + '/midea-card'
}
let name = path.join(targetDir, path.basename(file, extname))
let webEntryFile = path.resolve(entryPath, name + '.web.js')
let weexEntryFile = path.resolve(entryPath, name + '.js')
// console.log(weexEntryFile + "@@" + directory + "@@" + name);
fs.outputFileSync(webEntryFile, getWebEntryFileContent('@/' + dir.replace('\\', '/') + '/' + path.basename(file, extname)))
fs.outputFileSync(weexEntryFile, getNativeEntryFileContent('@/' + dir.replace('\\', '/') + '/' + path.basename(file, extname)))
webEntry[name] = webEntryFile;
weexEntry[name] = weexEntryFile;
hasTargetFile = true
} else if (stat.isDirectory()) {
if (file == "assets") {
let targetDir = dir
if (dir.indexOf("midea-card")==0){
targetDir = dir + '/midea-card'
}
let targetImgFolder = path.join(distPath, targetDir, "assets")
subProjectAssets.push({ from: fullpath, to: targetImgFolder })
} else if (file == "static") {
let targetDir = dir
let targetImgFolder = path.join(distPath, targetDir, "static")
subProjectAssets.push({ from: fullpath, to: targetImgFolder })
} else if (file !== "components") {
let subdir = path.join(dir, file)
runWalk(subdir)
}
}
})
if (hasTargetFile) {
let targetDir = dir
if (dir.indexOf("midea-card")==0){
targetDir = dir + '/midea-card'
}
plugins.push(
new CopyWebpackPlugin([
{ from: commonImagePath, to: path.join(distPath, targetDir, "img") }
])
)
if (fs.existsSync(path.join(directory, 'weex.html'))) {
plugins.push(
new CopyWebpackPlugin([
{ from: path.join(directory, 'weex.html'), to: path.join(distPath, targetDir) }
])
)
}
}
}
walk()
plugins.push(
new CopyWebpackPlugin(subProjectAssets)
)
const getBaseConfig = () => (
{
//devtool: '#source-map',
context: __dirname,
output: {
path: path.join(__dirname, 'dist')
/* publicPath: '/',
filename: 'aa/[name].js'
libraryTarget: 'umd',
library: `npm/${pkg.name}/[name]`,
umdNamedDefine: false*/
},
stats: {
colors: true,
modules: false,
reasons: false
},
module: {
rules: [{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
}
},
// exclude: /node_modules(?!\/.*(weex).*)/
}, {
test: /\.vue(\?[^?]+)?$/,
use: []
},
{
test: /\.css$/,
use: [{
loader: 'css-loader'
}]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 1000
}
}
]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
}
]
},
plugins,
devServer: {
inline: true,
hot: true,
headers: {
"Cache-Control": "no-cache"
}
},
resolve: {
extensions: ['.js'],
modules: [
'node_modules'
],
alias: {
'@': path.resolve(__dirname, 'src')
}
}
});
const webCfg = getBaseConfig();
console.log(webEntry);
webCfg.entry = webEntry;
webCfg.output.filename = '[name].web.js';
webCfg.module.rules[1].use.push({
loader: 'vue-loader',
options: {
compilerModules: [
{
postTransformNode: el => {
el.staticStyle = `$processStyle(${el.staticStyle})`
el.styleBinding = `$processStyle(${el.styleBinding})`
}
}
]
}
});
const nativeCfg = getBaseConfig();
console.log(weexEntry);
nativeCfg.entry = weexEntry;
nativeCfg.output.filename = '[name].js';
nativeCfg.module.rules[1].use.push('weex-loader');
const exportConfig = [
// webCfg,
nativeCfg
];
module.exports = exportConfig;