generated from NewFuture/miniprogram-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtpl.js
85 lines (81 loc) · 2.96 KB
/
dtpl.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const path = require("path");
const SRC = "src";
/**
* Convert Windows backslash paths to slash paths
* @param {string} input
* @returns {string}
*/
function slash(input) {
const isExtendedLengthPath = /^\\\\\?\\/.test(input);
const hasNonAscii = /[^\u0000-\u0080]+/.test(input); // eslint-disable-line no-control-regex
if (isExtendedLengthPath || hasNonAscii) {
return input;
}
return input.replace(/\\/g, "/");
}
module.exports = function(source) {
const data = source.basicData;
const currentFolder = slash(path.relative(SRC, path.relative(data.rootPath, data.dirPath)));
return {
templates: [
{
// 当在 pages 目录下新建一个文件夹时,向这个文件夹内注入 .dtpl/page 下的文件
matches: function() {
return source.isDirectory && currentFolder.startsWith("page");
},
name: "./page/",
inject: function() {
const rawModuleName = data.rawModuleName;
const page = [currentFolder, rawModuleName, rawModuleName].join("/");
// 向 app.json 中注入内容
const appJson = path.resolve(data.rootPath, SRC, "app.jsonc");
const projectJson = path.resolve(data.rootPath, SRC, "project.config.jsonc");
return [
{
file: appJson,
data: { page: '"' + page + '",' },
tags: "loose",
append: true,
eol: require("os").EOL,
},
{
file: projectJson,
data: {
page: `{
"id": ${Math.round((Date.now() % 100000000000) / 1000)},
"name": "${rawModuleName}",
"pathName": "${page}",
"query": "",
"scene": null,
},`,
},
tags: "loose",
append: true,
eol: require("os").EOL,
},
];
},
},
{
// 当在 components 目录下新建一个文件夹时,向这个文件夹内注入 .dtpl/component 下的文件
matches: function() {
return (
source.isDirectory &&
(currentFolder.toLowerCase() === "component" ||
currentFolder.toLowerCase() === "components")
);
},
name: "./component/",
},
{
name: "./wxts.dtpl", // wxts模板
matches: "src/**/*.wxts",
},
],
globalData: {
dollar: "$",
style: "scss",
folder: currentFolder,
},
};
};