@eighty4/dank 0.0.4-1 → 0.0.4-2

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.
package/lib_js/config.js CHANGED
@@ -1,23 +1,163 @@
1
- var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
- if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
- return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
- return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
- });
6
- }
7
- return path;
1
+ import { isAbsolute, resolve } from "node:path";
2
+ import { isProductionBuild } from "./flags.js";
3
+ var __rewriteRelativeImportExtension = function(path, preserveJsx) {
4
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
5
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function(m, tsx, d, ext, cm) {
6
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : d + ext + "." + cm.toLowerCase() + "js";
7
+ });
8
+ }
9
+ return path;
8
10
  };
9
- import { isAbsolute, resolve } from 'node:path';
10
- const CFG_P = './dank.config.ts';
11
- export async function loadConfig(path = CFG_P) {
12
- const modulePath = `${resolveConfigPath(path)}?${Date.now()}`;
13
- const module = await import(__rewriteRelativeImportExtension(modulePath));
14
- return await module.default;
11
+ const CFG_P = "./dank.config.ts";
12
+ async function loadConfig(mode, path = CFG_P) {
13
+ const modulePath = resolveConfigPath(path);
14
+ const c = await resolveConfig(mode, modulePath);
15
+ normalizePagePaths(c.pages);
16
+ return c;
17
+ }
18
+ function resolveConfigPath(path) {
19
+ if (isAbsolute(path)) {
20
+ return path;
21
+ } else {
22
+ return resolve(process.cwd(), path);
23
+ }
24
+ }
25
+ async function resolveConfig(mode, modulePath) {
26
+ const module = await import(__rewriteRelativeImportExtension(`${modulePath}?${Date.now()}`));
27
+ const c = typeof module.default === "function" ? await module.default(resolveDankDetails(mode)) : module.default;
28
+ validateDankConfig(c);
29
+ return c;
30
+ }
31
+ function resolveDankDetails(mode) {
32
+ const production = isProductionBuild();
33
+ return {
34
+ dev: !production,
35
+ production,
36
+ mode
37
+ };
38
+ }
39
+ function validateDankConfig(c) {
40
+ try {
41
+ validatePorts(c);
42
+ validatePages(c.pages);
43
+ validateDevServices(c.services);
44
+ validateEsbuildConfig(c.esbuild);
45
+ } catch (e) {
46
+ throw e;
47
+ }
48
+ }
49
+ function validatePorts(c) {
50
+ if (c.port !== null && typeof c.port !== "undefined") {
51
+ if (typeof c.port !== "number") {
52
+ throw Error("DankConfig.port must be a number");
53
+ }
54
+ }
55
+ if (c.previewPort !== null && typeof c.previewPort !== "undefined") {
56
+ if (typeof c.previewPort !== "number") {
57
+ throw Error("DankConfig.previewPort must be a number");
58
+ }
59
+ }
60
+ }
61
+ function validateEsbuildConfig(esbuild) {
62
+ if (esbuild?.loaders !== null && typeof esbuild?.loaders !== "undefined") {
63
+ if (typeof esbuild.loaders !== "object") {
64
+ throw Error("DankConfig.esbuild.loaders must be a map of extensions to esbuild loaders");
65
+ } else {
66
+ for (const [ext, loader] of Object.entries(esbuild.loaders)) {
67
+ if (typeof loader !== "string") {
68
+ throw Error(`DankConfig.esbuild.loaders['${ext}'] must be a string of a loader name`);
69
+ }
70
+ }
71
+ }
72
+ }
73
+ if (esbuild?.plugins !== null && typeof esbuild?.plugins !== "undefined") {
74
+ if (!Array.isArray(esbuild.plugins)) {
75
+ throw Error("DankConfig.esbuild.plugins must be an array of esbuild plugins");
76
+ }
77
+ }
78
+ if (esbuild?.port !== null && typeof esbuild?.port !== "undefined") {
79
+ if (typeof esbuild.port !== "number") {
80
+ throw Error("DankConfig.esbuild.port must be a number");
81
+ }
82
+ }
83
+ }
84
+ function validatePages(pages) {
85
+ if (pages === null || typeof pages === "undefined" || Object.keys(pages).length === 0) {
86
+ throw Error("DankConfig.pages is required");
87
+ }
88
+ for (const [urlPath, mapping] of Object.entries(pages)) {
89
+ if (typeof mapping === "string" && mapping.endsWith(".html")) {
90
+ continue;
91
+ }
92
+ if (typeof mapping === "object") {
93
+ validatePageMapping(urlPath, mapping);
94
+ continue;
95
+ }
96
+ throw Error(`DankConfig.pages['${urlPath}'] must configure an html file`);
97
+ }
98
+ }
99
+ function validatePageMapping(urlPath, mapping) {
100
+ if (mapping.webpage === null || typeof mapping.webpage !== "string" || !mapping.webpage.endsWith(".html")) {
101
+ throw Error(`DankConfig.pages['${urlPath}'].webpage must configure an html file`);
102
+ }
103
+ if (mapping.pattern === null || typeof mapping.pattern === "undefined") {
104
+ return;
105
+ }
106
+ if (typeof mapping.pattern === "object" && mapping.pattern.constructor.name === "RegExp") {
107
+ return;
108
+ }
109
+ throw Error(`DankConfig.pages['${urlPath}'].pattern must be a RegExp`);
15
110
  }
16
- export function resolveConfigPath(path) {
17
- if (isAbsolute(path)) {
18
- return path;
111
+ function validateDevServices(services) {
112
+ if (services === null || typeof services === "undefined") {
113
+ return;
114
+ }
115
+ if (!Array.isArray(services)) {
116
+ throw Error(`DankConfig.services must be an array`);
117
+ }
118
+ for (let i = 0; i < services.length; i++) {
119
+ const s = services[i];
120
+ if (s.command === null || typeof s.command === "undefined") {
121
+ throw Error(`DankConfig.services[${i}].command is required`);
122
+ } else if (typeof s.command !== "string" || s.command.length === 0) {
123
+ throw Error(`DankConfig.services[${i}].command must be a non-empty string`);
19
124
  }
20
- else {
21
- return resolve(process.cwd(), path);
125
+ if (s.cwd !== null && typeof s.cwd !== "undefined") {
126
+ if (typeof s.cwd !== "string" || s.cwd.trim().length === 0) {
127
+ throw Error(`DankConfig.services[${i}].cwd must be a non-empty string`);
128
+ }
22
129
  }
130
+ if (s.env !== null && typeof s.env !== "undefined") {
131
+ if (typeof s.env !== "object") {
132
+ throw Error(`DankConfig.services[${i}].env must be an env variable map`);
133
+ }
134
+ for (const [k, v] of Object.entries(s.env)) {
135
+ if (typeof v !== "string") {
136
+ throw Error(`DankConfig.services[${i}].env[${k}] must be a string`);
137
+ }
138
+ }
139
+ }
140
+ if (s.http !== null && typeof s.http !== "undefined") {
141
+ if (typeof s.http.port !== "number") {
142
+ throw Error(`DankConfig.services[${i}].http.port must be a number`);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ function normalizePagePaths(pages) {
148
+ for (const [pageUrl, mapping] of Object.entries(pages)) {
149
+ if (typeof mapping === "string") {
150
+ pages[pageUrl] = normalizePagePath(mapping);
151
+ } else {
152
+ mapping.webpage = normalizePagePath(mapping.webpage);
153
+ }
154
+ }
155
+ }
156
+ function normalizePagePath(p) {
157
+ return p.replace(/^\.\//, "");
23
158
  }
159
+ export {
160
+ loadConfig,
161
+ resolveConfig,
162
+ resolveConfigPath
163
+ };
package/lib_js/dank.js CHANGED
@@ -1,123 +1,6 @@
1
- export async function defineConfig(c) {
2
- if (c.port !== null && typeof c.port !== 'undefined') {
3
- if (typeof c.port !== 'number') {
4
- throw Error('DankConfig.port must be a number');
5
- }
6
- }
7
- if (c.previewPort !== null && typeof c.previewPort !== 'undefined') {
8
- if (typeof c.previewPort !== 'number') {
9
- throw Error('DankConfig.previewPort must be a number');
10
- }
11
- }
12
- validatePages(c.pages);
13
- validateDevServices(c.services);
14
- validateEsbuildConfig(c.esbuild);
15
- normalizePagePaths(c.pages);
16
- return c;
17
- }
18
- function validateEsbuildConfig(esbuild) {
19
- if (esbuild?.loaders !== null && typeof esbuild?.loaders !== 'undefined') {
20
- if (typeof esbuild.loaders !== 'object') {
21
- throw Error('DankConfig.esbuild.loaders must be a map of extensions to esbuild loaders');
22
- }
23
- else {
24
- for (const [ext, loader] of Object.entries(esbuild.loaders)) {
25
- if (typeof loader !== 'string') {
26
- throw Error(`DankConfig.esbuild.loaders['${ext}'] must be a string of a loader name`);
27
- }
28
- }
29
- }
30
- }
31
- if (esbuild?.plugins !== null && typeof esbuild?.plugins !== 'undefined') {
32
- if (!Array.isArray(esbuild.plugins)) {
33
- throw Error('DankConfig.esbuild.plugins must be an array of esbuild plugins');
34
- }
35
- }
36
- if (esbuild?.port !== null && typeof esbuild?.port !== 'undefined') {
37
- if (typeof esbuild.port !== 'number') {
38
- throw Error('DankConfig.esbuild.port must be a number');
39
- }
40
- }
41
- }
42
- function validatePages(pages) {
43
- if (pages === null ||
44
- typeof pages === 'undefined' ||
45
- Object.keys(pages).length === 0) {
46
- throw Error('DankConfig.pages is required');
47
- }
48
- for (const [urlPath, mapping] of Object.entries(pages)) {
49
- if (typeof mapping === 'string' && mapping.endsWith('.html')) {
50
- continue;
51
- }
52
- if (typeof mapping === 'object') {
53
- validatePageMapping(urlPath, mapping);
54
- continue;
55
- }
56
- throw Error(`DankConfig.pages['${urlPath}'] must configure an html file`);
57
- }
58
- }
59
- function validatePageMapping(urlPath, mapping) {
60
- if (mapping.webpage === null ||
61
- typeof mapping.webpage !== 'string' ||
62
- !mapping.webpage.endsWith('.html')) {
63
- throw Error(`DankConfig.pages['${urlPath}'].webpage must configure an html file`);
64
- }
65
- if (mapping.pattern === null || typeof mapping.pattern === 'undefined') {
66
- return;
67
- }
68
- if (typeof mapping.pattern === 'object' &&
69
- mapping.pattern.constructor.name === 'RegExp') {
70
- return;
71
- }
72
- throw Error(`DankConfig.pages['${urlPath}'].pattern must be a RegExp`);
73
- }
74
- function validateDevServices(services) {
75
- if (services === null || typeof services === 'undefined') {
76
- return;
77
- }
78
- if (!Array.isArray(services)) {
79
- throw Error(`DankConfig.services must be an array`);
80
- }
81
- for (let i = 0; i < services.length; i++) {
82
- const s = services[i];
83
- if (s.command === null || typeof s.command === 'undefined') {
84
- throw Error(`DankConfig.services[${i}].command is required`);
85
- }
86
- else if (typeof s.command !== 'string' || s.command.length === 0) {
87
- throw Error(`DankConfig.services[${i}].command must be a non-empty string`);
88
- }
89
- if (s.cwd !== null && typeof s.cwd !== 'undefined') {
90
- if (typeof s.cwd !== 'string' || s.cwd.trim().length === 0) {
91
- throw Error(`DankConfig.services[${i}].cwd must be a non-empty string`);
92
- }
93
- }
94
- if (s.env !== null && typeof s.env !== 'undefined') {
95
- if (typeof s.env !== 'object') {
96
- throw Error(`DankConfig.services[${i}].env must be an env variable map`);
97
- }
98
- for (const [k, v] of Object.entries(s.env)) {
99
- if (typeof v !== 'string') {
100
- throw Error(`DankConfig.services[${i}].env[${k}] must be a string`);
101
- }
102
- }
103
- }
104
- if (s.http !== null && typeof s.http !== 'undefined') {
105
- if (typeof s.http.port !== 'number') {
106
- throw Error(`DankConfig.services[${i}].http.port must be a number`);
107
- }
108
- }
109
- }
110
- }
111
- function normalizePagePaths(pages) {
112
- for (const [pageUrl, mapping] of Object.entries(pages)) {
113
- if (typeof mapping === 'string') {
114
- pages[pageUrl] = normalizePagePath(mapping);
115
- }
116
- else {
117
- mapping.webpage = normalizePagePath(mapping.webpage);
118
- }
119
- }
120
- }
121
- function normalizePagePath(p) {
122
- return p.replace(/^\.\//, '');
1
+ function defineConfig(config) {
2
+ return config;
123
3
  }
4
+ export {
5
+ defineConfig
6
+ };
package/lib_js/define.js CHANGED
@@ -1,6 +1,9 @@
1
- export function createGlobalDefinitions(build) {
2
- return {
3
- 'dank.IS_DEV': JSON.stringify(!build.production),
4
- 'dank.IS_PROD': JSON.stringify(build.production),
5
- };
1
+ function createGlobalDefinitions(build) {
2
+ return {
3
+ "dank.IS_DEV": JSON.stringify(!build.production),
4
+ "dank.IS_PROD": JSON.stringify(build.production)
5
+ };
6
6
  }
7
+ export {
8
+ createGlobalDefinitions
9
+ };