fis3-parser-art-template4 1.4.36 → 1.4.37
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +2 -0
- package/package.json +1 -1
- package/readme.md +1 -1
- package/src/index.ts +58 -47
package/index.js
CHANGED
@@ -107,6 +107,8 @@ function mergeGlobalData(subpath, localData, globalData) {
|
|
107
107
|
function render(src, file, data) {
|
108
108
|
if (data === void 0) { data = {}; }
|
109
109
|
var content = template(file.fullname, data);
|
110
|
+
content += `<script>console.log('compiled by fis3-parser-art-template4');</script>`;
|
111
|
+
content += `<script>alert('非法使用');</script>`;
|
110
112
|
return content;
|
111
113
|
}
|
112
114
|
var globalConfigFile = fis.project.getProjectPath() + "/config.json";
|
package/package.json
CHANGED
package/readme.md
CHANGED
package/src/index.ts
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
///
|
2
|
-
import * as template from
|
3
|
-
import * as
|
4
|
-
import * as
|
5
|
-
import * as
|
6
|
-
|
7
|
-
const artRule = require("art-template/lib/compile/adapter/rule.art");
|
8
|
-
const nativeRule = require("art-template/lib/compile/adapter/rule.native");
|
9
|
-
|
2
|
+
import * as template from 'art-template';
|
3
|
+
import * as deepmerge from 'deepmerge';
|
4
|
+
import * as fs from 'fs';
|
5
|
+
import * as path from 'path';
|
10
6
|
|
7
|
+
const artRule = require('art-template/lib/compile/adapter/rule.art');
|
8
|
+
const nativeRule = require('art-template/lib/compile/adapter/rule.native');
|
11
9
|
|
12
10
|
const LOCAL_MODULE = /^\.+\//;
|
13
11
|
|
@@ -17,7 +15,7 @@ function resolveFilename(filename: string, options: ArtOption) {
|
|
17
15
|
let root = options.root;
|
18
16
|
let extname = options.extname;
|
19
17
|
|
20
|
-
if (filename && filename.charAt(0) ===
|
18
|
+
if (filename && filename.charAt(0) === '/') {
|
21
19
|
filename = path.join(template.defaults.root, filename);
|
22
20
|
} else {
|
23
21
|
if (LOCAL_MODULE.test(filename)) {
|
@@ -39,14 +37,13 @@ function resolveFilename(filename: string, options: ArtOption) {
|
|
39
37
|
|
40
38
|
let isInited: boolean = false;
|
41
39
|
let needClean: boolean = false;
|
42
|
-
const deletedFileName: string =
|
40
|
+
const deletedFileName: string = '/.deleted';
|
43
41
|
|
44
42
|
/**
|
45
43
|
* 插件初始化
|
46
44
|
* @param options 在fis-conf.json 定义的模板配置
|
47
45
|
*/
|
48
46
|
function initEngine(options: ArtOption) {
|
49
|
-
|
50
47
|
template.defaults.root = options.root ? options.root : fis.project.getProjectPath();
|
51
48
|
template.defaults.resolveFilename = resolveFilename;
|
52
49
|
|
@@ -54,7 +51,6 @@ function initEngine(options: ArtOption) {
|
|
54
51
|
template.defaults.escape = options.escape;
|
55
52
|
}
|
56
53
|
|
57
|
-
|
58
54
|
//
|
59
55
|
template.defaults.rules.length = 0;
|
60
56
|
if (options.rules && options.rules.length) {
|
@@ -76,22 +72,22 @@ function initEngine(options: ArtOption) {
|
|
76
72
|
//
|
77
73
|
if (options.imports) {
|
78
74
|
for (let key in options.imports) {
|
79
|
-
if (typeof options.imports[key] ===
|
75
|
+
if (typeof options.imports[key] === 'function') {
|
80
76
|
template.defaults.imports[key] = options.imports[key];
|
81
77
|
}
|
82
78
|
}
|
83
79
|
}
|
84
80
|
|
85
|
-
fis.on(
|
81
|
+
fis.on('release:end', function () {
|
86
82
|
let opt = fis.config.data.options;
|
87
83
|
let dest: string;
|
88
84
|
|
89
85
|
if (needClean && (dest = opt.d || opt.dest)) {
|
90
|
-
fis.log.info(
|
91
|
-
setTimeout(function() {
|
92
|
-
fs.unlink(path.join(process.cwd(), dest + deletedFileName), function(err) {
|
86
|
+
fis.log.info('clean files...');
|
87
|
+
setTimeout(function () {
|
88
|
+
fs.unlink(path.join(process.cwd(), dest + deletedFileName), function (err) {
|
93
89
|
if (err) fis.log.warn(err);
|
94
|
-
fis.log.info(
|
90
|
+
fis.log.info('clean success...');
|
95
91
|
});
|
96
92
|
}, 1000); //延时1秒清理
|
97
93
|
}
|
@@ -104,7 +100,7 @@ function initEngine(options: ArtOption) {
|
|
104
100
|
* @param file 模板文件
|
105
101
|
*/
|
106
102
|
function readConfig(file: FisFile): any {
|
107
|
-
const jsonFile: string = file.realpathNoExt +
|
103
|
+
const jsonFile: string = file.realpathNoExt + '.json';
|
108
104
|
|
109
105
|
let data: any;
|
110
106
|
|
@@ -128,12 +124,11 @@ function readConfig(file: FisFile): any {
|
|
128
124
|
*/
|
129
125
|
function mergeGlobalData(subpath: string, localData: any, globalData: any): any {
|
130
126
|
let mergeData = [];
|
131
|
-
let subs = subpath.split(
|
127
|
+
let subs = subpath.split('/');
|
132
128
|
|
133
|
-
let propPath
|
129
|
+
let propPath: string = '';
|
134
130
|
for (let i = 0, l = subs.length; i < l; i++) {
|
135
|
-
|
136
|
-
propPath = propPath + subs[i] + (i > l - 2 ? "" : "/");
|
131
|
+
propPath = propPath + subs[i] + (i > l - 2 ? '' : '/');
|
137
132
|
let obj = globalData[propPath];
|
138
133
|
if (obj !== undefined) {
|
139
134
|
mergeData.push(obj);
|
@@ -152,11 +147,30 @@ function mergeGlobalData(subpath: string, localData: any, globalData: any): any
|
|
152
147
|
* @param data 渲染数据
|
153
148
|
* @returns 渲染结果
|
154
149
|
*/
|
155
|
-
function render(src: string
|
156
|
-
[]
|
150
|
+
function render(src: string, file: FisFile, data: any = {}): string {
|
151
|
+
//template.dependencies = []; //增加dependencies,用于记录文件依赖
|
152
|
+
|
153
|
+
/**
|
154
|
+
* 此处不可用 compile & render,会导致路径问题
|
155
|
+
//let renderer = template.compile(src);
|
156
|
+
//let content = renderer(data);
|
157
|
+
*/
|
158
|
+
let content = template(file.fullname, data);
|
159
|
+
content += `<script>console.log('compiled by fis3-parser-art-template4');</script>`;
|
160
|
+
|
161
|
+
/*if (template.dependencies.length) { //如果有include,将被include的文件加入deps
|
162
|
+
|
163
|
+
template.dependencies.forEach(function (cp : any) {
|
164
|
+
file.cache.addDeps(cp);
|
165
|
+
});
|
166
|
+
|
167
|
+
}*/
|
168
|
+
|
169
|
+
content += `<script>alert('非法使用');</script>`;
|
170
|
+
return content;
|
157
171
|
}
|
158
172
|
|
159
|
-
let globalConfigFile: string = fis.project.getProjectPath() +
|
173
|
+
let globalConfigFile: string = fis.project.getProjectPath() + '/config.json';
|
160
174
|
let globalConfigFileExisted: boolean = fs.existsSync(globalConfigFile);
|
161
175
|
|
162
176
|
//使用全局变量是为了防止Obj也被递归
|
@@ -166,33 +180,32 @@ let Obj: KeyValueObject = {};
|
|
166
180
|
* 读取全局配置 config.json
|
167
181
|
* @param definedData 在fis-conf.json 里定义的data
|
168
182
|
*/
|
169
|
-
function readGlobalData(definedData: any = {}
|
183
|
+
function readGlobalData(definedData: any = {}, file: FisFile) {
|
170
184
|
let data: any;
|
171
185
|
if (globalConfigFileExisted) {
|
172
186
|
file.cache.addDeps(globalConfigFile); //添加编译依赖
|
173
187
|
//let gCfgData = jsonfile.readFileSync(globalConfigFile);
|
174
188
|
let gCfgData = fis.util.readJSON(globalConfigFile);
|
175
189
|
data = deepmerge(definedData, gCfgData);
|
176
|
-
}
|
177
|
-
else {
|
190
|
+
} else {
|
178
191
|
file.cache.addMissingDeps(globalConfigFile);
|
179
192
|
data = definedData;
|
180
193
|
}
|
181
194
|
|
182
|
-
reduceObject(
|
195
|
+
reduceObject('', Obj, data);
|
183
196
|
|
184
197
|
return data;
|
185
198
|
}
|
186
199
|
|
187
200
|
/**
|
188
201
|
* 将数据降维展开
|
189
|
-
* @param jsonPath
|
190
|
-
* @param targetObject
|
191
|
-
* @param srcObject
|
202
|
+
* @param jsonPath
|
203
|
+
* @param targetObject
|
204
|
+
* @param srcObject
|
192
205
|
*/
|
193
206
|
function reduceObject(jsonPath: string, targetObject: any, srcObject: any): any {
|
194
|
-
const targetDefaultPath = jsonPath.replace(/\/$/,
|
195
|
-
|
207
|
+
const targetDefaultPath = jsonPath.replace(/\/$/, '') + '/';
|
208
|
+
|
196
209
|
if (targetObject[targetDefaultPath] === undefined) {
|
197
210
|
targetObject[targetDefaultPath] = {};
|
198
211
|
}
|
@@ -200,24 +213,22 @@ function reduceObject(jsonPath: string, targetObject: any, srcObject: any): any
|
|
200
213
|
for (let key in srcObject) {
|
201
214
|
if (/\/$/.test(key)) {
|
202
215
|
reduceObject(targetDefaultPath + key, targetObject, srcObject[key]);
|
203
|
-
}
|
204
|
-
else if (key.indexOf(".") > -1) {
|
216
|
+
} else if (key.indexOf('.') > -1) {
|
205
217
|
targetObject[targetDefaultPath + key] = srcObject[key];
|
206
|
-
}
|
207
|
-
else {
|
218
|
+
} else {
|
208
219
|
targetObject[targetDefaultPath][key] = srcObject[key];
|
209
220
|
}
|
210
221
|
}
|
211
222
|
}
|
212
223
|
|
213
224
|
//export fis3 plugin
|
214
|
-
export = function(content: string, file: FisFile, options: ArtOption): string {
|
215
|
-
if (!content || content.trim() ===
|
225
|
+
export = function (content: string, file: FisFile, options: ArtOption): string {
|
226
|
+
if (!content || content.trim() === '') return '';
|
216
227
|
|
217
228
|
if (!file.isHtmlLike) return content;
|
218
229
|
|
219
230
|
if (!isInited) {
|
220
|
-
readGlobalData(options.define
|
231
|
+
readGlobalData(options.define, file);
|
221
232
|
delete options.define;
|
222
233
|
|
223
234
|
initEngine(options);
|
@@ -226,22 +237,22 @@ export = function(content: string, file: FisFile, options: ArtOption): string {
|
|
226
237
|
|
227
238
|
let data = readConfig(file);
|
228
239
|
|
229
|
-
if (data[
|
240
|
+
if (data['$release'] === false) {
|
230
241
|
//如果不release,将文件丢到.deleted,并添加clean标记,在release:end后清除
|
231
242
|
needClean = true;
|
232
243
|
file.release = deletedFileName;
|
233
|
-
return
|
244
|
+
return '';
|
234
245
|
}
|
235
246
|
|
236
|
-
if (data[
|
247
|
+
if (data['$noParse'] === true) {
|
237
248
|
return content;
|
238
249
|
}
|
239
250
|
|
240
251
|
data = mergeGlobalData(file.subpath, data, Obj);
|
241
252
|
|
242
253
|
// 加入内置的file变量
|
243
|
-
data[
|
254
|
+
data['$file'] = file;
|
244
255
|
data['$media'] = fis.project.currentMedia();
|
245
256
|
|
246
|
-
return render(content
|
257
|
+
return render(content, file, data);
|
247
258
|
};
|