UNPKG

7.02 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const lodash = require("lodash");
4const webpack = require('webpack');
5const defaultExt = ['we', 'vue', 'js'];
6const webpackBuilder = require('./webpackBuilder');
7const vueLoaderConfig = require('./vueLoader');
8const utils = require('../utils');
9const webpackEntrys = {};
10
11class builder extends webpackBuilder {
12
13 /**
14 * 构造函数
15 * @param source
16 * @param dest
17 * @param options
18 */
19 constructor(source, dest, options = {}) {
20 if (!(options.ext && typeof options.ext === 'string')) {
21 options.ext = defaultExt.join('|');
22 }
23 super(source, dest, options);
24 }
25
26 /**
27 * 初始化参数
28 */
29 initConfig() {
30 process.env.__LOADER_TYPE = this.options.loaderType || "";
31 const destExt = path.extname(this.dest);
32 const sourceExt = path.extname(this.sourceDef);
33 let dir;
34 let filename;
35 let banner = `// { "framework": "Vue"} \nif(typeof app=="undefined"){app=weex}`;
36 let eeuiLog;
37 if (this.options.min) {
38 eeuiLog = fs.readFileSync(path.resolve(__dirname, 'eeuiLogProd.js'), 'utf8');
39 } else {
40 eeuiLog = fs.readFileSync(path.resolve(__dirname, 'eeuiLog.js'), 'utf8');
41 }
42 const plugins = [
43 new webpack.BannerPlugin({
44 banner: banner + `\n` + eeuiLog,
45 raw: true,
46 exclude: 'Vue'
47 })
48 ];
49 filename = '[name].js';
50 if (destExt && this.dest[this.dest.length - 1] !== '/' && sourceExt) {
51 dir = path.dirname(this.dest);
52 filename = path.basename(this.dest);
53 } else {
54 dir = this.dest;
55 }
56 if (this.options.onProgress) {
57 plugins.push(new webpack.ProgressPlugin(this.options.onProgress));
58 }
59 const webpackConfig = () => {
60 if (typeof this.source.length === "undefined") {
61 utils.each(this.source, (fileName, s) => {
62 webpackEntrys[fileName] = s + '?entry=true';
63 })
64 } else {
65 this.source.forEach(s => {
66 let fileName = path.relative(path.resolve(this.base), s).replace(/\.\w+$/, '');
67 webpackEntrys[fileName] = s + '?entry=true';
68 });
69 }
70 const configs = {
71 entry: () => {
72 return webpackEntrys
73 },
74 output: {
75 path: dir,
76 filename: filename
77 },
78 optimization: {
79 minimize: this.options.minimize || false
80 },
81 mode: this.options.mode || 'development',
82 watch: this.options.watch || false,
83 devtool: this.options.devtool || false,
84 module: {
85 rules: [{
86 test: /\.js$/,
87 use: [{
88 loader: 'babel-loader',
89 options: this.options.babelOptions || {
90 "presets": [
91 "@babel/react",
92 "@babel/env"
93 ]
94 }
95 }]
96 }]
97 },
98 resolve: {
99 extensions: ['.js', '.json', '.vue'],
100 alias: {
101 '@': path.resolve('src')
102 }
103 },
104 resolveLoader: {
105 modules: [path.resolve(__dirname, '../loaders'), path.resolve(__dirname, '../../node_modules'), path.resolve(process.cwd(), 'node_modules')],
106 extensions: ['.js', '.json', '.vue'],
107 mainFields: ['loader', 'main'],
108 moduleExtensions: ['-loader']
109 },
110 plugins: plugins
111 };
112 configs.module.rules.push({
113 test: /\.vue(\?[^?]+)?$/,
114 use: [{
115 loader: 'eeui-loader',
116 options: vueLoaderConfig({useVue: false})
117 }]
118 });
119 configs.node = {
120 setImmediate: false,
121 dgram: 'empty',
122 fs: 'empty',
123 net: 'empty',
124 tls: 'empty',
125 child_process: 'empty'
126 };
127 if (fs.existsSync(path.resolve('webpack.config.js'))) {
128 return lodash.merge(configs, require(path.resolve('webpack.config.js')));
129 } else {
130 return configs;
131 }
132 };
133 this.config = webpackConfig();
134 }
135
136 /**
137 * 添加入口文件
138 * @param array
139 * @returns {boolean}
140 */
141 insertEntry(array) {
142 if (!utils.likeArray(array)) {
143 array = [array];
144 }
145 let isAdd = false;
146 array.forEach((item) => {
147 let fileName,
148 sourcePath;
149 if (typeof item === "string") {
150 fileName = utils.rightDelete(utils.leftDelete(item, '/'), '.vue');
151 sourcePath = item + '?entry=true';
152 } else if (utils.isJson(item)) {
153 fileName = item.fileName;
154 sourcePath = item.sourcePath;
155 }
156 if (fileName && sourcePath) {
157 if (typeof webpackEntrys[fileName] === "undefined" || webpackEntrys[fileName] !== sourcePath) {
158 webpackEntrys[fileName] = sourcePath;
159 isAdd = true;
160 }
161 }
162 });
163 return isAdd ? this.webpackInvalidate() : true;
164 }
165
166 /**
167 * 移除入口文件
168 * @param array
169 * @returns {boolean}
170 */
171 removeEntry(array) {
172 if (!utils.likeArray(array)) {
173 array = [array];
174 }
175 let isDel = false;
176 array.forEach((item) => {
177 let fileName;
178 if (typeof item === "string") {
179 fileName = utils.rightDelete(utils.leftDelete(item, '/'), '.vue');
180 } else if (utils.isJson(item)) {
181 fileName = item.fileName;
182 }
183 if (fileName) {
184 if (typeof webpackEntrys[fileName] !== 'undefined') {
185 delete webpackEntrys[fileName];
186 isDel = true;
187 }
188 }
189 });
190 return isDel;
191 }
192
193 /**
194 * 作废当前编译循环
195 * @returns {boolean}
196 */
197 webpackInvalidate() {
198 try {
199 this.webpackWatching.invalidate();
200 return true;
201 } catch (e) {
202 return false;
203 }
204 }
205
206 /**
207 * 编译
208 * @param callback
209 * @returns {webpackBuilder}
210 */
211 build(callback) {
212 this.initConfig();
213 return super.build(callback);
214 }
215}
216
217module.exports = builder;
\No newline at end of file