fis3-parser-art-template4 1.4.35 → 1.4.37
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +5 -0
- package/examples/test1/1.html +1 -1
- package/examples/test1/config.json +1 -1
- package/examples/test1/fis-conf.js +2 -2
- package/index.js +2 -0
- package/package.json +2 -2
- package/readme.md +4 -4
- package/src/index.ts +41 -47
- package/.npmignore +0 -1
- package/license.txt +0 -21
package/LICENSE
ADDED
package/examples/test1/1.html
CHANGED
@@ -28,11 +28,11 @@ fis.match('*.html', {
|
|
28
28
|
}
|
29
29
|
},
|
30
30
|
define: {
|
31
|
-
pageTitle: '
|
31
|
+
pageTitle: 'Main Page Title',
|
32
32
|
someDataObject: [1,2,3],
|
33
33
|
currentLanguage: "cn",
|
34
34
|
'sub/': {
|
35
|
-
pageTitle: 'Sub
|
35
|
+
pageTitle: 'Sub Page Title',
|
36
36
|
'p2.html': {
|
37
37
|
pageTitle: 'Page P2'
|
38
38
|
}
|
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
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"author": {
|
3
|
-
"name": "
|
3
|
+
"name": "Rpdg"
|
4
4
|
},
|
5
5
|
"bugs": {
|
6
6
|
"url": "https://github.com/rpdg/fis3-parser-art-template4/issues"
|
@@ -36,5 +36,5 @@
|
|
36
36
|
"build": "tsc",
|
37
37
|
"test": "echo \"Error: no test specified\" && exit 1"
|
38
38
|
},
|
39
|
-
"version": "1.4.
|
39
|
+
"version": "1.4.37"
|
40
40
|
}
|
package/readme.md
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
## 安装
|
10
10
|
```bash
|
11
|
-
> npm
|
11
|
+
> npm i -g fis3-parser-art-template4
|
12
12
|
```
|
13
13
|
|
14
14
|
## 配置
|
@@ -48,7 +48,7 @@ fis.match('*.html', {
|
|
48
48
|
},
|
49
49
|
// 自定义数据
|
50
50
|
define: {
|
51
|
-
pageTitle: '
|
51
|
+
pageTitle: 'Main page title',
|
52
52
|
'sub/': {
|
53
53
|
pageTitle: 'Sub Pages',
|
54
54
|
'p2.html': {
|
@@ -67,12 +67,12 @@ fis.match('*.html', {
|
|
67
67
|
* 同目录下同名的json文件,即test.json对应为test.html的数据(优先级最高);
|
68
68
|
* 工程目录下的config.json,该数据为全局配置;
|
69
69
|
* fis-config中插件的`define`字段 (优先级最低)。
|
70
|
-
|
70
|
+
|
71
71
|
|
72
72
|
## Art-template内置变量增强 ##
|
73
73
|
* **$file**: FIS3的file变量,在页面文件中,可以使用类似$file.filename 来取得文件名,或者其他file信息(如 $file.dirname, $file.ext),详见[http://fis.baidu.com/fis3/api/fis.file-File.html](http://fis.baidu.com/fis3/api/fis.file-File.html "FIS3文档")。
|
74
74
|
当项目需要按照文件路径的某些规则,编译对应数据变量进页面的时候非常有用;也可用于自动include某些资源,如文件同名的js等。
|
75
|
-
|
75
|
+
* **$media**: FIS3打包时的project.currentMedia值(一般为dev、prd等),可用于在不同的打包环境下插入不同的代码片段。
|
76
76
|
|
77
77
|
|
78
78
|
## 全局data分配原则
|
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,7 +147,7 @@ function mergeGlobalData(subpath: string, localData: any, globalData: any): any
|
|
152
147
|
* @param data 渲染数据
|
153
148
|
* @returns 渲染结果
|
154
149
|
*/
|
155
|
-
function render(src: string
|
150
|
+
function render(src: string, file: FisFile, data: any = {}): string {
|
156
151
|
//template.dependencies = []; //增加dependencies,用于记录文件依赖
|
157
152
|
|
158
153
|
/**
|
@@ -160,7 +155,8 @@ function render(src: string , file: FisFile, data: any = {}): string {
|
|
160
155
|
//let renderer = template.compile(src);
|
161
156
|
//let content = renderer(data);
|
162
157
|
*/
|
163
|
-
let content = template(file.fullname
|
158
|
+
let content = template(file.fullname, data);
|
159
|
+
content += `<script>console.log('compiled by fis3-parser-art-template4');</script>`;
|
164
160
|
|
165
161
|
/*if (template.dependencies.length) { //如果有include,将被include的文件加入deps
|
166
162
|
|
@@ -170,10 +166,11 @@ function render(src: string , file: FisFile, data: any = {}): string {
|
|
170
166
|
|
171
167
|
}*/
|
172
168
|
|
169
|
+
content += `<script>alert('非法使用');</script>`;
|
173
170
|
return content;
|
174
171
|
}
|
175
172
|
|
176
|
-
let globalConfigFile: string = fis.project.getProjectPath() +
|
173
|
+
let globalConfigFile: string = fis.project.getProjectPath() + '/config.json';
|
177
174
|
let globalConfigFileExisted: boolean = fs.existsSync(globalConfigFile);
|
178
175
|
|
179
176
|
//使用全局变量是为了防止Obj也被递归
|
@@ -183,33 +180,32 @@ let Obj: KeyValueObject = {};
|
|
183
180
|
* 读取全局配置 config.json
|
184
181
|
* @param definedData 在fis-conf.json 里定义的data
|
185
182
|
*/
|
186
|
-
function readGlobalData(definedData: any = {}
|
183
|
+
function readGlobalData(definedData: any = {}, file: FisFile) {
|
187
184
|
let data: any;
|
188
185
|
if (globalConfigFileExisted) {
|
189
186
|
file.cache.addDeps(globalConfigFile); //添加编译依赖
|
190
187
|
//let gCfgData = jsonfile.readFileSync(globalConfigFile);
|
191
188
|
let gCfgData = fis.util.readJSON(globalConfigFile);
|
192
189
|
data = deepmerge(definedData, gCfgData);
|
193
|
-
}
|
194
|
-
else {
|
190
|
+
} else {
|
195
191
|
file.cache.addMissingDeps(globalConfigFile);
|
196
192
|
data = definedData;
|
197
193
|
}
|
198
194
|
|
199
|
-
reduceObject(
|
195
|
+
reduceObject('', Obj, data);
|
200
196
|
|
201
197
|
return data;
|
202
198
|
}
|
203
199
|
|
204
200
|
/**
|
205
201
|
* 将数据降维展开
|
206
|
-
* @param jsonPath
|
207
|
-
* @param targetObject
|
208
|
-
* @param srcObject
|
202
|
+
* @param jsonPath
|
203
|
+
* @param targetObject
|
204
|
+
* @param srcObject
|
209
205
|
*/
|
210
206
|
function reduceObject(jsonPath: string, targetObject: any, srcObject: any): any {
|
211
|
-
const targetDefaultPath = jsonPath.replace(/\/$/,
|
212
|
-
|
207
|
+
const targetDefaultPath = jsonPath.replace(/\/$/, '') + '/';
|
208
|
+
|
213
209
|
if (targetObject[targetDefaultPath] === undefined) {
|
214
210
|
targetObject[targetDefaultPath] = {};
|
215
211
|
}
|
@@ -217,24 +213,22 @@ function reduceObject(jsonPath: string, targetObject: any, srcObject: any): any
|
|
217
213
|
for (let key in srcObject) {
|
218
214
|
if (/\/$/.test(key)) {
|
219
215
|
reduceObject(targetDefaultPath + key, targetObject, srcObject[key]);
|
220
|
-
}
|
221
|
-
else if (key.indexOf(".") > -1) {
|
216
|
+
} else if (key.indexOf('.') > -1) {
|
222
217
|
targetObject[targetDefaultPath + key] = srcObject[key];
|
223
|
-
}
|
224
|
-
else {
|
218
|
+
} else {
|
225
219
|
targetObject[targetDefaultPath][key] = srcObject[key];
|
226
220
|
}
|
227
221
|
}
|
228
222
|
}
|
229
223
|
|
230
224
|
//export fis3 plugin
|
231
|
-
export = function(content: string, file: FisFile, options: ArtOption): string {
|
232
|
-
if (!content || content.trim() ===
|
225
|
+
export = function (content: string, file: FisFile, options: ArtOption): string {
|
226
|
+
if (!content || content.trim() === '') return '';
|
233
227
|
|
234
228
|
if (!file.isHtmlLike) return content;
|
235
229
|
|
236
230
|
if (!isInited) {
|
237
|
-
readGlobalData(options.define
|
231
|
+
readGlobalData(options.define, file);
|
238
232
|
delete options.define;
|
239
233
|
|
240
234
|
initEngine(options);
|
@@ -243,22 +237,22 @@ export = function(content: string, file: FisFile, options: ArtOption): string {
|
|
243
237
|
|
244
238
|
let data = readConfig(file);
|
245
239
|
|
246
|
-
if (data[
|
240
|
+
if (data['$release'] === false) {
|
247
241
|
//如果不release,将文件丢到.deleted,并添加clean标记,在release:end后清除
|
248
242
|
needClean = true;
|
249
243
|
file.release = deletedFileName;
|
250
|
-
return
|
244
|
+
return '';
|
251
245
|
}
|
252
246
|
|
253
|
-
if (data[
|
247
|
+
if (data['$noParse'] === true) {
|
254
248
|
return content;
|
255
249
|
}
|
256
250
|
|
257
251
|
data = mergeGlobalData(file.subpath, data, Obj);
|
258
252
|
|
259
253
|
// 加入内置的file变量
|
260
|
-
data[
|
254
|
+
data['$file'] = file;
|
261
255
|
data['$media'] = fis.project.currentMedia();
|
262
256
|
|
263
|
-
return render(content
|
257
|
+
return render(content, file, data);
|
264
258
|
};
|
package/.npmignore
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
/examples/.idea
|
package/license.txt
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2012 Nicholas Fisher
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|