UNPKG

7.45 kBJavaScriptView Raw
1let utils = light.util;
2let config = light.config;
3let shell = utils.require('shelljs');
4let version = require('./package.json').version;
5
6let vueTemplate = utils.template(`
7/**AUTO-GEN-START*/
8import __VUE__ from "vue"
9__VUE__.use(__LIGHT__);
10import __LIGHT__ from "light"
11__LIGHT__.mainPage = '<%=mainPage%>'
12__LIGHT__.onlyFor = '<%=onlyFor%>'
13__LIGHT__
14<%views.forEach(function(view){%>
15.route({
16 path: '/<%=view.id%>',
17 <%if(view.async=="true"){%>component: ()=>{return import(/* webpackChunkName: "<%=view.chunkname||page+'-'+view.name%>" */"./<%=view.id%>.vue")},<%}%>
18 <%if(!view.async||view.async=="false"){%>component: require("./<%=view.id%>.vue"),<%}%>
19 <%if(view.parent){%>parent:"/<%=view.parent%>",<%}%>
20 <%if(view.home){%>home:"/<%=view.home%>",<%}%>
21 <%for(var key in view){%><%if(['id', 'async', 'parent', 'home', 'chunkname'].indexOf(key) == -1){%><%=key%>: "<%=view[key]%>",<%}%><%}%>
22})
23<%})%>
24/**AUTO-GEN-END*/
25`);
26
27
28
29module.exports = {
30 pluginName: `type-vue@${version}`,
31 install: function (light) {
32 },
33
34 /**
35 * 准备阶段
36 */
37 prepare: function (light, cb) {
38 // 修复在lighting工程中执行light searver --debug 时无法正常执行的问题
39 if (!utils.existsSync(config.dist)) return cb();
40
41 var Module = require('module');
42 var originalRequire = Module.prototype.require;
43
44 Module.prototype.require = function (m) {
45 //do your thing here
46 if (m === "./JsonpMainTemplatePlugin") {
47 return require("./plugins/JsonpMainTemplatePlugin.js")
48 }
49 return originalRequire.apply(this, arguments);
50 };
51
52 //准备编译
53 let promise;
54
55 //1. 自定义标签的处理
56 //1.1 view标签的处理
57
58 let works = [];
59 utils.glob(`${config.dist}/*.html`, function (err, files) {
60 //105239 多页工程支持分页面打包
61 let fs = require('fs');
62 if (light.options.page && fs.existsSync(`${config.dist}/${light.options.page}.html`)) {
63 files = [`${config.dist}/${light.options.page}.html`]
64 }
65 files.forEach(function (file) {
66 let viewsArr = [];
67 works.push(new Promise(function (resolve, reject) {
68 let temparr = file.split('/');
69 let filename = temparr[temparr.length - 1];
70 filename = filename.replace('.html', '');
71 let $ = utils.require("cheerio").load(utils.readFileSync(file), {
72 recognizeSelfClosing: true
73 });
74
75 let views = $("view");
76 views.first().replaceWith("<router-view></router-view>");
77 views.remove();
78
79 utils.outputFile(file, $.html(), (err) => {
80 viewsArr = {
81 name: filename,
82 views: views
83 }
84 resolve(viewsArr);
85 });
86
87 }))
88 })
89
90 Promise.all(works).then(function (viewsArr) {
91
92 //1.2 根据视图内容生成视图注册的模板,注册路由
93 //105231 确定主页名称为app或index并注入Light
94 var mainPage = require('./plugins/entryFile').getEntryFile().mainPage;
95
96 viewsArr.forEach(function (viewObj) {
97 let viewArr = viewObj.views;
98 let v = [];
99 viewArr.each(function (i, view) {
100 let id = view.attribs.id;
101 view.attribs.name = id.replace(/\//g, '-')
102 v.push(view.attribs)
103 });
104 //110579:将onlyFor注入Light
105 let vueViewString = vueTemplate({ views: v, page: viewObj.name, mainPage: mainPage, onlyFor: light.options.onlyFor });
106 utils.writeFileSync(`${config.dist}/view/${viewObj.name}.js`, `${vueViewString}`);
107
108 })
109
110 cb();
111
112 });
113 })
114
115 },
116
117 /**
118 * 编译阶段
119 */
120 build(light, cb) {
121 //2 编译,通过webpack把所有资源整合起来
122 new Promise(function (resolve, reject) {
123 process.chdir(config.dist);
124 process.mainModule.paths.push(`${__dirname}/node_modules`);
125
126 //环境变量
127 process.env.NODE_ENV = process.env.NODE_ENV || "production";
128 return require("./build").build().then(resolve);
129 }).then(cb);
130 },
131
132 /**
133 * 打包阶段
134 */
135 packager(light, cb) {
136 shell.rm("-fr", `${config.dist}/view`);
137 shell.rm("-fr", `${config.dist}/ui`);
138 shell.rm("-fr", `${config.dist}/html`);
139 shell.rm("-fr", `${config.dist}/js`);
140 shell.rm("-fr", `${config.dist}/lib`);
141
142 //145355 打包时移除根目录的app.less
143 shell.rm("-fr", `${config.dist}/app.less`);
144 shell.rm("-fr", `${config.dist}/api`);
145 shell.rm("-fr", `${config.dist}/plugins`);
146 //114521 【工具】工程根目录中有node_modules目录时移除它
147 shell.rm("-fr",`${config.dist}/node_modules`);
148 //118795 lighting的native插件打包时-p,此时删除目录中存在的modules目录
149 shell.rm("-fr",`${config.dist}/modules`);
150 //116125【TS:201812210068-财富业委会-潘腾-【需求类型】需求【需求描述】1. light release -p或者-up时,能增加参数支持 不要将某些文件夹打入zip包
151 if(light.options.except){
152 var exceptFolders = light.options.except.split(",");
153 exceptFolders.forEach(function(folder){
154 shell.rm("-fr", `${config.dist}/${folder}`);
155 });
156 }
157
158
159 //184657【type-vue插件】打包阶段将框架信息写入project.json文件
160 //189105【type-vue插件】修复macOS上打包报错的问题
161 writeProjectInfo();
162 function writeProjectInfo() {
163 if(utils.getLightingVersion) {
164 var path = require('path'), fs = require('fs');
165 light.config.lightToolkits = {
166 lighting: utils.getLightingVersion(),
167 plugins : []
168 }
169
170 if(!config.plugins) config['plugins'] = ['type-vue']
171 var plugins = config.plugins;
172
173 plugins && plugins.forEach((plugin)=>{
174 let pluginConfigDir = utils.join(config.pluginsDir, `node_modules/lighting-plugin-${plugin}/package.json`);
175 let pluginVersion = require(pluginConfigDir).version;
176 plugin = plugin.replace('@'+pluginVersion, '');
177 light.config.lightToolkits.plugins.push(`${plugin}@${pluginVersion}`)
178 })
179
180 let projectConfigFile = path.join(config.dist,"project.json");
181 if(utils.existsSync(projectConfigFile)){
182 let projectConfig = require(projectConfigFile);
183 projectConfig['lightToolkits'] = light.config.lightToolkits;
184 fs.writeFileSync(projectConfigFile, JSON.stringify(projectConfig,null,"\t"))
185 }
186 }
187 }
188
189 cb();
190 },
191
192 /**
193 * 运行阶段
194 */
195 runtime: function () {
196
197 }
198};
\No newline at end of file