-
Notifications
You must be signed in to change notification settings - Fork 68
/
dtpl.js.dtpl
38 lines (34 loc) · 1.44 KB
/
dtpl.js.dtpl
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
var path = require('path');
module.exports = function (source) {
return {
templates: [
{
// 当在 pages 目录下新建一个文件夹时,向这个文件夹内注入 .dtpl/page 下的文件
matches: function () { return source.isDirectory && /^pages?$/.test(source.basicData.dirName); },
name: './page',
inject: function () {
let {rawModuleName, dirName, dirPath} = source.basicData
let page = [dirName, rawModuleName, rawModuleName].join('/')
// 向 app.json 和 base/MyApp.ts 中注入内容
let appJson = path.resolve(dirPath, '..', 'app.cjson')
let MyAppTs = path.resolve(dirPath, '..', 'base', 'MyApp.ts')
return [
{ file: appJson, data: { page: "\"" + page + "\"," }, tags: 'loose', append: true },
{ file: MyAppTs, data: { pagesMap: camelCase(rawModuleName) + ": Url" }, tags: 'loose', append: true },
]
}
},
{
// 当在 components 目录下新建一个文件夹时,向这个文件夹内注入 .dtpl/component 下的文件
matches: function () { return source.isDirectory && /^components?$/.test(source.basicData.dirName); },
name: './component/'
}
],
globalData: {
dollar: '$',
style: '${style}'
}
}
}
function camelCase(str) { return str.replace(/[-_](\w)/g, camelCaseReplacer) }
function camelCaseReplacer(r, k) { return k.toUpperCase() }