yoho-entry-loader.js
1.68 KB
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
const shelljs = require('shelljs');
const _ = require('lodash');
const path = require('path');
module.exports = function(source) {
let transforSource = '';
const options = eval(source); //eslint-disable-line
transforSource += _.join(_.map(options.common, common => `import '${common}';`), '\r\n');
// transforSource += `
// window.loadStatic = (name) => {
// switch (name) {
// case 'product.new-detail':
// return import(/* webpackChunkName: "page.product.new-detail" */ './js/product/new-detail.page.js')
// }
// }`;
transforSource += `
window.loadStatic = (name) => {
switch (name) {${
_.join(_.map(options.page.regx, regx => {
return _.join(_.map(shelljs.ls(path.join(this.context, options.page.context, regx)), file => {
const dir = _.slice(file.split('/'), -3);
let moduleName, pageName;
if (_.last(dir) === 'index.js') { // [modulename, page, index.js]
moduleName = _.nth(dir, 0);
pageName = _.nth(dir, 1);
} else { // [xxx, modulename, xx.page.js]
moduleName = _.nth(dir, 1);
pageName = _.nth(dir, 2).match(/(.*).page.js/)[1];
}
return `
case '${moduleName}.${pageName}':
return import(/* webpackChunkName: "page.${moduleName}.${pageName}" */ './${path.relative(this.context, file)}');`; //eslint-disable-line
}), '');
}), '')
}
${
_.join(_.map(options.dll, (stat, dll) => {
return `
case '${dll}':
return import(/* webpackChunkName: "${dll}" */ '${stat}');`;
}))
}
}
}`;
return transforSource;
};