phecda-module 1.0.0

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,22 @@
1
+  WARN  Issue while reading "/home/runner/work/phecda/phecda/.npmrc". Failed to replace env in config: ${NPM_TOKEN}
2
+
3
+ > phecda-module@1.0.0 build /home/runner/work/phecda/phecda/packages/module
4
+ > tsup
5
+
6
+ CLI Building entry: src/index.ts, src/vite.ts
7
+ CLI Using tsconfig: ../../tsconfig.json
8
+ CLI tsup v6.5.0
9
+ CLI Using tsup config: /home/runner/work/phecda/phecda/packages/module/tsup.config.ts
10
+ CLI Target: esnext
11
+ CJS Build start
12
+ ESM Build start
13
+ ESM dist/index.mjs 1.91 KB
14
+ ESM dist/vite.mjs 519.00 B
15
+ ESM ⚡️ Build success in 99ms
16
+ CJS dist/index.js 3.13 KB
17
+ CJS dist/vite.js 1.42 KB
18
+ CJS ⚡️ Build success in 101ms
19
+ DTS Build start
20
+ DTS ⚡️ Build success in 3602ms
21
+ DTS dist/index.d.ts 186.00 B
22
+ DTS dist/vite.d.ts 210.00 B
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 fgsreally
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ export * from 'phecda-core';
2
+
3
+ declare const moduleMap: Map<string, any>;
4
+ declare function Factory(Modules: (new (...args: any) => any)[]): Promise<void>;
5
+
6
+ export { Factory, moduleMap };
package/dist/index.js ADDED
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // src/index.ts
23
+ var src_exports = {};
24
+ __export(src_exports, {
25
+ Factory: () => Factory,
26
+ moduleMap: () => moduleMap
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+
30
+ // src/core.ts
31
+ var import_phecda_core = require("phecda-core");
32
+ var import_reflect_metadata = require("reflect-metadata");
33
+ var moduleMap = /* @__PURE__ */ new Map();
34
+ async function Factory(Modules) {
35
+ for (const Module of Modules)
36
+ await buildNestModule(Module);
37
+ }
38
+ __name(Factory, "Factory");
39
+ if (__DEV__) {
40
+ window.__PHECDA_MODULE_UPDATE__ = (target) => {
41
+ target = Object.values(target)[0];
42
+ const tag = target.prototype?.__TAG__ || target.name;
43
+ const module2 = moduleMap.get(tag);
44
+ module2.destroy?.();
45
+ moduleMap.delete(tag);
46
+ buildNestModule(target);
47
+ };
48
+ }
49
+ async function buildNestModule(Module) {
50
+ const paramtypes = getParamtypes(Module);
51
+ let instance;
52
+ const tag = Module.prototype?.__TAG__ || Module.name;
53
+ if (moduleMap.has(tag)) {
54
+ instance = moduleMap.get(tag);
55
+ if (!instance)
56
+ throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
57
+ return instance;
58
+ }
59
+ moduleMap.set(tag, void 0);
60
+ if (paramtypes) {
61
+ const paramtypesInstances = [];
62
+ for (const i in paramtypes)
63
+ paramtypesInstances[i] = await buildNestModule(paramtypes[i]);
64
+ instance = new Module(...paramtypesInstances);
65
+ } else {
66
+ instance = new Module();
67
+ }
68
+ await (0, import_phecda_core.registerAsync)(instance);
69
+ moduleMap.set(tag, instance);
70
+ if (__DEV__) {
71
+ const proxy = new Proxy({}, {
72
+ get(_target, p) {
73
+ return moduleMap.get(tag)[p];
74
+ },
75
+ set(_target, p, value) {
76
+ moduleMap.get(tag)[p] = value;
77
+ return true;
78
+ }
79
+ });
80
+ return proxy;
81
+ }
82
+ return instance;
83
+ }
84
+ __name(buildNestModule, "buildNestModule");
85
+ function getParamtypes(Module, key) {
86
+ return Reflect.getMetadata("design:paramtypes", Module, key);
87
+ }
88
+ __name(getParamtypes, "getParamtypes");
89
+
90
+ // src/index.ts
91
+ __reExport(src_exports, require("phecda-core"), module.exports);
92
+ // Annotate the CommonJS export names for ESM import in node:
93
+ 0 && (module.exports = {
94
+ Factory,
95
+ moduleMap
96
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,69 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/core.ts
5
+ import { registerAsync } from "phecda-core";
6
+ import "reflect-metadata";
7
+ var moduleMap = /* @__PURE__ */ new Map();
8
+ async function Factory(Modules) {
9
+ for (const Module of Modules)
10
+ await buildNestModule(Module);
11
+ }
12
+ __name(Factory, "Factory");
13
+ if (__DEV__) {
14
+ window.__PHECDA_MODULE_UPDATE__ = (target) => {
15
+ target = Object.values(target)[0];
16
+ const tag = target.prototype?.__TAG__ || target.name;
17
+ const module = moduleMap.get(tag);
18
+ module.destroy?.();
19
+ moduleMap.delete(tag);
20
+ buildNestModule(target);
21
+ };
22
+ }
23
+ async function buildNestModule(Module) {
24
+ const paramtypes = getParamtypes(Module);
25
+ let instance;
26
+ const tag = Module.prototype?.__TAG__ || Module.name;
27
+ if (moduleMap.has(tag)) {
28
+ instance = moduleMap.get(tag);
29
+ if (!instance)
30
+ throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
31
+ return instance;
32
+ }
33
+ moduleMap.set(tag, void 0);
34
+ if (paramtypes) {
35
+ const paramtypesInstances = [];
36
+ for (const i in paramtypes)
37
+ paramtypesInstances[i] = await buildNestModule(paramtypes[i]);
38
+ instance = new Module(...paramtypesInstances);
39
+ } else {
40
+ instance = new Module();
41
+ }
42
+ await registerAsync(instance);
43
+ moduleMap.set(tag, instance);
44
+ if (__DEV__) {
45
+ const proxy = new Proxy({}, {
46
+ get(_target, p) {
47
+ return moduleMap.get(tag)[p];
48
+ },
49
+ set(_target, p, value) {
50
+ moduleMap.get(tag)[p] = value;
51
+ return true;
52
+ }
53
+ });
54
+ return proxy;
55
+ }
56
+ return instance;
57
+ }
58
+ __name(buildNestModule, "buildNestModule");
59
+ function getParamtypes(Module, key) {
60
+ return Reflect.getMetadata("design:paramtypes", Module, key);
61
+ }
62
+ __name(getParamtypes, "getParamtypes");
63
+
64
+ // src/index.ts
65
+ export * from "phecda-core";
66
+ export {
67
+ Factory,
68
+ moduleMap
69
+ };
package/dist/vite.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ declare function export_default(reg?: RegExp): {
2
+ name: string;
3
+ enforce: string;
4
+ apply: string;
5
+ transform(code: string, id: string): string | undefined;
6
+ };
7
+
8
+ export { export_default as default };
package/dist/vite.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/vite.ts
22
+ var vite_exports = {};
23
+ __export(vite_exports, {
24
+ default: () => vite_default
25
+ });
26
+ module.exports = __toCommonJS(vite_exports);
27
+ function vite_default(reg = /\.module\.ts$/) {
28
+ return {
29
+ name: "phecda-module-hmr",
30
+ enforce: "pre",
31
+ apply: "serve",
32
+ transform(code, id) {
33
+ if (reg.test(id)) {
34
+ return code += `
35
+ if (import.meta.hot)
36
+ import.meta.hot.accept(__PHECDA_MODULE_UPDATE__)`;
37
+ }
38
+ }
39
+ };
40
+ }
41
+ __name(vite_default, "default");
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {});
package/dist/vite.mjs ADDED
@@ -0,0 +1,22 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/vite.ts
5
+ function vite_default(reg = /\.module\.ts$/) {
6
+ return {
7
+ name: "phecda-module-hmr",
8
+ enforce: "pre",
9
+ apply: "serve",
10
+ transform(code, id) {
11
+ if (reg.test(id)) {
12
+ return code += `
13
+ if (import.meta.hot)
14
+ import.meta.hot.accept(__PHECDA_MODULE_UPDATE__)`;
15
+ }
16
+ }
17
+ };
18
+ }
19
+ __name(vite_default, "default");
20
+ export {
21
+ vite_default as default
22
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "phecda-module",
3
+ "version": "1.0.0",
4
+ "description": "provide DI and HMR for modules by reflect-metadata and vite",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ },
13
+ "./vite": {
14
+ "import": "./dist/vite.mjs",
15
+ "require": "./dist/vite.js"
16
+ }
17
+ },
18
+ "typesVersions": {
19
+ "*": {
20
+ "vite": [
21
+ "dist/vite.d.ts"
22
+ ]
23
+ }
24
+ },
25
+ "keywords": [],
26
+ "author": "",
27
+ "license": "ISC",
28
+ "dependencies": {
29
+ "reflect-metadata": "^0.1.13",
30
+ "phecda-core": "2.0.0-alpha.7"
31
+ },
32
+ "devDependencies": {
33
+ "tsup": "^6.5.0"
34
+ },
35
+ "scripts": {
36
+ "dev": "tsup --watch",
37
+ "build": "tsup"
38
+ }
39
+ }
package/src/core.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { registerAsync } from 'phecda-core'
2
+ import 'reflect-metadata'
3
+
4
+ type Construct<T = any> = new (...args: any[]) => T
5
+
6
+ export const moduleMap = new Map<string, InstanceType<Construct>>()
7
+
8
+ export async function Factory(Modules: (new (...args: any) => any)[]) {
9
+ for (const Module of Modules)
10
+ await buildNestModule(Module)
11
+ }
12
+
13
+ if (__DEV__) {
14
+ // @ts-expect-error work for hmr
15
+ window.__PHECDA_MODULE_UPDATE__ = (target) => {
16
+ target = Object.values(target)[0]
17
+ const tag = target.prototype?.__TAG__ || target.name
18
+ const module = moduleMap.get(tag)
19
+ module.destroy?.()
20
+ moduleMap.delete(tag)
21
+ buildNestModule(target)
22
+ }
23
+ }
24
+
25
+ async function buildNestModule(Module: Construct) {
26
+ const paramtypes = getParamtypes(Module) as Construct[]
27
+
28
+ let instance: InstanceType<Construct>
29
+ const tag = Module.prototype?.__TAG__ || Module.name
30
+
31
+ if (moduleMap.has(tag)) {
32
+ instance = moduleMap.get(tag)
33
+ if (!instance)
34
+ throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`)
35
+
36
+ return instance
37
+ }
38
+ moduleMap.set(tag, undefined)
39
+ if (paramtypes) {
40
+ const paramtypesInstances = [] as any[]
41
+ for (const i in paramtypes)
42
+ paramtypesInstances[i] = await buildNestModule(paramtypes[i])
43
+
44
+ instance = new Module(...paramtypesInstances)
45
+ }
46
+ else {
47
+ instance = new Module()
48
+ }
49
+ await registerAsync(instance)
50
+ moduleMap.set(tag, instance)
51
+ if (__DEV__) {
52
+ const proxy = new Proxy({}, {
53
+ get(_target, p) {
54
+ return moduleMap.get(tag)[p]
55
+ },
56
+ set(_target, p, value) {
57
+ moduleMap.get(tag)[p] = value
58
+ return true
59
+ },
60
+ })
61
+
62
+ return proxy
63
+ }
64
+ return instance
65
+ }
66
+
67
+ function getParamtypes(Module: any, key?: string | symbol) {
68
+ return Reflect.getMetadata('design:paramtypes', Module, key!)
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './core'
2
+ export * from 'phecda-core'
package/src/vite.ts ADDED
@@ -0,0 +1,13 @@
1
+ export default function (reg = /\.module\.ts$/) {
2
+ return {
3
+ name: 'phecda-module-hmr',
4
+ enforce: 'pre',
5
+ apply: 'serve',
6
+ transform(code: string, id: string) {
7
+ if (reg.test(id)) {
8
+ return code += `\nif (import.meta.hot)
9
+ import.meta.hot.accept(__PHECDA_MODULE_UPDATE__)`
10
+ }
11
+ },
12
+ }
13
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { Options } from 'tsup'
2
+
3
+ export const tsup: Options = {
4
+ entry: ['src/index.ts', 'src/vite.ts'],
5
+ format: ['cjs', 'esm'],
6
+ dts: true,
7
+ splitting: false,
8
+ shims: false,
9
+ }