-
Notifications
You must be signed in to change notification settings - Fork 33
/
script-h5.js
283 lines (257 loc) · 8.75 KB
/
script-h5.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
280
281
282
283
/**
* @file gulp plugin
* @author zhangjingyuan02
*/
/* eslint-disable fecs-no-require */
/* eslint-disable no-native-reassign */
/* eslint-disable fecs-min-vars-per-destructure */
const babel = require('babel-core');
const fs = require('fs');
const mkdirp = require('mkdirp');
const transformScriptPlugin = require('../../h5/transform/plugins/transformScriptPlugin');
const transformMainPlugin = require('../../h5/transform/plugins/transformMainPlugin');
const transformCompPlugin = require('../../h5/transform/plugins/transformCompPlugin');
const transformRouterPlugin = require('../../h5/transform/plugins/transformRouterPlugin');
const transformAppPlugin = require('../../h5/transform/plugins/transformAppPlugin');
const transformGetAppPlugin = require('../../h5/transform/plugins/transformGetAppPlugin');
const postTransformScriptPlugin = require('./babel-plugin-script-post');
const MARS_ENV = process.env.MARS_ENV_TARGET || 'h5';
exports.preCompile = function (file) {
};
exports.postCompile = function (file) {
};
exports.compileRouter = function (content, options) {
const {config = {}, mars} = options;
const {
pages = [],
subPackages = []
} = config;
let subPages = [];
if (subPackages && subPackages.length > 0) {
subPackages.forEach(item => {
if (!item.pages) {
return;
}
if (item.pages.length > 0) {
item.pages.forEach(route => {
subPages.push(`${item.root}/${route}`);
});
}
else {
subPages.push(`${item.root}/${item.pages[0]}`);
}
});
}
// 判断当前MARS_ENV模式下是否有对应的页面数组
let envPages = [];
if (config && config['pages-' + MARS_ENV]) {
envPages = config['pages-' + MARS_ENV];
}
const routes = pages.concat(subPages, envPages);
const mode = mars && mars.mode ? mars.mode : 'history';
const routerRet = babel.transform(content.toString(), {
plugins: [transformRouterPlugin({
routes,
mode
})]
});
const routerStr = babel.transformFromAst(routerRet.ast).code;
content = new Buffer(routerStr);
return content;
};
exports.compileComponents = function (file, options) {
let componentSet = file._info_.componentSet;
// 注册App.vue 里的 回到主页
Object.assign(componentSet, {
'mars-navigator': 'MarsNavigator'
});
const devCompPath = options.devCompPath;
file.contents = new Buffer('export default basicComponents;');
const componentsRet = babel.transform(file.contents.toString(), {
plugins: [transformCompPlugin({
componentSet,
devCompPath
})]
});
const componentsStr = babel.transformFromAst(componentsRet.ast).code;
file.contents = new Buffer(componentsStr);
return file;
};
exports.compileMain = function (content, options) {
let {
mainOptions,
mars,
componentSet // componentSet 决定了main文件是否引入components
} = options;
let {
pages,
tabBar,
window = null,
pagesInfo
} = mainOptions;
const {
backgroundColor,
borderStyle,
color,
selectedColor,
list = []
} = tabBar || {};
const tabBarStyle = {
backgroundColor,
borderStyle,
color,
selectedColor
};
const mainRet = babel.transform(content.toString(), {
plugins: [transformMainPlugin({
routes: list,
tabBarStyle,
window,
mars: {
navigationBarHomeColor: mars.navigationBarHomeColor === undefined
? 'dark'
: mars.navigationBarHomeColor,
showNavigationBorder: !!mars.showNavigationBorder,
useTransition: mars.useTransition === undefined ? true : mars.useTransition,
homePage: `/${list.length > 0 ? list[0].pagePath : pages[0]}`,
supportPWA: !!mars.supportPWA
},
componentSet,
pagesInfo
})]
});
const mainStr = babel.transformFromAst(mainRet.ast).code;
content = new Buffer(mainStr);
return content;
};
const path = require('path');
const compileModules = require('../file/compileModules');
const {resolveComponentsPath, getUIModules} = compileModules;
exports.compileScript = async function (content, options = {}) {
if (!content) {
return null;
}
const {
isApp = false,
mpConfig,
target,
dest,
mars
} = options;
let baseOptions = {}; // 收集 config 和 components
content = content.replace(
/process\.env\.MARS_ENV/g,
JSON.stringify(MARS_ENV)
).replace(
/process\.env\.NODE_ENV/g,
JSON.stringify(process.env.NODE_ENV || 'development')
);
const useAOP = mars.useAOP === undefined ? true : mars.useAOP;
const scriptRet = babel.transform(content, {
plugins: [
transformScriptPlugin({
baseOptions,
isApp,
mpConfig,
useAOP
})
]
});
let scriptStr = babel.transformFromAst(scriptRet.ast).code;
// 处理完再进行minify,发现minify和定制的插件会有坑
const cleanScriptAst = babel.transform(scriptStr, {
plugins: [
'minify-guarded-expressions',
'minify-dead-code-elimination'
]
});
scriptStr = babel.transformFromAst(cleanScriptAst.ast).code;
content = new Buffer(scriptStr);
const {config = {}, components = {}, enableConfig = null} = baseOptions;
const uiModules = getUIModules(components, target);
const destPath = path.resolve(dest.path);
const rPath = path.relative(path.dirname(options.path), destPath);
let usedMoudles = {};
content = babel.transform(content, {
plugins: [
[
path.resolve(__dirname, '../file/babel-plugin-relative-import.js'),
{
rPath,
modules: Object.assign(compileModules.H5Modules, uiModules),
usedMoudles
}
]
]
}).code;
resolveComponentsPath(components, usedMoudles);
await compileModules.compileUIModules(uiModules, destPath);
return {config, components, enableConfig, content};
};
exports.postCompileScript = function (content, options = {}) {
const {componentsInUsed} = options;
return babel.transform(content, {
plugins: [
postTransformScriptPlugin({
componentsInUsed
})
]
}).code;
};
exports.compileApp = function (script) {
const content = script.content;
// let customExp = {}; // 收集 用户 自定义 数据和方法
// console.log(content.toString());
// const scriptRet = babel.transform(content, {
// plugins: [
// transformAppPlugin({
// customExp
// })
// ]
// });
// let scriptStr = babel.transformFromAst(scriptRet.ast).code;
// 处理完再进行minify,发现minify和定制的插件会有坑
const cleanScriptAst = babel.transform(content, {
plugins: [
'minify-guarded-expressions',
'minify-dead-code-elimination'
]
});
const scriptStr = babel.transformFromAst(cleanScriptAst.ast).code;
// 更新 APP.vue script
const newContent = `
import app from './appApi.js';
export default app;
`;
script.content = new Buffer(newContent);
return scriptStr;
};
exports.compileApi = function (content, options) {
let apiLibrary = options.devApiPath ? options.devApiPath : '@marsjs/api';
let contentStr = content.toString();
contentStr = `
import Mars, {directives} from '${apiLibrary}';
${contentStr}
`;
content = new Buffer(contentStr);
return content;
};
// 处理tabBar iconPath,更改icon目录为默认目录,解决动态require 引发的context为src目录情况
exports.compileTabBar = function (options, {dest, baseDir}) {
// 处理tabBar iconPath
function dealIconPath(path) {
let pathArr = path.split('/');
let newPath = `${pathArr[pathArr.length - 1]}`;
let iconContent = fs.readFileSync(`${baseDir}${path}`);
fs.writeFileSync(process.cwd() + `/${dest}/tabBarIcons/${newPath}`, iconContent);
return newPath;
}
mkdirp.sync(dest + '/tabBarIcons');
const tabBarList = options && options.tabBar && options.tabBar.list ? options.tabBar.list : [];
tabBarList.forEach(item => {
if (item.iconPath && item.selectedIconPath) {
item.iconPath = dealIconPath(item.iconPath);
item.selectedIconPath = dealIconPath(item.selectedIconPath);
}
});
};