UNPKG

7.76 kBJavaScriptView Raw
1"use strict";
2var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6};
7var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8 if (kind === "m") throw new TypeError("Private method is not writable");
9 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12};
13var _AppHelper_appConfig, _AppHelper_pages, _AppHelper_pagesConfigList, _AppHelper_comps, _AppHelper_compsConfigList;
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.AppHelper = void 0;
16const helper_1 = require("@tarojs/helper");
17const lodash_1 = require("lodash");
18const path = require("path");
19class AppHelper {
20 constructor(entry = {}, options = {}) {
21 _AppHelper_appConfig.set(this, void 0);
22 _AppHelper_pages.set(this, void 0);
23 _AppHelper_pagesConfigList.set(this, void 0);
24 _AppHelper_comps.set(this, void 0);
25 _AppHelper_compsConfigList.set(this, void 0);
26 this.options = (0, lodash_1.defaults)(options || {}, {
27 sourceDir: '',
28 entryFileName: 'app',
29 frameworkExts: helper_1.SCRIPT_EXT
30 });
31 this.entry = entry;
32 }
33 get appEntry() {
34 // Note: 不考虑 `() => Promise` 情况
35 const { entryFileName, sourceDir } = this.options;
36 const appEntry = this.entry[entryFileName];
37 if (!appEntry)
38 return path.join(sourceDir, entryFileName);
39 if (Array.isArray(appEntry)) {
40 return appEntry.filter(item => path.basename(item, path.extname(item)) === entryFileName)[0];
41 }
42 else if (Array.isArray(appEntry.import)) {
43 return appEntry.import.filter(item => path.basename(item, path.extname(item)) === entryFileName)[0];
44 }
45 return appEntry;
46 }
47 get appConfig() {
48 if (!__classPrivateFieldGet(this, _AppHelper_appConfig, "f")) {
49 const appConfigPath = this.getConfigFilePath(this.appEntry);
50 const appConfig = (0, helper_1.readConfig)(appConfigPath);
51 if ((0, helper_1.isEmptyObject)(appConfig)) {
52 throw new Error('缺少 app 全局配置,请检查!');
53 }
54 __classPrivateFieldSet(this, _AppHelper_appConfig, appConfig, "f");
55 }
56 return __classPrivateFieldGet(this, _AppHelper_appConfig, "f");
57 }
58 get pages() {
59 if (!__classPrivateFieldGet(this, _AppHelper_pages, "f")) {
60 const appPages = this.appConfig.pages;
61 if (!appPages || !appPages.length) {
62 throw new Error('全局配置缺少 pages 字段,请检查!');
63 }
64 const { frameworkExts, sourceDir } = this.options;
65 __classPrivateFieldSet(this, _AppHelper_pages, new Set([
66 ...appPages.map(item => ({
67 name: item,
68 path: (0, helper_1.resolveMainFilePath)(path.join(sourceDir, item), frameworkExts)
69 }))
70 ]), "f");
71 this.getSubPackages();
72 }
73 return __classPrivateFieldGet(this, _AppHelper_pages, "f");
74 }
75 get comps() {
76 if (!__classPrivateFieldGet(this, _AppHelper_comps, "f")) {
77 const appPages = this.appConfig.components;
78 if (!appPages || !appPages.length) {
79 throw new Error('全局配置缺少 components 字段,请检查!');
80 }
81 const { frameworkExts, sourceDir } = this.options;
82 __classPrivateFieldSet(this, _AppHelper_comps, new Set([
83 ...appPages.map(item => ({
84 name: item,
85 path: (0, helper_1.resolveMainFilePath)(path.join(sourceDir, item), frameworkExts)
86 }))
87 ]), "f");
88 }
89 return __classPrivateFieldGet(this, _AppHelper_comps, "f");
90 }
91 getSubPackages() {
92 const subPackages = this.appConfig.subPackages || this.appConfig.subpackages;
93 const { frameworkExts, sourceDir } = this.options;
94 if (subPackages && subPackages.length) {
95 subPackages.forEach(item => {
96 if (item.pages && item.pages.length) {
97 const root = item.root;
98 item.pages.forEach(page => {
99 var _a;
100 let pageItem = `${root}/${page}`;
101 pageItem = pageItem.replace(/\/{2,}/g, '/');
102 let hasPageIn = false;
103 this.pages.forEach(({ name }) => {
104 if (name === pageItem) {
105 hasPageIn = true;
106 }
107 });
108 if (!hasPageIn) {
109 const pagePath = (0, helper_1.resolveMainFilePath)(path.join(sourceDir, pageItem), frameworkExts);
110 this.pages.add({
111 name: pageItem,
112 path: pagePath
113 });
114 (_a = this.appConfig.pages) === null || _a === void 0 ? void 0 : _a.push(pageItem);
115 }
116 });
117 }
118 });
119 }
120 }
121 get pagesConfigList() {
122 if (!__classPrivateFieldGet(this, _AppHelper_pagesConfigList, "f")) {
123 const list = new Map();
124 const pages = this.pages;
125 pages.forEach(({ name, path }) => {
126 const pageConfigPath = this.getConfigFilePath(path);
127 list.set(name, pageConfigPath);
128 });
129 __classPrivateFieldSet(this, _AppHelper_pagesConfigList, list, "f");
130 }
131 return __classPrivateFieldGet(this, _AppHelper_pagesConfigList, "f");
132 }
133 get compsConfigList() {
134 if (!__classPrivateFieldGet(this, _AppHelper_compsConfigList, "f")) {
135 const list = new Map();
136 const comps = this.comps;
137 comps.forEach(({ name, path }) => {
138 const pageConfigPath = this.getConfigFilePath(path);
139 list.set(name, pageConfigPath);
140 });
141 __classPrivateFieldSet(this, _AppHelper_compsConfigList, list, "f");
142 }
143 return __classPrivateFieldGet(this, _AppHelper_compsConfigList, "f");
144 }
145 getConfigFilePath(filePath = '') {
146 // console.log('filePath: ', filePath)
147 return (0, helper_1.resolveMainFilePath)(`${filePath.replace(path.extname(filePath), '')}.config`);
148 }
149 clear() {
150 __classPrivateFieldSet(this, _AppHelper_appConfig, undefined, "f");
151 __classPrivateFieldSet(this, _AppHelper_pages, undefined, "f");
152 __classPrivateFieldSet(this, _AppHelper_pagesConfigList, undefined, "f");
153 }
154}
155exports.AppHelper = AppHelper;
156_AppHelper_appConfig = new WeakMap(), _AppHelper_pages = new WeakMap(), _AppHelper_pagesConfigList = new WeakMap(), _AppHelper_comps = new WeakMap(), _AppHelper_compsConfigList = new WeakMap();
157//# sourceMappingURL=app.js.map
\No newline at end of file