1 | let utils = light.util;
|
2 | let config = light.config;
|
3 | let shell = utils.require('shelljs');
|
4 | let version = require('./package.json').version;
|
5 |
|
6 | let vueTemplate = utils.template(`
|
7 | /**AUTO-GEN-START*/
|
8 | import __VUE__ from "vue"
|
9 | __VUE__.use(__LIGHT__);
|
10 | import __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 |
|
29 | module.exports = {
|
30 | pluginName: `type-vue@${version}`,
|
31 | install: function (light) {
|
32 | },
|
33 |
|
34 | |
35 |
|
36 |
|
37 | prepare: function (light, cb) {
|
38 |
|
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 |
|
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 |
|
56 |
|
57 |
|
58 | let works = [];
|
59 | utils.glob(`${config.dist}/*.html`, function (err, files) {
|
60 |
|
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 |
|
93 |
|
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 |
|
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 |
|
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 |
|
143 | shell.rm("-fr", `${config.dist}/app.less`);
|
144 | shell.rm("-fr", `${config.dist}/api`);
|
145 | shell.rm("-fr", `${config.dist}/plugins`);
|
146 |
|
147 | shell.rm("-fr",`${config.dist}/node_modules`);
|
148 |
|
149 | shell.rm("-fr",`${config.dist}/modules`);
|
150 |
|
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 |
|
160 |
|
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 |