@dimina/compiler 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,299 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import os from "node:os";
4
+ function hasCompileInfo(modulePath, list, preList) {
5
+ const mergeList = Array.isArray(preList) ? [...preList, ...list] : list;
6
+ for (const element of mergeList) {
7
+ if (element.path === modulePath) {
8
+ return true;
9
+ }
10
+ }
11
+ return false;
12
+ }
13
+ function getAbsolutePath(workPath, pagePath, src) {
14
+ const relativePath = pagePath.split("/").filter((part) => part !== "").slice(0, -1).join("/");
15
+ return path.resolve(workPath, relativePath, src);
16
+ }
17
+ const assetsMap = {};
18
+ function collectAssets(workPath, pagePath, src, targetPath, appId) {
19
+ if (src.startsWith("http") || src.startsWith("//")) {
20
+ return src;
21
+ }
22
+ if (!/\.(?:png|jpe?g|gif|svg)(?:\?.*)?$/.test(src)) {
23
+ return src;
24
+ }
25
+ const relativePath = pagePath.split("/").slice(0, -1).join("/");
26
+ const absolutePath = src.startsWith("/") ? workPath + src : path.resolve(workPath, relativePath, src);
27
+ if (assetsMap[absolutePath]) {
28
+ return assetsMap[absolutePath];
29
+ }
30
+ try {
31
+ const ext = `.${src.split(".").pop()}`;
32
+ const dirPath = absolutePath.split("/").slice(0, -1).join("/");
33
+ const prefix = uuid();
34
+ const targetStatic = `${targetPath}/main/static`;
35
+ if (!fs.existsSync(targetStatic)) {
36
+ fs.mkdirSync(targetStatic);
37
+ }
38
+ getFilesWithExtension(dirPath, ext).forEach((file) => {
39
+ fs.copyFileSync(path.resolve(dirPath, file), `${targetStatic}/${prefix}_${file}`);
40
+ });
41
+ const filename = src.split("/").pop();
42
+ assetsMap[absolutePath] = `/${appId}/main/static/${prefix}_${filename}`;
43
+ } catch (error) {
44
+ console.log(error);
45
+ }
46
+ return assetsMap[absolutePath] || src;
47
+ }
48
+ function getFilesWithExtension(directory, extension) {
49
+ const files = fs.readdirSync(directory);
50
+ const filteredFiles = files.filter((file) => path.extname(file) === extension);
51
+ return filteredFiles;
52
+ }
53
+ function isObjectEmpty(objectName) {
54
+ if (!objectName) {
55
+ return true;
56
+ }
57
+ return Object.keys(objectName).length === 0 && objectName.constructor === Object;
58
+ }
59
+ function isString(o) {
60
+ return Object.prototype.toString.call(o) === "[object String]";
61
+ }
62
+ function transformRpx(styleText) {
63
+ if (!isString(styleText)) {
64
+ return styleText;
65
+ }
66
+ return styleText.replace(/([+-]?\d+(?:\.\d+)?)rpx/g, (_, pixel) => {
67
+ return `${Number(pixel)}rem`;
68
+ });
69
+ }
70
+ function uuid() {
71
+ return Math.random().toString(36).slice(2, 7);
72
+ }
73
+ const tagWhiteList = [
74
+ "page",
75
+ "wrapper",
76
+ "block",
77
+ "button",
78
+ "camera",
79
+ "checkbox-group",
80
+ "checkbox",
81
+ "cover-image",
82
+ "cover-view",
83
+ "form",
84
+ "icon",
85
+ "image",
86
+ "input",
87
+ "keyboard-accessory",
88
+ "label",
89
+ "map",
90
+ "movable-area",
91
+ "movable-view",
92
+ "navigation-bar",
93
+ "navigator",
94
+ "open-data",
95
+ "page-meta",
96
+ "picker-view-column",
97
+ "picker-view",
98
+ "picker",
99
+ "progress",
100
+ "radio-group",
101
+ "radio",
102
+ "rich-text",
103
+ "root-portal",
104
+ "scroll-view",
105
+ "slider",
106
+ "swiper-item",
107
+ "swiper",
108
+ "switch",
109
+ "template",
110
+ "text",
111
+ "textarea",
112
+ "video",
113
+ "view",
114
+ "web-view"
115
+ ];
116
+ let pathInfo = {};
117
+ let configInfo = {};
118
+ function storeInfo(workPath) {
119
+ storePathInfo(workPath);
120
+ storeProjectConfig();
121
+ storeAppConfig();
122
+ storePageConfig();
123
+ return {
124
+ pathInfo,
125
+ configInfo
126
+ };
127
+ }
128
+ function resetStoreInfo(opts) {
129
+ pathInfo = opts.pathInfo;
130
+ configInfo = opts.configInfo;
131
+ }
132
+ function storePathInfo(workPath) {
133
+ pathInfo.workPath = workPath;
134
+ pathInfo.targetPath = path.join(os.tmpdir(), `dimina-fe-dist-${Date.now()}`);
135
+ }
136
+ function storeProjectConfig() {
137
+ const filePath = `${pathInfo.workPath}/project.config.json`;
138
+ configInfo.projectInfo = parseContentByPath(filePath);
139
+ }
140
+ function storeAppConfig() {
141
+ const filePath = `${pathInfo.workPath}/app.json`;
142
+ const content = parseContentByPath(filePath);
143
+ const newObj = {};
144
+ for (const key in content) {
145
+ if (Object.prototype.hasOwnProperty.call(content, key)) {
146
+ if (key === "subpackages") {
147
+ newObj.subPackages = content[key];
148
+ } else {
149
+ newObj[key] = content[key];
150
+ }
151
+ }
152
+ }
153
+ configInfo.appInfo = newObj;
154
+ }
155
+ function getContentByPath(path2) {
156
+ return fs.readFileSync(path2, { encoding: "utf-8" });
157
+ }
158
+ function parseContentByPath(path2) {
159
+ return JSON.parse(getContentByPath(path2));
160
+ }
161
+ function storePageConfig() {
162
+ const { pages, subPackages } = configInfo.appInfo;
163
+ configInfo.pageInfo = {};
164
+ configInfo.componentInfo = {};
165
+ collectionPageJson(pages);
166
+ if (subPackages) {
167
+ subPackages.forEach((subPkg) => {
168
+ collectionPageJson(subPkg.pages, subPkg.root);
169
+ });
170
+ }
171
+ }
172
+ function collectionPageJson(pages, root) {
173
+ pages.forEach((pagePath) => {
174
+ let np = pagePath;
175
+ if (root) {
176
+ if (!root.endsWith("/")) {
177
+ root += "/";
178
+ }
179
+ np = root + np;
180
+ }
181
+ const pageFilePath = `${pathInfo.workPath}/${np}.json`;
182
+ if (fs.existsSync(pageFilePath)) {
183
+ const pageJsonContent = parseContentByPath(pageFilePath);
184
+ if (root) {
185
+ pageJsonContent.root = transSubDir(root);
186
+ }
187
+ configInfo.pageInfo[np] = pageJsonContent;
188
+ storeComponentConfig(pageJsonContent, pageFilePath);
189
+ }
190
+ });
191
+ }
192
+ function storeComponentConfig(pageJsonContent, pageFilePath) {
193
+ if (isObjectEmpty(pageJsonContent.usingComponents)) {
194
+ return;
195
+ }
196
+ for (const [componentName, componentPath] of Object.entries(pageJsonContent.usingComponents)) {
197
+ const moduleId = getModuleId(componentPath, pageFilePath);
198
+ if (configInfo.componentInfo[moduleId]) {
199
+ continue;
200
+ }
201
+ pageJsonContent.usingComponents[componentName] = moduleId;
202
+ const componentFilePath = path.resolve(getWorkPath(), `./${moduleId}.json`);
203
+ if (!fs.existsSync(componentFilePath)) {
204
+ continue;
205
+ }
206
+ const cContent = parseContentByPath(componentFilePath);
207
+ const cUsing = cContent.usingComponents || {};
208
+ const isComponent = cContent.component || false;
209
+ const cComponents = Object.keys(cUsing).reduce((acc, key) => {
210
+ acc[key] = getModuleId(cUsing[key], pageFilePath);
211
+ return acc;
212
+ }, {});
213
+ configInfo.componentInfo[moduleId] = {
214
+ id: uuid(),
215
+ path: moduleId,
216
+ component: isComponent,
217
+ usingComponents: cComponents
218
+ };
219
+ storeComponentConfig(configInfo.componentInfo[moduleId], pageFilePath);
220
+ }
221
+ }
222
+ function getModuleId(src, pageFilePath) {
223
+ const lastIndex = pageFilePath.lastIndexOf("/");
224
+ const newPath = pageFilePath.slice(0, lastIndex);
225
+ const workPath = getWorkPath();
226
+ const res = path.resolve(newPath, src);
227
+ return res.replace(workPath, "");
228
+ }
229
+ function getTargetPath() {
230
+ return pathInfo.targetPath;
231
+ }
232
+ function getComponent(src) {
233
+ return configInfo.componentInfo[src];
234
+ }
235
+ function getPageConfigInfo() {
236
+ return configInfo.pageInfo;
237
+ }
238
+ function getAppConfigInfo() {
239
+ return configInfo.appInfo;
240
+ }
241
+ function getWorkPath() {
242
+ return pathInfo.workPath;
243
+ }
244
+ function getAppId() {
245
+ return configInfo.projectInfo.appid;
246
+ }
247
+ function getAppName() {
248
+ if (configInfo.projectInfo.projectname) {
249
+ return decodeURIComponent(configInfo.projectInfo.projectname);
250
+ }
251
+ return getAppId();
252
+ }
253
+ function transSubDir(name) {
254
+ return `sub_${name.replace(/\/$/, "")}`;
255
+ }
256
+ function getPages() {
257
+ const { pages, subPackages = [] } = getAppConfigInfo();
258
+ const pageInfo = getPageConfigInfo();
259
+ const mainPages = pages.map((path2) => ({
260
+ id: uuid(),
261
+ path: path2,
262
+ usingComponents: pageInfo[path2]?.usingComponents || {}
263
+ }));
264
+ const subPages = {};
265
+ subPackages.forEach((subPkg) => {
266
+ const rootPath = subPkg.root.endsWith("/") ? subPkg.root : `${subPkg.root}/`;
267
+ const independent = subPkg.independent ? subPkg.independent : false;
268
+ subPages[transSubDir(rootPath)] = {
269
+ independent,
270
+ info: subPkg.pages.map((path2) => ({
271
+ id: uuid(),
272
+ path: rootPath + path2,
273
+ usingComponents: pageInfo[rootPath + path2]?.usingComponents || {}
274
+ }))
275
+ };
276
+ });
277
+ return {
278
+ mainPages,
279
+ subPages
280
+ };
281
+ }
282
+ export {
283
+ getComponent as a,
284
+ getContentByPath as b,
285
+ getAbsolutePath as c,
286
+ collectAssets as d,
287
+ getAppId as e,
288
+ getWorkPath as f,
289
+ getTargetPath as g,
290
+ tagWhiteList as h,
291
+ hasCompileInfo as i,
292
+ getAppConfigInfo as j,
293
+ getPageConfigInfo as k,
294
+ getPages as l,
295
+ getAppName as m,
296
+ resetStoreInfo as r,
297
+ storeInfo as s,
298
+ transformRpx as t
299
+ };
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+ const fs = require("node:fs");
3
+ const path = require("node:path");
4
+ const os = require("node:os");
5
+ function hasCompileInfo(modulePath, list, preList) {
6
+ const mergeList = Array.isArray(preList) ? [...preList, ...list] : list;
7
+ for (const element of mergeList) {
8
+ if (element.path === modulePath) {
9
+ return true;
10
+ }
11
+ }
12
+ return false;
13
+ }
14
+ function getAbsolutePath(workPath, pagePath, src) {
15
+ const relativePath = pagePath.split("/").filter((part) => part !== "").slice(0, -1).join("/");
16
+ return path.resolve(workPath, relativePath, src);
17
+ }
18
+ const assetsMap = {};
19
+ function collectAssets(workPath, pagePath, src, targetPath, appId) {
20
+ if (src.startsWith("http") || src.startsWith("//")) {
21
+ return src;
22
+ }
23
+ if (!/\.(?:png|jpe?g|gif|svg)(?:\?.*)?$/.test(src)) {
24
+ return src;
25
+ }
26
+ const relativePath = pagePath.split("/").slice(0, -1).join("/");
27
+ const absolutePath = src.startsWith("/") ? workPath + src : path.resolve(workPath, relativePath, src);
28
+ if (assetsMap[absolutePath]) {
29
+ return assetsMap[absolutePath];
30
+ }
31
+ try {
32
+ const ext = `.${src.split(".").pop()}`;
33
+ const dirPath = absolutePath.split("/").slice(0, -1).join("/");
34
+ const prefix = uuid();
35
+ const targetStatic = `${targetPath}/main/static`;
36
+ if (!fs.existsSync(targetStatic)) {
37
+ fs.mkdirSync(targetStatic);
38
+ }
39
+ getFilesWithExtension(dirPath, ext).forEach((file) => {
40
+ fs.copyFileSync(path.resolve(dirPath, file), `${targetStatic}/${prefix}_${file}`);
41
+ });
42
+ const filename = src.split("/").pop();
43
+ assetsMap[absolutePath] = `/${appId}/main/static/${prefix}_${filename}`;
44
+ } catch (error) {
45
+ console.log(error);
46
+ }
47
+ return assetsMap[absolutePath] || src;
48
+ }
49
+ function getFilesWithExtension(directory, extension) {
50
+ const files = fs.readdirSync(directory);
51
+ const filteredFiles = files.filter((file) => path.extname(file) === extension);
52
+ return filteredFiles;
53
+ }
54
+ function isObjectEmpty(objectName) {
55
+ if (!objectName) {
56
+ return true;
57
+ }
58
+ return Object.keys(objectName).length === 0 && objectName.constructor === Object;
59
+ }
60
+ function isString(o) {
61
+ return Object.prototype.toString.call(o) === "[object String]";
62
+ }
63
+ function transformRpx(styleText) {
64
+ if (!isString(styleText)) {
65
+ return styleText;
66
+ }
67
+ return styleText.replace(/([+-]?\d+(?:\.\d+)?)rpx/g, (_, pixel) => {
68
+ return `${Number(pixel)}rem`;
69
+ });
70
+ }
71
+ function uuid() {
72
+ return Math.random().toString(36).slice(2, 7);
73
+ }
74
+ const tagWhiteList = [
75
+ "page",
76
+ "wrapper",
77
+ "block",
78
+ "button",
79
+ "camera",
80
+ "checkbox-group",
81
+ "checkbox",
82
+ "cover-image",
83
+ "cover-view",
84
+ "form",
85
+ "icon",
86
+ "image",
87
+ "input",
88
+ "keyboard-accessory",
89
+ "label",
90
+ "map",
91
+ "movable-area",
92
+ "movable-view",
93
+ "navigation-bar",
94
+ "navigator",
95
+ "open-data",
96
+ "page-meta",
97
+ "picker-view-column",
98
+ "picker-view",
99
+ "picker",
100
+ "progress",
101
+ "radio-group",
102
+ "radio",
103
+ "rich-text",
104
+ "root-portal",
105
+ "scroll-view",
106
+ "slider",
107
+ "swiper-item",
108
+ "swiper",
109
+ "switch",
110
+ "template",
111
+ "text",
112
+ "textarea",
113
+ "video",
114
+ "view",
115
+ "web-view"
116
+ ];
117
+ let pathInfo = {};
118
+ let configInfo = {};
119
+ function storeInfo(workPath) {
120
+ storePathInfo(workPath);
121
+ storeProjectConfig();
122
+ storeAppConfig();
123
+ storePageConfig();
124
+ return {
125
+ pathInfo,
126
+ configInfo
127
+ };
128
+ }
129
+ function resetStoreInfo(opts) {
130
+ pathInfo = opts.pathInfo;
131
+ configInfo = opts.configInfo;
132
+ }
133
+ function storePathInfo(workPath) {
134
+ pathInfo.workPath = workPath;
135
+ pathInfo.targetPath = path.join(os.tmpdir(), `dimina-fe-dist-${Date.now()}`);
136
+ }
137
+ function storeProjectConfig() {
138
+ const filePath = `${pathInfo.workPath}/project.config.json`;
139
+ configInfo.projectInfo = parseContentByPath(filePath);
140
+ }
141
+ function storeAppConfig() {
142
+ const filePath = `${pathInfo.workPath}/app.json`;
143
+ const content = parseContentByPath(filePath);
144
+ const newObj = {};
145
+ for (const key in content) {
146
+ if (Object.prototype.hasOwnProperty.call(content, key)) {
147
+ if (key === "subpackages") {
148
+ newObj.subPackages = content[key];
149
+ } else {
150
+ newObj[key] = content[key];
151
+ }
152
+ }
153
+ }
154
+ configInfo.appInfo = newObj;
155
+ }
156
+ function getContentByPath(path2) {
157
+ return fs.readFileSync(path2, { encoding: "utf-8" });
158
+ }
159
+ function parseContentByPath(path2) {
160
+ return JSON.parse(getContentByPath(path2));
161
+ }
162
+ function storePageConfig() {
163
+ const { pages, subPackages } = configInfo.appInfo;
164
+ configInfo.pageInfo = {};
165
+ configInfo.componentInfo = {};
166
+ collectionPageJson(pages);
167
+ if (subPackages) {
168
+ subPackages.forEach((subPkg) => {
169
+ collectionPageJson(subPkg.pages, subPkg.root);
170
+ });
171
+ }
172
+ }
173
+ function collectionPageJson(pages, root) {
174
+ pages.forEach((pagePath) => {
175
+ let np = pagePath;
176
+ if (root) {
177
+ if (!root.endsWith("/")) {
178
+ root += "/";
179
+ }
180
+ np = root + np;
181
+ }
182
+ const pageFilePath = `${pathInfo.workPath}/${np}.json`;
183
+ if (fs.existsSync(pageFilePath)) {
184
+ const pageJsonContent = parseContentByPath(pageFilePath);
185
+ if (root) {
186
+ pageJsonContent.root = transSubDir(root);
187
+ }
188
+ configInfo.pageInfo[np] = pageJsonContent;
189
+ storeComponentConfig(pageJsonContent, pageFilePath);
190
+ }
191
+ });
192
+ }
193
+ function storeComponentConfig(pageJsonContent, pageFilePath) {
194
+ if (isObjectEmpty(pageJsonContent.usingComponents)) {
195
+ return;
196
+ }
197
+ for (const [componentName, componentPath] of Object.entries(pageJsonContent.usingComponents)) {
198
+ const moduleId = getModuleId(componentPath, pageFilePath);
199
+ if (configInfo.componentInfo[moduleId]) {
200
+ continue;
201
+ }
202
+ pageJsonContent.usingComponents[componentName] = moduleId;
203
+ const componentFilePath = path.resolve(getWorkPath(), `./${moduleId}.json`);
204
+ if (!fs.existsSync(componentFilePath)) {
205
+ continue;
206
+ }
207
+ const cContent = parseContentByPath(componentFilePath);
208
+ const cUsing = cContent.usingComponents || {};
209
+ const isComponent = cContent.component || false;
210
+ const cComponents = Object.keys(cUsing).reduce((acc, key) => {
211
+ acc[key] = getModuleId(cUsing[key], pageFilePath);
212
+ return acc;
213
+ }, {});
214
+ configInfo.componentInfo[moduleId] = {
215
+ id: uuid(),
216
+ path: moduleId,
217
+ component: isComponent,
218
+ usingComponents: cComponents
219
+ };
220
+ storeComponentConfig(configInfo.componentInfo[moduleId], pageFilePath);
221
+ }
222
+ }
223
+ function getModuleId(src, pageFilePath) {
224
+ const lastIndex = pageFilePath.lastIndexOf("/");
225
+ const newPath = pageFilePath.slice(0, lastIndex);
226
+ const workPath = getWorkPath();
227
+ const res = path.resolve(newPath, src);
228
+ return res.replace(workPath, "");
229
+ }
230
+ function getTargetPath() {
231
+ return pathInfo.targetPath;
232
+ }
233
+ function getComponent(src) {
234
+ return configInfo.componentInfo[src];
235
+ }
236
+ function getPageConfigInfo() {
237
+ return configInfo.pageInfo;
238
+ }
239
+ function getAppConfigInfo() {
240
+ return configInfo.appInfo;
241
+ }
242
+ function getWorkPath() {
243
+ return pathInfo.workPath;
244
+ }
245
+ function getAppId() {
246
+ return configInfo.projectInfo.appid;
247
+ }
248
+ function getAppName() {
249
+ if (configInfo.projectInfo.projectname) {
250
+ return decodeURIComponent(configInfo.projectInfo.projectname);
251
+ }
252
+ return getAppId();
253
+ }
254
+ function transSubDir(name) {
255
+ return `sub_${name.replace(/\/$/, "")}`;
256
+ }
257
+ function getPages() {
258
+ const { pages, subPackages = [] } = getAppConfigInfo();
259
+ const pageInfo = getPageConfigInfo();
260
+ const mainPages = pages.map((path2) => ({
261
+ id: uuid(),
262
+ path: path2,
263
+ usingComponents: pageInfo[path2]?.usingComponents || {}
264
+ }));
265
+ const subPages = {};
266
+ subPackages.forEach((subPkg) => {
267
+ const rootPath = subPkg.root.endsWith("/") ? subPkg.root : `${subPkg.root}/`;
268
+ const independent = subPkg.independent ? subPkg.independent : false;
269
+ subPages[transSubDir(rootPath)] = {
270
+ independent,
271
+ info: subPkg.pages.map((path2) => ({
272
+ id: uuid(),
273
+ path: rootPath + path2,
274
+ usingComponents: pageInfo[rootPath + path2]?.usingComponents || {}
275
+ }))
276
+ };
277
+ });
278
+ return {
279
+ mainPages,
280
+ subPages
281
+ };
282
+ }
283
+ exports.collectAssets = collectAssets;
284
+ exports.getAbsolutePath = getAbsolutePath;
285
+ exports.getAppConfigInfo = getAppConfigInfo;
286
+ exports.getAppId = getAppId;
287
+ exports.getAppName = getAppName;
288
+ exports.getComponent = getComponent;
289
+ exports.getContentByPath = getContentByPath;
290
+ exports.getPageConfigInfo = getPageConfigInfo;
291
+ exports.getPages = getPages;
292
+ exports.getTargetPath = getTargetPath;
293
+ exports.getWorkPath = getWorkPath;
294
+ exports.hasCompileInfo = hasCompileInfo;
295
+ exports.resetStoreInfo = resetStoreInfo;
296
+ exports.storeInfo = storeInfo;
297
+ exports.tagWhiteList = tagWhiteList;
298
+ exports.transformRpx = transformRpx;