swpp-backends 3.0.0-alpha.1 → 3.0.0-alpha.100
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/dist/index.d.ts +19 -0
- package/dist/index.js +16 -11
- package/{types → dist}/swpp/FileParser.d.ts +11 -6
- package/dist/swpp/FileParser.js +27 -13
- package/dist/swpp/JsonBuilder.js +2 -2
- package/dist/swpp/NetworkFileHandler.js +3 -3
- package/{types → dist}/swpp/ResourcesScanner.d.ts +11 -5
- package/dist/swpp/ResourcesScanner.js +95 -30
- package/{types → dist}/swpp/SwCompiler.d.ts +12 -8
- package/dist/swpp/SwCompiler.js +16 -6
- package/{types/swpp/config/ConfigLoader.d.ts → dist/swpp/config/ConfigCluster.d.ts} +39 -30
- package/dist/swpp/config/ConfigCluster.js +86 -0
- package/dist/swpp/config/ConfigLoader.d.ts +56 -0
- package/dist/swpp/config/ConfigLoader.js +59 -102
- package/{types → dist}/swpp/database/CompilationEnv.d.ts +19 -6
- package/dist/swpp/database/CompilationEnv.js +74 -40
- package/{types → dist}/swpp/database/CrossDepCode.d.ts +7 -0
- package/dist/swpp/database/CrossDepCode.js +16 -0
- package/dist/swpp/database/DomCode.d.ts +32 -0
- package/dist/swpp/database/DomCode.js +114 -0
- package/{types → dist}/swpp/database/KeyValueDatabase.d.ts +21 -13
- package/dist/swpp/database/KeyValueDatabase.js +50 -17
- package/{types → dist}/swpp/database/RuntimeCoreCode.d.ts +5 -3
- package/dist/swpp/database/RuntimeCoreCode.js +6 -6
- package/{types → dist}/swpp/database/RuntimeDepCode.d.ts +1 -5
- package/dist/swpp/database/RuntimeDepCode.js +0 -11
- package/{types → dist}/swpp/database/RuntimeEventCode.d.ts +1 -1
- package/dist/swpp/database/RuntimeEventCode.js +20 -4
- package/{types → dist}/swpp/untils.d.ts +24 -11
- package/dist/swpp/untils.js +53 -30
- package/package.json +2 -2
- package/dist/resources/sw-dom.js +0 -45
- package/dist/resources/sw-fetch.js +0 -76
- package/types/DomBuilder.d.ts +0 -6
- package/types/FileAnalyzer.d.ts +0 -96
- package/types/ServiceWorkerBuilder.d.ts +0 -7
- package/types/SwppConfig.d.ts +0 -139
- package/types/SwppRules.d.ts +0 -117
- package/types/UpdateJsonBuilder.d.ts +0 -44
- package/types/Utils.d.ts +0 -43
- package/types/Variant.d.ts +0 -58
- package/types/VersionAnalyzer.d.ts +0 -27
- package/types/browser/ServiceWorkerRuntimeTypes.d.ts +0 -18
- package/types/index.d.ts +0 -31
- package/types/swpp/RuntimeEnv.d.ts +0 -39
- package/types/swpp/SwCodeInject.d.ts +0 -17
- package/types/swpp/database/RuntimeEnv.d.ts +0 -5
- /package/{types → dist}/swpp/JsonBuilder.d.ts +0 -0
- /package/{types → dist}/swpp/NetworkFileHandler.d.ts +0 -0
- /package/{types → dist}/swpp/database/CrossEnv.d.ts +0 -0
- /package/{types → dist}/swpp/database/RuntimeKeyValueDatabase.d.ts +0 -0
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
import { COMMON_TYPE_COMP_ENV } from '../database/CompilationEnv';
|
|
2
2
|
import { COMMON_TYPE_CROSS_DEP } from '../database/CrossDepCode';
|
|
3
3
|
import { COMMON_TYPE_CROSS_ENV } from '../database/CrossEnv';
|
|
4
|
+
import { COMMON_TYPE_DOM_CODE } from '../database/DomCode';
|
|
4
5
|
import { COMMON_TYPE_RUNTIME_CORE } from '../database/RuntimeCoreCode';
|
|
5
6
|
import { COMMON_KEY_RUNTIME_DEP, FunctionInBrowser } from '../database/RuntimeDepCode';
|
|
6
7
|
import { COMMON_TYPE_RUNTIME_EVENT } from '../database/RuntimeEventCode';
|
|
7
|
-
import { CompilationData
|
|
8
|
-
|
|
9
|
-
/** 支持的拓展名列表 */
|
|
10
|
-
private static readonly extensions;
|
|
11
|
-
private static jiti;
|
|
12
|
-
private config;
|
|
13
|
-
private isBuilt;
|
|
14
|
-
/**
|
|
15
|
-
* 加载一个配置文件,越早加载的优先级越高
|
|
16
|
-
* @param file
|
|
17
|
-
*/
|
|
18
|
-
load(file: string): Promise<void>;
|
|
19
|
-
/** 将配置项的内容写入到环境中 */
|
|
20
|
-
write(runtime: RuntimeData, compilation: CompilationData): void;
|
|
21
|
-
/** 将新配置合并到已有配置中 */
|
|
22
|
-
private mergeConfig;
|
|
23
|
-
}
|
|
8
|
+
import { CompilationData } from '../SwCompiler';
|
|
9
|
+
import { IndivisibleName, SwppConfigModifier } from './ConfigLoader';
|
|
24
10
|
/** 定义一个通过 `export default` 导出的配置 */
|
|
25
11
|
export declare function defineConfig(config: SwppConfigTemplate): SwppConfigTemplate;
|
|
26
12
|
/** 定义一个通过 `export const compilationEnv` 导出的配置 */
|
|
@@ -33,8 +19,13 @@ export declare function defineRuntimeDep(config: SwppConfigRuntimeDep): SwppConf
|
|
|
33
19
|
export declare function defineCrossDep(config: SwppConfigCrossDep): SwppConfigCrossDep;
|
|
34
20
|
/** 定义一个通过 `export const runtimeCore` 导出的配置 */
|
|
35
21
|
export declare function defineRuntimeCore(config: SwppConfigRuntimeCore): SwppConfigRuntimeCore;
|
|
22
|
+
/** 定义一个通过 `export const domConfig` 导出的配置 */
|
|
23
|
+
export declare function defineDomConfig(config: SwppConfigDomConfig): SwppConfigDomConfig;
|
|
36
24
|
/** 定义一个通过 `export const runtimeEvent` 导出的配置 */
|
|
37
25
|
export declare function defineRuntimeEvent(config: SwppConfigRuntimeEvent): SwppConfigRuntimeEvent;
|
|
26
|
+
export type IndivisibleConfig<T> = {
|
|
27
|
+
[K in typeof IndivisibleName]: true;
|
|
28
|
+
} & T;
|
|
38
29
|
/**
|
|
39
30
|
* 定义一个无法分割的对象配置,这对一些强依赖对象内部属性的设置很有用,可以避免对象被错误地拼接。
|
|
40
31
|
*
|
|
@@ -68,7 +59,7 @@ export declare function defineRuntimeEvent(config: SwppConfigRuntimeEvent): Swpp
|
|
|
68
59
|
* }
|
|
69
60
|
* ```
|
|
70
61
|
*/
|
|
71
|
-
export declare function defineIndivisibleConfig<T extends
|
|
62
|
+
export declare function defineIndivisibleConfig<T extends object>(value: T): IndivisibleConfig<T>;
|
|
72
63
|
type ValueOrReturnValue<T> = T | ((this: CompilationData) => T);
|
|
73
64
|
/**
|
|
74
65
|
* SWPP 配置模板
|
|
@@ -77,17 +68,23 @@ type ValueOrReturnValue<T> = T | ((this: CompilationData) => T);
|
|
|
77
68
|
*/
|
|
78
69
|
export interface SwppConfigTemplate {
|
|
79
70
|
/** @see {SwppConfigCompilationEnv} */
|
|
80
|
-
compilationEnv
|
|
71
|
+
compilationEnv?: SwppConfigCompilationEnv;
|
|
81
72
|
/** @see {SwppConfigCrossEnv} */
|
|
82
|
-
crossEnv
|
|
73
|
+
crossEnv?: SwppConfigCrossEnv;
|
|
83
74
|
/** @see {SwppConfigRuntimeDep} */
|
|
84
|
-
runtimeDep
|
|
75
|
+
runtimeDep?: SwppConfigRuntimeDep;
|
|
85
76
|
/** @see {SwppConfigCrossDep} */
|
|
86
|
-
crossDep
|
|
77
|
+
crossDep?: SwppConfigCrossDep;
|
|
87
78
|
/** @see {SwppConfigRuntimeCore} */
|
|
88
|
-
runtimeCore
|
|
79
|
+
runtimeCore?: SwppConfigRuntimeCore;
|
|
89
80
|
/** @see {SwppConfigRuntimeEvent} */
|
|
90
|
-
runtimeEvent
|
|
81
|
+
runtimeEvent?: SwppConfigRuntimeEvent;
|
|
82
|
+
/** @see {SwppConfigDomConfig} */
|
|
83
|
+
domConfig?: SwppConfigDomConfig;
|
|
84
|
+
/** 配置编辑器 */
|
|
85
|
+
modifier?: SwppConfigModifier;
|
|
86
|
+
}
|
|
87
|
+
export interface SwppConfigInterface {
|
|
91
88
|
}
|
|
92
89
|
/**
|
|
93
90
|
* 运行时函数依赖。
|
|
@@ -121,7 +118,7 @@ export interface SwppConfigTemplate {
|
|
|
121
118
|
*/
|
|
122
119
|
export type SwppConfigRuntimeDep = {
|
|
123
120
|
[K in keyof COMMON_KEY_RUNTIME_DEP | string]?: K extends keyof COMMON_KEY_RUNTIME_DEP ? COMMON_KEY_RUNTIME_DEP[K]['default'] : FunctionInBrowser<any[], any>;
|
|
124
|
-
};
|
|
121
|
+
} & SwppConfigInterface;
|
|
125
122
|
/**
|
|
126
123
|
* 运行时核心功能.
|
|
127
124
|
*
|
|
@@ -132,7 +129,7 @@ export type SwppConfigRuntimeDep = {
|
|
|
132
129
|
*/
|
|
133
130
|
export type SwppConfigRuntimeCore = {
|
|
134
131
|
[K in keyof COMMON_TYPE_RUNTIME_CORE | string]?: K extends keyof COMMON_TYPE_RUNTIME_CORE ? COMMON_TYPE_RUNTIME_CORE[K]['default'] : FunctionInBrowser<any[], any>;
|
|
135
|
-
};
|
|
132
|
+
} & SwppConfigInterface;
|
|
136
133
|
/**
|
|
137
134
|
* 运行时 & 编译期的函数依赖。
|
|
138
135
|
*
|
|
@@ -168,7 +165,7 @@ export type SwppConfigRuntimeCore = {
|
|
|
168
165
|
*/
|
|
169
166
|
export type SwppConfigCrossDep = {
|
|
170
167
|
[K in keyof COMMON_TYPE_CROSS_DEP | string]?: K extends keyof COMMON_TYPE_CROSS_DEP ? COMMON_TYPE_CROSS_DEP[K]['default'] : any;
|
|
171
|
-
};
|
|
168
|
+
} & SwppConfigInterface;
|
|
172
169
|
/**
|
|
173
170
|
* 运行时事件注册。
|
|
174
171
|
*
|
|
@@ -196,7 +193,7 @@ export type SwppConfigCrossDep = {
|
|
|
196
193
|
*/
|
|
197
194
|
export type SwppConfigRuntimeEvent = {
|
|
198
195
|
[K in keyof COMMON_TYPE_RUNTIME_EVENT | string]?: K extends keyof COMMON_TYPE_RUNTIME_EVENT ? COMMON_TYPE_RUNTIME_EVENT[K]['default'] : FunctionInBrowser<[Event], any>;
|
|
199
|
-
};
|
|
196
|
+
} & SwppConfigInterface;
|
|
200
197
|
/**
|
|
201
198
|
* 运行时 & 编译期环境变量。
|
|
202
199
|
*
|
|
@@ -226,7 +223,7 @@ export type SwppConfigRuntimeEvent = {
|
|
|
226
223
|
*/
|
|
227
224
|
export type SwppConfigCrossEnv = {
|
|
228
225
|
[K in keyof COMMON_TYPE_CROSS_ENV | string]: ValueOrReturnValue<K extends keyof COMMON_TYPE_CROSS_ENV ? COMMON_TYPE_CROSS_ENV[K]['default'] : any>;
|
|
229
|
-
};
|
|
226
|
+
} & SwppConfigInterface;
|
|
230
227
|
/**
|
|
231
228
|
* 构建期使用的环境变量。
|
|
232
229
|
*
|
|
@@ -240,5 +237,17 @@ export type SwppConfigCrossEnv = {
|
|
|
240
237
|
*/
|
|
241
238
|
export type SwppConfigCompilationEnv = {
|
|
242
239
|
[K in keyof COMMON_TYPE_COMP_ENV | string]?: K extends keyof COMMON_TYPE_COMP_ENV ? COMMON_TYPE_COMP_ENV[K]['default'] : any;
|
|
243
|
-
};
|
|
240
|
+
} & SwppConfigInterface;
|
|
241
|
+
/**
|
|
242
|
+
* 运行时使用的常量、函数。
|
|
243
|
+
*
|
|
244
|
+
* 该配置项用于放置需要生成到 dom.js 中的内容。
|
|
245
|
+
*
|
|
246
|
+
* 对于每一项配置 `<KEY>: <value>`:<KEY> 是常量名或函数名,常量推荐大写下划线命名,函数推荐小写驼峰命名,<value> 是值。
|
|
247
|
+
*
|
|
248
|
+
* 该配置项中的值只能使用本配置项中包含的内容,不能使用其它编译期、运行期的内容。
|
|
249
|
+
*/
|
|
250
|
+
export type SwppConfigDomConfig = {
|
|
251
|
+
[K in keyof COMMON_TYPE_DOM_CODE | string]?: K extends keyof COMMON_TYPE_DOM_CODE ? COMMON_TYPE_DOM_CODE[K]['default'] : any;
|
|
252
|
+
} & SwppConfigInterface;
|
|
244
253
|
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineConfig = defineConfig;
|
|
4
|
+
exports.defineCompilationEnv = defineCompilationEnv;
|
|
5
|
+
exports.defineCrossEnv = defineCrossEnv;
|
|
6
|
+
exports.defineRuntimeDep = defineRuntimeDep;
|
|
7
|
+
exports.defineCrossDep = defineCrossDep;
|
|
8
|
+
exports.defineRuntimeCore = defineRuntimeCore;
|
|
9
|
+
exports.defineDomConfig = defineDomConfig;
|
|
10
|
+
exports.defineRuntimeEvent = defineRuntimeEvent;
|
|
11
|
+
exports.defineIndivisibleConfig = defineIndivisibleConfig;
|
|
12
|
+
const ConfigLoader_1 = require("./ConfigLoader");
|
|
13
|
+
/** 定义一个通过 `export default` 导出的配置 */
|
|
14
|
+
function defineConfig(config) {
|
|
15
|
+
return config;
|
|
16
|
+
}
|
|
17
|
+
/** 定义一个通过 `export const compilationEnv` 导出的配置 */
|
|
18
|
+
function defineCompilationEnv(config) {
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
/** 定义一个通过 `export const crossEnv` 导出的配置 */
|
|
22
|
+
function defineCrossEnv(config) {
|
|
23
|
+
return config;
|
|
24
|
+
}
|
|
25
|
+
/** 定义一个通过 `export const runtimeDep` 导出的配置 */
|
|
26
|
+
function defineRuntimeDep(config) {
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
/** 定义一个通过 `export const crossDep` 导出的配置 */
|
|
30
|
+
function defineCrossDep(config) {
|
|
31
|
+
return config;
|
|
32
|
+
}
|
|
33
|
+
/** 定义一个通过 `export const runtimeCore` 导出的配置 */
|
|
34
|
+
function defineRuntimeCore(config) {
|
|
35
|
+
return config;
|
|
36
|
+
}
|
|
37
|
+
/** 定义一个通过 `export const domConfig` 导出的配置 */
|
|
38
|
+
function defineDomConfig(config) {
|
|
39
|
+
return config;
|
|
40
|
+
}
|
|
41
|
+
/** 定义一个通过 `export const runtimeEvent` 导出的配置 */
|
|
42
|
+
function defineRuntimeEvent(config) {
|
|
43
|
+
return config;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 定义一个无法分割的对象配置,这对一些强依赖对象内部属性的设置很有用,可以避免对象被错误地拼接。
|
|
47
|
+
*
|
|
48
|
+
* 默认情况下,当定义一个对象配置时,将允许从其它配置文件中合并一部分配置到对象中,比如:
|
|
49
|
+
*
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // 当前配置
|
|
52
|
+
* exampleConfig.obj = {
|
|
53
|
+
* value1: 'hello world'
|
|
54
|
+
* }
|
|
55
|
+
* // 如果还有一个配置文件中也声明了这个配置
|
|
56
|
+
* exampleConfig.obj = {
|
|
57
|
+
* value2: 'hello swpp'
|
|
58
|
+
* }
|
|
59
|
+
* // 最终将合并生成如下配置
|
|
60
|
+
* exampleConfig.obj = {
|
|
61
|
+
* value1: 'hello world',
|
|
62
|
+
* value2: 'hello swpp'
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* 通过该函数,可以禁止 swpp 合并配置时仅选取对象的部分字段,要么全部使用 [value] 的值,要么完全不使用 [value] 的值。
|
|
67
|
+
*
|
|
68
|
+
* 放入到上述例子中,假如两个 obj 任意一个或多个通过 `defineIndivisibleConfig({ xxx: xxx })` 设置,最终的值将取决于两个配置文件的优先级,
|
|
69
|
+
* 若 `value2` 的优先级高将产生:
|
|
70
|
+
*
|
|
71
|
+
* ```typescript
|
|
72
|
+
* // 最终结果
|
|
73
|
+
* exampleConfig.obj = {
|
|
74
|
+
* value2: 'hello swpp'
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function defineIndivisibleConfig(value) {
|
|
79
|
+
Object.defineProperty(value, ConfigLoader_1.IndivisibleName, {
|
|
80
|
+
value: true,
|
|
81
|
+
writable: false,
|
|
82
|
+
configurable: false,
|
|
83
|
+
enumerable: false
|
|
84
|
+
});
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { CompilationData, RuntimeData } from '../SwCompiler';
|
|
2
|
+
import { IndivisibleConfig, SwppConfigTemplate } from './ConfigCluster';
|
|
3
|
+
export declare const IndivisibleName = "1indivisible__";
|
|
4
|
+
export declare class ConfigLoader {
|
|
5
|
+
/** 支持的拓展名列表 */
|
|
6
|
+
private static readonly extensions;
|
|
7
|
+
private static jiti;
|
|
8
|
+
private config;
|
|
9
|
+
private modifierList;
|
|
10
|
+
/**
|
|
11
|
+
* 加载一个配置文件,越早加载的优先级越高
|
|
12
|
+
* @param file
|
|
13
|
+
*/
|
|
14
|
+
load(file: string): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* 加载一个在代码层面编写的配置
|
|
17
|
+
*/
|
|
18
|
+
loadFromCode(config: SwppConfigTemplate): void;
|
|
19
|
+
/** 将配置项的内容写入到环境中 */
|
|
20
|
+
generate(): Readonly<{
|
|
21
|
+
runtime: RuntimeData;
|
|
22
|
+
compilation: CompilationData;
|
|
23
|
+
}>;
|
|
24
|
+
/** 将新配置合并到已有配置中 */
|
|
25
|
+
private static mergeConfig;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 配置编辑器
|
|
29
|
+
*/
|
|
30
|
+
export interface SwppConfigModifier {
|
|
31
|
+
/**
|
|
32
|
+
* 自定义运行时和编译期的属性表
|
|
33
|
+
*
|
|
34
|
+
* 优先级越高越优先生效
|
|
35
|
+
*/
|
|
36
|
+
build?: () => {
|
|
37
|
+
runtime: IndivisibleConfig<RuntimeData>;
|
|
38
|
+
compilation: IndivisibleConfig<CompilationData>;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* 本函数用于向系统注册新的属性。
|
|
42
|
+
*
|
|
43
|
+
* 该函数内应当只调用 xxx.append 函数及其它工具函数,非必要不应当包含其它有副作用的操作。
|
|
44
|
+
*
|
|
45
|
+
* 优先级越低该函数越早执行。
|
|
46
|
+
*/
|
|
47
|
+
registry?: (runtime: RuntimeData, compilation: CompilationData) => void;
|
|
48
|
+
/**
|
|
49
|
+
* 本函数用于动态修改属性的值。
|
|
50
|
+
*
|
|
51
|
+
* 该函数内应当只调用 xxx.update 函数及其它工具函数,非必要不应当包含其它有副作用的操作。
|
|
52
|
+
*
|
|
53
|
+
* 优先级越低该函数越早执行
|
|
54
|
+
*/
|
|
55
|
+
dynamic?: (runtime: RuntimeData, compilation: CompilationData) => void;
|
|
56
|
+
}
|
|
@@ -3,22 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ConfigLoader = void 0;
|
|
7
|
-
exports.defineConfig = defineConfig;
|
|
8
|
-
exports.defineCompilationEnv = defineCompilationEnv;
|
|
9
|
-
exports.defineCrossEnv = defineCrossEnv;
|
|
10
|
-
exports.defineRuntimeDep = defineRuntimeDep;
|
|
11
|
-
exports.defineCrossDep = defineCrossDep;
|
|
12
|
-
exports.defineRuntimeCore = defineRuntimeCore;
|
|
13
|
-
exports.defineRuntimeEvent = defineRuntimeEvent;
|
|
14
|
-
exports.defineIndivisibleConfig = defineIndivisibleConfig;
|
|
6
|
+
exports.ConfigLoader = exports.IndivisibleName = void 0;
|
|
15
7
|
const jiti_1 = require("jiti");
|
|
16
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const SwCompiler_1 = require("../SwCompiler");
|
|
17
10
|
const untils_1 = require("../untils");
|
|
18
|
-
|
|
11
|
+
exports.IndivisibleName = '1indivisible__';
|
|
19
12
|
class ConfigLoader {
|
|
20
13
|
constructor() {
|
|
21
|
-
this.
|
|
14
|
+
this.modifierList = [];
|
|
22
15
|
}
|
|
23
16
|
// noinspection JSUnusedGlobalSymbols
|
|
24
17
|
/**
|
|
@@ -26,13 +19,7 @@ class ConfigLoader {
|
|
|
26
19
|
* @param file
|
|
27
20
|
*/
|
|
28
21
|
async load(file) {
|
|
29
|
-
|
|
30
|
-
throw {
|
|
31
|
-
code: untils_1.exceptionNames.configBuilt,
|
|
32
|
-
message: '配置文件已经完成构建,不能继续加载新的配置',
|
|
33
|
-
file
|
|
34
|
-
};
|
|
35
|
-
const extensionName = path_1.default.extname(file);
|
|
22
|
+
const extensionName = path_1.default.extname(file).substring(1);
|
|
36
23
|
if (!ConfigLoader.extensions.includes(extensionName)) {
|
|
37
24
|
throw {
|
|
38
25
|
code: untils_1.exceptionNames.unsupportedFileType,
|
|
@@ -43,30 +30,50 @@ class ConfigLoader {
|
|
|
43
30
|
// @ts-ignore
|
|
44
31
|
const content = await ConfigLoader.jiti.import(file);
|
|
45
32
|
let newConfig = 'default' in content ? content.default : content;
|
|
33
|
+
this.loadFromCode(newConfig);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 加载一个在代码层面编写的配置
|
|
37
|
+
*/
|
|
38
|
+
loadFromCode(config) {
|
|
39
|
+
if ('modifier' in config) {
|
|
40
|
+
this.modifierList.push(config.modifier);
|
|
41
|
+
}
|
|
46
42
|
if (this.config)
|
|
47
|
-
|
|
43
|
+
ConfigLoader.mergeConfig(this.config, config);
|
|
48
44
|
else
|
|
49
|
-
this.config =
|
|
45
|
+
this.config = config;
|
|
50
46
|
}
|
|
51
47
|
// noinspection JSUnusedGlobalSymbols
|
|
52
48
|
/** 将配置项的内容写入到环境中 */
|
|
53
|
-
|
|
49
|
+
generate() {
|
|
54
50
|
if (!this.config)
|
|
55
51
|
throw {
|
|
56
52
|
code: untils_1.exceptionNames.nullPoint,
|
|
57
53
|
message: '构建配之前必须至少加载一个配置文件'
|
|
58
54
|
};
|
|
55
|
+
// 构建属性集
|
|
56
|
+
const { runtime, compilation } = (this.modifierList.find(it => it.build)?.build ?? (() => {
|
|
57
|
+
const compilation = new SwCompiler_1.CompilationData();
|
|
58
|
+
const runtime = new SwCompiler_1.RuntimeData(compilation);
|
|
59
|
+
return { runtime, compilation };
|
|
60
|
+
}))();
|
|
59
61
|
const config = this.config;
|
|
60
62
|
// 写入运行时信息
|
|
61
63
|
const writeRuntime = () => {
|
|
62
|
-
const insertList = ['runtimeDep', 'runtimeCore', 'runtimeEvent'];
|
|
64
|
+
const insertList = ['runtimeDep', 'runtimeCore', 'runtimeEvent', 'domConfig'];
|
|
63
65
|
for (let str of insertList) {
|
|
64
66
|
const configValue = config[str];
|
|
67
|
+
const database = runtime[str];
|
|
65
68
|
if (!configValue)
|
|
66
69
|
continue;
|
|
67
70
|
for (let key in configValue) {
|
|
68
71
|
const value = configValue[key];
|
|
69
|
-
|
|
72
|
+
if (typeof value == 'object') {
|
|
73
|
+
const def = database.readDefault(key);
|
|
74
|
+
ConfigLoader.mergeConfig(value, def, false);
|
|
75
|
+
}
|
|
76
|
+
database.update(key, () => value ?? null);
|
|
70
77
|
}
|
|
71
78
|
}
|
|
72
79
|
};
|
|
@@ -79,6 +86,10 @@ class ConfigLoader {
|
|
|
79
86
|
};
|
|
80
87
|
for (let key in config.compilationEnv) {
|
|
81
88
|
const value = config.compilationEnv[key];
|
|
89
|
+
if (typeof value == 'object') {
|
|
90
|
+
const def = compilation.compilationEnv.readDefault(key);
|
|
91
|
+
ConfigLoader.mergeConfig(value, def, false);
|
|
92
|
+
}
|
|
82
93
|
compilation.compilationEnv.update(key, () => value);
|
|
83
94
|
}
|
|
84
95
|
};
|
|
@@ -93,6 +104,10 @@ class ConfigLoader {
|
|
|
93
104
|
code: untils_1.exceptionNames.invalidVarType,
|
|
94
105
|
message: `crossEnv[${key}] 应当返回一个非函数对象,却返回了:${value.toString()}`
|
|
95
106
|
};
|
|
107
|
+
if (typeof value == 'object') {
|
|
108
|
+
const def = runtime.crossEnv.readDefault(key);
|
|
109
|
+
ConfigLoader.mergeConfig(value, def, false);
|
|
110
|
+
}
|
|
96
111
|
runtime.crossEnv.update(key, () => value);
|
|
97
112
|
}
|
|
98
113
|
}
|
|
@@ -114,18 +129,33 @@ class ConfigLoader {
|
|
|
114
129
|
code: untils_1.exceptionNames.invalidVarType,
|
|
115
130
|
message: `crossDep[${key}] 返回的对象应当包含 {runOnBrowser} 字段,却返回了:${JSON.stringify(value, null, 2)}`
|
|
116
131
|
};
|
|
132
|
+
const def = runtime.crossDep.readDefault(key);
|
|
133
|
+
ConfigLoader.mergeConfig(value, def, false);
|
|
117
134
|
runtime.crossDep.update(key, () => value);
|
|
118
135
|
}
|
|
119
136
|
}
|
|
120
137
|
};
|
|
138
|
+
// 运行 registry
|
|
139
|
+
for (let i = this.modifierList.length - 1; i >= 0; i--) {
|
|
140
|
+
const modifier = this.modifierList[i];
|
|
141
|
+
modifier.registry?.(runtime, compilation);
|
|
142
|
+
}
|
|
121
143
|
writeRuntime();
|
|
122
144
|
writeCompilation();
|
|
123
145
|
writeCross();
|
|
146
|
+
// 运行 dynamic
|
|
147
|
+
for (let i = this.modifierList.length - 1; i >= 0; i--) {
|
|
148
|
+
const modifier = this.modifierList[i];
|
|
149
|
+
modifier.dynamic?.(runtime, compilation);
|
|
150
|
+
}
|
|
151
|
+
return Object.freeze({ runtime, compilation });
|
|
124
152
|
}
|
|
125
153
|
/** 将新配置合并到已有配置中 */
|
|
126
|
-
mergeConfig(other) {
|
|
127
|
-
function mergeHelper(high, low) {
|
|
154
|
+
static mergeConfig(config, other, isTop = true) {
|
|
155
|
+
function mergeHelper(high, low, skip) {
|
|
128
156
|
for (let key in low) {
|
|
157
|
+
if (skip && key == 'modifier')
|
|
158
|
+
continue;
|
|
129
159
|
const lowValue = low[key];
|
|
130
160
|
if (key in high) {
|
|
131
161
|
const highValue = high[key];
|
|
@@ -135,8 +165,8 @@ class ConfigLoader {
|
|
|
135
165
|
}
|
|
136
166
|
if (typeof highValue != typeof lowValue)
|
|
137
167
|
continue;
|
|
138
|
-
if (typeof highValue == 'object' && !highValue[IndivisibleName] && !lowValue[IndivisibleName]) {
|
|
139
|
-
mergeHelper(highValue, lowValue);
|
|
168
|
+
if (typeof highValue == 'object' && !highValue[exports.IndivisibleName] && !lowValue[exports.IndivisibleName]) {
|
|
169
|
+
mergeHelper(highValue, lowValue, false);
|
|
140
170
|
}
|
|
141
171
|
}
|
|
142
172
|
else {
|
|
@@ -144,7 +174,7 @@ class ConfigLoader {
|
|
|
144
174
|
}
|
|
145
175
|
}
|
|
146
176
|
}
|
|
147
|
-
mergeHelper(
|
|
177
|
+
mergeHelper(config, other, isTop);
|
|
148
178
|
}
|
|
149
179
|
}
|
|
150
180
|
exports.ConfigLoader = ConfigLoader;
|
|
@@ -153,78 +183,5 @@ ConfigLoader.extensions = [
|
|
|
153
183
|
'js', 'ts', 'cjs', 'cts', 'mjs', 'mjs'
|
|
154
184
|
];
|
|
155
185
|
ConfigLoader.jiti = (0, jiti_1.createJiti)(__filename, {
|
|
156
|
-
fsCache: false
|
|
157
|
-
alias: {
|
|
158
|
-
'swpp-backends': path_1.default.join(__dirname, '..', '..', 'index')
|
|
159
|
-
}
|
|
186
|
+
fsCache: false
|
|
160
187
|
});
|
|
161
|
-
/** 定义一个通过 `export default` 导出的配置 */
|
|
162
|
-
function defineConfig(config) {
|
|
163
|
-
return config;
|
|
164
|
-
}
|
|
165
|
-
/** 定义一个通过 `export const compilationEnv` 导出的配置 */
|
|
166
|
-
function defineCompilationEnv(config) {
|
|
167
|
-
return config;
|
|
168
|
-
}
|
|
169
|
-
/** 定义一个通过 `export const crossEnv` 导出的配置 */
|
|
170
|
-
function defineCrossEnv(config) {
|
|
171
|
-
return config;
|
|
172
|
-
}
|
|
173
|
-
/** 定义一个通过 `export const runtimeDep` 导出的配置 */
|
|
174
|
-
function defineRuntimeDep(config) {
|
|
175
|
-
return config;
|
|
176
|
-
}
|
|
177
|
-
/** 定义一个通过 `export const crossDep` 导出的配置 */
|
|
178
|
-
function defineCrossDep(config) {
|
|
179
|
-
return config;
|
|
180
|
-
}
|
|
181
|
-
/** 定义一个通过 `export const runtimeCore` 导出的配置 */
|
|
182
|
-
function defineRuntimeCore(config) {
|
|
183
|
-
return config;
|
|
184
|
-
}
|
|
185
|
-
/** 定义一个通过 `export const runtimeEvent` 导出的配置 */
|
|
186
|
-
function defineRuntimeEvent(config) {
|
|
187
|
-
return config;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* 定义一个无法分割的对象配置,这对一些强依赖对象内部属性的设置很有用,可以避免对象被错误地拼接。
|
|
191
|
-
*
|
|
192
|
-
* 默认情况下,当定义一个对象配置时,将允许从其它配置文件中合并一部分配置到对象中,比如:
|
|
193
|
-
*
|
|
194
|
-
* ```typescript
|
|
195
|
-
* // 当前配置
|
|
196
|
-
* exampleConfig.obj = {
|
|
197
|
-
* value1: 'hello world'
|
|
198
|
-
* }
|
|
199
|
-
* // 如果还有一个配置文件中也声明了这个配置
|
|
200
|
-
* exampleConfig.obj = {
|
|
201
|
-
* value2: 'hello swpp'
|
|
202
|
-
* }
|
|
203
|
-
* // 最终将合并生成如下配置
|
|
204
|
-
* exampleConfig.obj = {
|
|
205
|
-
* value1: 'hello world',
|
|
206
|
-
* value2: 'hello swpp'
|
|
207
|
-
* }
|
|
208
|
-
* ```
|
|
209
|
-
*
|
|
210
|
-
* 通过该函数,可以禁止 swpp 合并配置时仅选取对象的部分字段,要么全部使用 [value] 的值,要么完全不使用 [value] 的值。
|
|
211
|
-
*
|
|
212
|
-
* 放入到上述例子中,假如两个 obj 任意一个或多个通过 `defineIndivisibleConfig({ xxx: xxx })` 设置,最终的值将取决于两个配置文件的优先级,
|
|
213
|
-
* 若 `value2` 的优先级高将产生:
|
|
214
|
-
*
|
|
215
|
-
* ```typescript
|
|
216
|
-
* // 最终结果
|
|
217
|
-
* exampleConfig.obj = {
|
|
218
|
-
* value2: 'hello swpp'
|
|
219
|
-
* }
|
|
220
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
function defineIndivisibleConfig(value) {
|
|
223
|
-
Object.defineProperty(value, IndivisibleName, {
|
|
224
|
-
value: true,
|
|
225
|
-
writable: false,
|
|
226
|
-
configurable: false,
|
|
227
|
-
enumerable: false
|
|
228
|
-
});
|
|
229
|
-
return value;
|
|
230
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { FileParserRegistry } from '../FileParser';
|
|
2
2
|
import { UpdateJson } from '../JsonBuilder';
|
|
3
3
|
import { FiniteConcurrencyFetcher } from '../NetworkFileHandler';
|
|
4
|
+
import { FileUpdateTracker } from '../ResourcesScanner';
|
|
5
|
+
import { CompilationData } from '../SwCompiler';
|
|
4
6
|
import { CrossDepCode } from './CrossDepCode';
|
|
5
7
|
import { CrossEnv } from './CrossEnv';
|
|
6
8
|
import { KeyValueDatabase } from './KeyValueDatabase';
|
|
@@ -22,22 +24,33 @@ export declare enum AllowNotFoundEnum {
|
|
|
22
24
|
}
|
|
23
25
|
declare function buildCommon(_env: any, crossEnv: CrossEnv, crossCode: CrossDepCode): {
|
|
24
26
|
readonly DOMAIN_HOST: import("./KeyValueDatabase").DatabaseValue<string>;
|
|
25
|
-
|
|
27
|
+
readonly SERVICE_WORKER: import("./KeyValueDatabase").DatabaseValue<string>;
|
|
28
|
+
/** HTML 数量限制,设置为 <= 0 表示不限制 */
|
|
26
29
|
readonly JSON_HTML_LIMIT: import("./KeyValueDatabase").DatabaseValue<number>;
|
|
27
30
|
/** 版本信息长度限制 */
|
|
28
31
|
readonly VERSION_LENGTH_LIMIT: import("./KeyValueDatabase").DatabaseValue<number>;
|
|
29
|
-
/**
|
|
30
|
-
readonly
|
|
32
|
+
/** swpp 的 JSON 文件的基本信息 */
|
|
33
|
+
readonly SWPP_JSON_FILE: import("./KeyValueDatabase").DatabaseValue<{
|
|
34
|
+
swppPath: string;
|
|
35
|
+
trackerPath: string;
|
|
36
|
+
versionPath: string;
|
|
37
|
+
fetchVersionFile(): Promise<UpdateJson>;
|
|
38
|
+
fetchTrackerFile(compilation: CompilationData): Promise<FileUpdateTracker>;
|
|
39
|
+
}>;
|
|
31
40
|
/** 读取一个本地文件 */
|
|
32
|
-
readonly
|
|
41
|
+
readonly readLocalFile: import("./KeyValueDatabase").DatabaseValue<(path: string) => Promise<string>>;
|
|
33
42
|
/** 拉取网络文件 */
|
|
34
|
-
readonly
|
|
43
|
+
readonly NETWORK_FILE_FETCHER: import("./KeyValueDatabase").DatabaseValue<FiniteConcurrencyFetcher>;
|
|
35
44
|
/** 判断文件是否是 404 */
|
|
36
|
-
readonly
|
|
45
|
+
readonly isNotFound: import("./KeyValueDatabase").DatabaseValue<{
|
|
37
46
|
response: (response: Response) => boolean;
|
|
38
47
|
error: (err: any) => boolean;
|
|
39
48
|
}>;
|
|
49
|
+
/** 是否允许 404 */
|
|
40
50
|
readonly ALLOW_NOT_FOUND: import("./KeyValueDatabase").DatabaseValue<AllowNotFoundEnum>;
|
|
51
|
+
/** 文件解析器 */
|
|
41
52
|
readonly FILE_PARSER: import("./KeyValueDatabase").DatabaseValue<FileParserRegistry>;
|
|
53
|
+
/** 检查一个链接是否是稳定的(也就是 URL 不变其返回的结果永远不变) */
|
|
54
|
+
readonly isStable: import("./KeyValueDatabase").DatabaseValue<(_url: URL) => boolean>;
|
|
42
55
|
};
|
|
43
56
|
export {};
|