vivth 1.3.2 → 1.3.4

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.
Files changed (61) hide show
  1. package/README.md +2156 -1782
  2. package/README.src.md +10 -0
  3. package/bun.lock +6 -0
  4. package/index.mjs +12 -3
  5. package/package.json +4 -2
  6. package/src/bundler/CompileJS.mjs +62 -28
  7. package/src/bundler/EsBundler.mjs +2 -1
  8. package/src/bundler/FSInline.mjs +8 -1
  9. package/src/bundler/FSInlineAnalyzer.mjs +47 -38
  10. package/src/bundler/adds/ToBundledJSPlugin.mjs +42 -28
  11. package/src/bundler/adds/pluginVivthBundle.mjs +3 -1
  12. package/src/class/EventSignal.mjs +15 -9
  13. package/src/class/FileSafe.mjs +30 -1
  14. package/src/class/ListSignal.mjs +1 -1
  15. package/src/class/LitExp.mjs +3 -2
  16. package/src/class/Paths.mjs +2 -11
  17. package/src/class/QChannel.mjs +4 -5
  18. package/src/class/SafeExit.mjs +7 -24
  19. package/src/class/Setup.mjs +5 -8
  20. package/src/class/Signal.mjs +4 -1
  21. package/src/common/Base64URL.mjs +6 -4
  22. package/src/common/Dev.mjs +134 -0
  23. package/src/common/DevBundled.mjs +5 -0
  24. package/src/common/Trace.mjs +24 -0
  25. package/src/doc/JSautoDOC.mjs +16 -11
  26. package/src/doc/correctBeforeParse.mjs +1 -1
  27. package/src/doc/parsedFile.mjs +1 -1
  28. package/src/function/GetNamedImportAlias.mjs +37 -0
  29. package/src/function/LazyFactory.mjs +1 -1
  30. package/src/function/TsToMjs.mjs +3 -3
  31. package/src/types/DevTestCB.mjs +11 -0
  32. package/src/types/Runtime.mjs +0 -1
  33. package/src/types/VivthDevCodeBlockStringType.mjs +6 -0
  34. package/tsconfig.json +6 -1
  35. package/types/index.d.mts +8 -3
  36. package/types/src/bundler/CompileJS.d.mts +58 -28
  37. package/types/src/bundler/EsBundler.d.mts +2 -2
  38. package/types/src/bundler/FSInline.d.mts +8 -1
  39. package/types/src/bundler/FSInlineAnalyzer.d.mts +1 -0
  40. package/types/src/bundler/adds/ToBundledJSPlugin.d.mts +6 -3
  41. package/types/src/class/EventSignal.d.mts +8 -8
  42. package/types/src/class/FileSafe.d.mts +21 -0
  43. package/types/src/class/ListSignal.d.mts +2 -2
  44. package/types/src/class/LitExp.d.mts +1 -0
  45. package/types/src/class/Paths.d.mts +2 -9
  46. package/types/src/class/QChannel.d.mts +4 -4
  47. package/types/src/class/SafeExit.d.mts +7 -24
  48. package/types/src/class/Setup.d.mts +5 -8
  49. package/types/src/common/Base64URL.d.mts +5 -3
  50. package/types/src/common/Dev.d.mts +68 -0
  51. package/types/src/common/DevBundled.d.mts +3 -0
  52. package/types/src/common/Trace.d.mts +14 -0
  53. package/types/src/doc/JSautoDOC.d.mts +8 -7
  54. package/types/src/function/GetNamedImportAlias.d.mts +22 -0
  55. package/types/src/function/LazyFactory.d.mts +1 -1
  56. package/types/src/function/TsToMjs.d.mts +3 -3
  57. package/types/src/function/WithDefer.d.mts +10 -0
  58. package/types/src/types/DevTestCB.d.mts +3 -0
  59. package/types/src/types/IsDevCBStringType.d.mts +4 -0
  60. package/types/src/types/Runtime.d.mts +3 -0
  61. package/types/src/types/VivthDevCodeBlockStringType.d.mts +4 -0
package/README.src.md CHANGED
@@ -10,6 +10,9 @@ npm i vivth
10
10
 
11
11
  - `vivth` are intended to be a collections of usefull `primitives`, and not to be directly used as
12
12
  underlying framework;
13
+ - `vivth` are leaning to `esm(.mjs)+jsdoc` first;
14
+ > - some functionality "might need" work around if you are using other than `esm+jsdoc`;
15
+ > - `CompileJS`, `ESBundler` expose `format` option for some leeway for `commonJS`;
13
16
  - completely `JSruntime` agnostic:
14
17
  > - instead of `vivth` trying to guess, all runtime specific API should be provided by dev;
15
18
  - contains helpers to help you write data driven javascript program, including:
@@ -28,6 +31,11 @@ npm i vivth
28
31
  > > - abstracted via `esbuild`;
29
32
  > - opionated `compiler`;
30
33
  > > - abstracted via `pkg`, `deno`, and `bun`;
34
+ - when using runtime that doesn't provide specific common modules golbally(like `Deno`), dev should
35
+ import it statically from `node:module_name`, example:
36
+ ```js
37
+ import process from 'node:process';
38
+ ```
31
39
 
32
40
  ## versions:
33
41
 
@@ -40,3 +48,5 @@ npm i vivth
40
48
 
41
49
  - `1.3.x:beta`:
42
50
  > - type should now fully fixed, even with strict ts check;
51
+ - `1.3.3+:beta`:
52
+ > - added class `Dev` for testing and `Dev`/`preBundled` only code block;
package/bun.lock CHANGED
@@ -14,7 +14,9 @@
14
14
  "devDependencies": {
15
15
  "@types/bun": "latest",
16
16
  "concurrently": "^9.2.1",
17
+ "prettier": "^3.6.2",
17
18
  "typescript": "^5.9.2",
19
+ "vivth": "link:vivth",
18
20
  },
19
21
  },
20
22
  },
@@ -261,6 +263,8 @@
261
263
 
262
264
  "prebuild-install": ["prebuild-install@7.1.1", "", { "dependencies": { "detect-libc": "^2.0.0", "expand-template": "^2.0.3", "github-from-package": "0.0.0", "minimist": "^1.2.3", "mkdirp-classic": "^0.5.3", "napi-build-utils": "^1.0.1", "node-abi": "^3.3.0", "pump": "^3.0.0", "rc": "^1.2.7", "simple-get": "^4.0.0", "tar-fs": "^2.0.0", "tunnel-agent": "^0.6.0" }, "bin": { "prebuild-install": "bin.js" } }, "sha512-jAXscXWMcCK8GgCoHOfIr0ODh5ai8mj63L2nWrjuAgXE6tDyYGnx4/8o/rCgU+B4JSyZBKbeZqzhtwtC3ovxjw=="],
263
265
 
266
+ "prettier": ["prettier@3.6.2", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ=="],
267
+
264
268
  "process-nextick-args": ["process-nextick-args@2.0.1", "", {}, "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="],
265
269
 
266
270
  "progress": ["progress@2.0.3", "", {}, "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA=="],
@@ -335,6 +339,8 @@
335
339
 
336
340
  "util-deprecate": ["util-deprecate@1.0.2", "", {}, "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="],
337
341
 
342
+ "vivth": ["vivth@link:vivth", {}],
343
+
338
344
  "webidl-conversions": ["webidl-conversions@3.0.1", "", {}, "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="],
339
345
 
340
346
  "whatwg-url": ["whatwg-url@5.0.0", "", { "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" } }, "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw=="],
package/index.mjs CHANGED
@@ -6,6 +6,7 @@
6
6
  * this library is made and distributed under MIT license;
7
7
  */
8
8
 
9
+ export { ToBundledJSPlugin } from './src/bundler/adds/ToBundledJSPlugin.mjs';
9
10
  export { CompileJS } from './src/bundler/CompileJS.mjs';
10
11
  export { CreateESPlugin } from './src/bundler/CreateESPlugin.mjs';
11
12
  export { EsBundler } from './src/bundler/EsBundler.mjs';
@@ -26,13 +27,18 @@ export { SafeExit } from './src/class/SafeExit.mjs';
26
27
  export { Setup } from './src/class/Setup.mjs';
27
28
  export { Signal } from './src/class/Signal.mjs';
28
29
  export { WorkerMainThread } from './src/class/WorkerMainThread.mjs';
30
+ export { WorkerResult } from './src/class/WorkerResult.mjs';
31
+ export { WorkerThread } from './src/class/WorkerThread.mjs';
29
32
  export { Base64URL } from './src/common/Base64URL.mjs';
30
33
  export { Base64URLFromFile } from './src/common/Base64URLFromFile.mjs';
34
+ export { Dev } from './src/common/Dev.mjs';
31
35
  export { EventNameSpace } from './src/common/EventNameSpace.mjs';
36
+ export { Trace } from './src/common/Trace.mjs';
32
37
  export { JSautoDOC } from './src/doc/JSautoDOC.mjs';
33
38
  export { CreateImmutable } from './src/function/CreateImmutable.mjs';
34
39
  export { EventCheck } from './src/function/EventCheck.mjs';
35
40
  export { EventObject } from './src/function/EventObject.mjs';
41
+ export { GetNamedImportAlias } from './src/function/GetNamedImportAlias.mjs';
36
42
  export { GetRuntime } from './src/function/GetRuntime.mjs';
37
43
  export { IsAsync } from './src/function/IsAsync.mjs';
38
44
  export { LazyFactory } from './src/function/LazyFactory.mjs';
@@ -41,12 +47,12 @@ export { Try } from './src/function/Try.mjs';
41
47
  export { TryAsync } from './src/function/TryAsync.mjs';
42
48
  export { TrySync } from './src/function/TrySync.mjs';
43
49
  export { TsToMjs } from './src/function/TsToMjs.mjs';
44
- export { WorkerResult } from './src/class/WorkerResult.mjs';
45
- export { WorkerThread } from './src/class/WorkerThread.mjs';
46
- export { ToBundledJSPlugin } from './src/bundler/adds/ToBundledJSPlugin.mjs';
47
50
  /**
48
51
  * @typedef {import('./src/types/AnyButUndefined.mjs').AnyButUndefined} AnyButUndefined
49
52
  */
53
+ /**
54
+ * @typedef {import('./src/types/DevTestCB.mjs').DevTestCB} DevTestCB
55
+ */
50
56
  /**
51
57
  * @typedef {import('./src/types/ExtnameType.mjs').ExtnameType} ExtnameType
52
58
  */
@@ -74,4 +80,7 @@ export { ToBundledJSPlugin } from './src/bundler/adds/ToBundledJSPlugin.mjs';
74
80
  */
75
81
  /**
76
82
  * @typedef {import('./src/types/Runtime.mjs').Runtime} Runtime
83
+ */
84
+ /**
85
+ * @typedef {import('./src/types/VivthDevCodeBlockStringType.mjs').VivthDevCodeBlockStringType} VivthDevCodeBlockStringType
77
86
  */
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "library primitives",
5
5
  "main": "index.mjs",
6
6
  "types": "./types/index.d.mts",
7
7
  "devDependencies": {
8
8
  "@types/bun": "latest",
9
9
  "concurrently": "^9.2.1",
10
- "typescript": "^5.9.2"
10
+ "typescript": "^5.9.2",
11
+ "vivth": "link:vivth"
11
12
  },
12
13
  "dependencies": {
14
+ "prettier": "^3.6.2",
13
15
  "@types/mime-types": "^3.0.1",
14
16
  "chokidar": "^4.0.3",
15
17
  "esbuild": "^0.25.9",
@@ -10,6 +10,7 @@ import { EsBundler } from './EsBundler.mjs';
10
10
  import { FileSafe } from '../class/FileSafe.mjs';
11
11
  import { TryAsync } from '../function/TryAsync.mjs';
12
12
  import { FSInlineAnalyzer } from './FSInlineAnalyzer.mjs';
13
+ import { removeVivthDevCodeBlock } from './adds/ToBundledJSPlugin.mjs';
13
14
 
14
15
  /**
15
16
  * @typedef {'win32' | 'linux' | 'darwin' | string} PlatformKey
@@ -67,9 +68,9 @@ const getBinaryExtension = () => {
67
68
  /**
68
69
  * @description
69
70
  * - function to compile `.ts`|`.mts`|`.mjs` file, into a single executable;
70
- * - also generate js representation;
71
+ * - also generate js representation of the `bundled` version of the target;
71
72
  * - uses [pkg](https://www.npmjs.com/package/pkg), [bun](https://bun.com/docs/bundler/executables), and [deno](https://docs.deno.com/runtime/reference/cli/compile/) compiler under the hood;
72
- * >- they are used only as packaging agent, and doesn't necessarily supports their advanced feature, such as, assets bundling(use [FSInline](#fsinline) instead);
73
+ * >- they are used only as packaging/compiler agent, and doesn't necessarily supports their advanced feature, such as, assets bundling(use [`FSInline`](#fsinline) instead);
73
74
  * >- `WorkerThread` will be converted to inline using `FSInline` too;
74
75
  *
75
76
  * !!!WARNING!!!
@@ -88,8 +89,12 @@ const getBinaryExtension = () => {
88
89
  * @param {string} options.entryPoint
89
90
  * - need to be manually prefixed;
90
91
  * @param {BufferEncoding} [options.encoding]
91
- * - write and read encoding for the sources;
92
+ * - read and write encoding for the sources;
92
93
  * - default: `utf-8`;
94
+ * @param {(entryPointContent:string)=>string} [options.preprocessEntryPoint]
95
+ * - to modify entry point before bundling;
96
+ * - `entryPointContent` is the original string of the entry point;
97
+ * - returned value then passed to `ESBundler`;
93
98
  * @param {boolean} options.minifyFirst
94
99
  * - minify the bundle before compilation;
95
100
  * @param {string} options.outDir
@@ -109,45 +114,71 @@ const getBinaryExtension = () => {
109
114
  * bundledJSFile:string|undefined
110
115
  * }>>}
111
116
  * @example
117
+ * import process from 'node:process';
112
118
  * import { join } from 'node:path';
113
119
  *
114
- * import { CompileJS, Paths } from 'vivth';
120
+ * import { CompileJS, Console, Paths, Setup } from 'vivth';
121
+ *
122
+ * const { paths, safeExit } = Setup;
123
+ * new paths({
124
+ * root: process.env.INIT_CWD ?? process.cwd(),
125
+ * });
126
+ * new safeExit({
127
+ * eventNames: ['SIGINT', 'SIGTERM'],
128
+ * terminator: () => process.exit(0),
129
+ * listener: (eventName) => {
130
+ * process.once(eventName, function () {
131
+ * if (!safeExit.instance) {
132
+ * return;
133
+ * }
134
+ * safeExit.instance.exiting.correction(true);
135
+ * Console.log(`safe exit via "${eventName}"`);
136
+ * });
137
+ * },
138
+ * });
139
+ * const pathRoot = Paths.root;
140
+ * if (pathRoot) {
141
+ * const [[, error], [, errorbun]] = await Promise.all([
142
+ * CompileJS({
143
+ * entryPoint: join(pathRoot, '/dev/myEntryPoint.mjs'),
144
+ * minifyFirst: true,
145
+ * outDir: join(pathRoot, '/dev-pkg/'),
146
+ * compiler: 'pkg',
147
+ * compilerArguments: {
148
+ * target: 'node18-win-x64',
149
+ * },
150
+ * encoding: 'utf-8',
151
+ * }),
152
+ * await CompileJS({
153
+ * entryPoint: join(pathRoot, '/dev/myEntryPoint.mjs'),
154
+ * minifyFirst: true,
155
+ * outDir: join(pathRoot, '/dev-bun/'),
156
+ * compiler: 'bun',
157
+ * compilerArguments: {
158
+ * target: 'bun-win-x64',
159
+ * },
160
+ * encoding: 'utf-8',
161
+ * }),
162
+ * ]);
163
+ * if (error || errorbun) {
164
+ * Console.error({ error, errorbun });
165
+ * }
166
+ * }
115
167
  *
116
- * const [[resultPkg, errorPkg], [resultBun, errorBun]] = await Promise.all([
117
- * CompileJS({
118
- * entryPoint: join(Paths.root, '/dev'),
119
- * minifyFirst: true,
120
- * outDir: join(Paths.root, '/dev-pkg'),
121
- * compiler: 'pkg',
122
- * compilerArguments: {
123
- * target: ['node18-win-x64'],
124
- * },
125
- * esBundlerPlugins: [],
126
- * }),
127
- * CompileJS({
128
- * entryPoint: join(Paths.root, '/dev'),
129
- * minifyFirst: true,
130
- * outDir: join(Paths.root, '/dev-pkg'),
131
- * compiler: 'bun',
132
- * compilerArguments: {
133
- * target: ['bun-win-x64'],
134
- * },
135
- * esBundlerPlugins: [],
136
- * }),
137
- * ])
138
168
  */
139
169
  export async function CompileJS({
140
170
  entryPoint,
141
171
  minifyFirst,
142
172
  encoding = 'utf-8',
143
173
  outDir,
174
+ preprocessEntryPoint = undefined,
144
175
  compiler = undefined,
145
176
  compilerArguments = {},
146
177
  esBundlerPlugins = [],
147
178
  }) {
148
179
  return await TryAsync(async () => {
149
180
  /**
150
- * @type {'cjs'|'esm'|'iife'}
181
+ * @type {'cjs'|'esm'}
151
182
  */
152
183
  let format;
153
184
  switch (compiler) {
@@ -174,7 +205,10 @@ export async function CompileJS({
174
205
  `extention mismatch: "${extOfSource}", should be one of:".mjs"|".mts"|".ts"`
175
206
  );
176
207
  }
177
- const sourceText = await readFile(entryPoint, { encoding });
208
+ let sourceText = removeVivthDevCodeBlock(await readFile(entryPoint, { encoding }));
209
+ if (preprocessEntryPoint) {
210
+ sourceText = preprocessEntryPoint(sourceText);
211
+ }
178
212
  const [bundledPrep, errorPrep] = await EsBundler(
179
213
  {
180
214
  content: sourceText,
@@ -17,7 +17,7 @@ import { externals } from './adds/externals.mjs';
17
17
  * - the code can also uses composites from the result from multiple readFiles;
18
18
  * @param {string} options.root
19
19
  * - use dirname of said fileString path;
20
- * @param {'.mts'|'.ts'|'.mjs'} options.extension
20
+ * @param {'.mts'|'.ts'|'.mjs'|'.cjs'} options.extension
21
21
  * @param {boolean} [options.withBinHeader]
22
22
  * @param {Omit<Parameters<build>[0],
23
23
  * 'entryPoints'|'bundle'|'write'|'sourcemap'>
@@ -51,6 +51,7 @@ export async function EsBundler(
51
51
  loader = 'ts';
52
52
  break;
53
53
  case '.mjs':
54
+ case '.cjs':
54
55
  loader = 'js';
55
56
  break;
56
57
  default:
@@ -7,8 +7,11 @@ import { Paths } from '../class/Paths.mjs';
7
7
 
8
8
  /**
9
9
  * @description
10
- * - class helper to inline file;
10
+ * - class helper to inline files;
11
+ * >- as `type: "buffer"`;
11
12
  * - use only if you are planning to use [CompileJS](#compilejs);
13
+ * >- the class static methods don't obfuscate target file;
14
+ * >- don't embed any sensitive content using this methods of this class;
12
15
  */
13
16
  export class FSInline {
14
17
  /**
@@ -19,6 +22,9 @@ export class FSInline {
19
22
  * @param {string} filePathFromProject
20
23
  * - doesn't require prefix;
21
24
  * @returns {Promise<Buffer<ArrayBuffer>>}
25
+ * - dev: returns `ArrayBuffer`;
26
+ * - bundled: embed the `ArrayBuffer` of file, which then returned;
27
+ * >- `Dev` will be cleaned up;
22
28
  * @example
23
29
  * import { FSInline } from 'vivth';
24
30
  *
@@ -35,6 +41,7 @@ export class FSInline {
35
41
  /**
36
42
  * @description
37
43
  * - declare entrypoint of file inlining, include all files on `dir` and `subdir` that match the `fileRule`;
44
+ * - consider this as inline assets embed/bundler;
38
45
  * @param {string} dirPathFromProject
39
46
  * - doesn't require prefix;
40
47
  * @param {RegExp} fileRule
@@ -8,6 +8,7 @@ import { LitExp } from '../class/LitExp.mjs';
8
8
  import { TryAsync } from '../function/TryAsync.mjs';
9
9
  import { FSInline } from './FSInline.mjs';
10
10
  import { EsBundler } from './EsBundler.mjs';
11
+ import { removeVivthDevCodeBlock } from './adds/ToBundledJSPlugin.mjs';
11
12
 
12
13
  /**
13
14
  * @param {string} str
@@ -25,6 +26,7 @@ const hydrateRegex = (str) => {
25
26
  /**
26
27
  * @description
27
28
  * - collections of static method to analyze content for `FSInline`;
29
+ * - mostly used internally;
28
30
  */
29
31
  export class FSInlineAnalyzer {
30
32
  /**
@@ -45,25 +47,26 @@ export class FSInlineAnalyzer {
45
47
  */
46
48
  static finalContent = async (content, format) => {
47
49
  return await TryAsync(async () => {
50
+ content = removeVivthDevCodeBlock(content);
48
51
  const [literalFile, errorPrepareFile] = LitExp.prepare({
49
- FSInline: /\w*/,
52
+ FSInline: /[\w][\w\d]*/,
50
53
  method: /\.vivthFSInlineFile\s*?\(\s*?['"]/,
51
54
  path: false,
52
- closing: /['"]\s*?\)(?:;|)/,
55
+ closing: /['"]\s*?\)/,
53
56
  });
54
57
  const [literalWorker, errorPreparWorker] = LitExp.prepare({
55
- ref: /\w*/,
58
+ ref: /[\w][\w\d]*/,
56
59
  method: /\.newVivthWorker\s*?\(\s*?['"]/,
57
60
  path: false,
58
- closing: /['"]/,
61
+ closing: /['"]\s*\)/,
59
62
  });
60
63
  const [literalDir, errorPrepareDir] = LitExp.prepare({
61
- FSInline: /\w*/,
64
+ FSInline: /[\w][\w\d]*/,
62
65
  method: /\.vivthFSInlineDir\s*?\(\s*?['"]/,
63
66
  path: false,
64
- methodClosing: /['"]\s*?,/,
67
+ methodClosing: /['"]\s*?,\s*/,
65
68
  rule: false,
66
- functionClosing: /\)(?:;|)/,
69
+ functionClosing: /\s*?\)/,
67
70
  });
68
71
  if (errorPrepareFile || errorPrepareDir || errorPreparWorker) {
69
72
  throw { errorPrepareFile, errorPrepareDir, errorPreparWorker };
@@ -71,7 +74,6 @@ export class FSInlineAnalyzer {
71
74
  const templateFile = literalFile`${'FSInline'}${'method'}${'path'}${'closing'}`;
72
75
  const templateWorker = literalWorker`${'ref'}${'method'}${'path'}${'closing'}`;
73
76
  const templateDir = literalDir`${'FSInline'}${'method'}${'path'}${'methodClosing'}${'rule'}${'functionClosing'}`;
74
-
75
77
  const [resultMatchingFile, errorMatchingFile] = templateFile.evaluate.matchedAllAndGrouped(
76
78
  content,
77
79
  {
@@ -94,19 +96,47 @@ export class FSInlineAnalyzer {
94
96
  if (errorMatchingFile || errorMatchingDir || errorMatchingWorker) {
95
97
  throw { errorMatchingFile, errorMatchingDir, errorMatchingWorker };
96
98
  }
97
- const {
98
- result: { named: namedFile },
99
- } = resultMatchingFile;
100
99
  if (Paths.root === undefined) {
101
100
  throw new Error('Path.root undefined');
102
101
  }
102
+ const {
103
+ result: { named: namedFile },
104
+ } = resultMatchingFile;
103
105
  for (let i = 0; i < namedFile.length; i++) {
104
106
  const res = namedFile[i];
105
107
  if (res === undefined) {
106
108
  continue;
107
109
  }
108
110
  const { path } = res;
109
- FSInline.vivthFSInlinelists[path] = Buffer.from(await readFile(join(Paths.root, path)));
111
+ if ('path' in FSInline.vivthFSInlinelists) {
112
+ continue;
113
+ }
114
+ FSInline.vivthFSInlinelists[path] = Buffer.from(
115
+ removeVivthDevCodeBlock(await readFile(join(Paths.root, path), { encoding: 'utf-8' }))
116
+ );
117
+ }
118
+ const {
119
+ result: { named: namedDir },
120
+ } = resultMatchingDir;
121
+ for (let i = 0; i < namedDir.length; i++) {
122
+ const res = namedDir[i];
123
+ if (res === undefined) {
124
+ continue;
125
+ }
126
+ let { path, rule } = res;
127
+ rule = rule.trim();
128
+ const results = await FSInlineAnalyzer.#dir(join(Paths.root, path), hydrateRegex(rule));
129
+ for (let j = 0; j < results.length; j++) {
130
+ const res = results[j];
131
+ if (res === undefined) {
132
+ continue;
133
+ }
134
+ const { path, buffer } = res;
135
+ if (path in FSInline.vivthFSInlinelists) {
136
+ continue;
137
+ }
138
+ FSInline.vivthFSInlinelists[path] = buffer;
139
+ }
110
140
  }
111
141
  const {
112
142
  result: { named: namedWorker },
@@ -118,12 +148,12 @@ export class FSInlineAnalyzer {
118
148
  }
119
149
  const { path } = res;
120
150
  const fullPath = join(Paths.root, path);
121
- const content = await readFile(fullPath, { encoding: 'utf-8' });
151
+ const content = removeVivthDevCodeBlock(await readFile(fullPath, { encoding: 'utf-8' }));
122
152
  const [contentBundled, errorBundled] = await EsBundler(
123
153
  {
124
154
  content,
125
155
  // @ts-expect-error
126
- extension: extname(path),
156
+ extension: extname(fullPath),
127
157
  root: dirname(fullPath),
128
158
  },
129
159
  {
@@ -143,29 +173,6 @@ export class FSInlineAnalyzer {
143
173
  }
144
174
  FSInline.vivthFSInlinelists[path] = Buffer.from(trueContent);
145
175
  }
146
- const {
147
- result: { named: namedDir },
148
- } = resultMatchingDir;
149
- for (let i = 0; i < namedDir.length; i++) {
150
- const res = namedDir[i];
151
- if (res === undefined) {
152
- continue;
153
- }
154
- let { path, rule } = res;
155
- rule = rule.trim();
156
- const results = await FSInlineAnalyzer.#dir(join(Paths.root, path), hydrateRegex(rule));
157
- for (let j = 0; j < results.length; j++) {
158
- const res = results[j];
159
- if (res === undefined) {
160
- continue;
161
- }
162
- const { path, buffer } = res;
163
- if (path in FSInline.vivthFSInlinelists) {
164
- continue;
165
- }
166
- FSInline.vivthFSInlinelists[path] = buffer;
167
- }
168
- }
169
176
  return content.replace(
170
177
  /static\s*vivthFSInlinelists(?:;|)/,
171
178
  `static vivthFSInlinelists=${JSON.stringify(FSInline.vivthFSInlinelists)}`
@@ -208,7 +215,9 @@ export class FSInlineAnalyzer {
208
215
  if (entry.isFile() && ruleForFileFullPath.test(fullPath)) {
209
216
  result.push({
210
217
  path: relativePath,
211
- buffer: Buffer.from(await readFile(fullPath)),
218
+ buffer: Buffer.from(
219
+ removeVivthDevCodeBlock(await readFile(fullPath, { encoding: 'utf-8' }))
220
+ ),
212
221
  });
213
222
  }
214
223
  }
@@ -1,17 +1,38 @@
1
1
  // @ts-check
2
2
 
3
3
  import { basename, extname } from 'node:path';
4
- import { readFile, exists } from 'node:fs/promises';
4
+ import { readFile } from 'node:fs/promises';
5
+
5
6
  import { CreateESPlugin } from '../CreateESPlugin.mjs';
7
+ import { GetNamedImportAlias } from '../../function/GetNamedImportAlias.mjs';
8
+ import { FileSafe } from '../../class/FileSafe.mjs';
9
+
10
+ /**
11
+ * @param {string} originalContent_
12
+ * @returns {string}
13
+ */
14
+ export const removeVivthDevCodeBlock = (originalContent_) => {
15
+ const alias = GetNamedImportAlias(originalContent_, 'Dev', 'vivth');
16
+ if (!alias) {
17
+ return originalContent_;
18
+ }
19
+ const regex = new RegExp(
20
+ `(\\s*${alias}.vivthDevCodeBlock\\s*\\([\\s\\S]*?,\\s*['"]vivthDevCodeBlock['"]\\s*\\)(?:\\;|,|))`,
21
+ 'g'
22
+ );
23
+ return originalContent_.replace(regex, '');
24
+ };
6
25
 
7
26
  /**
8
27
  * @description
9
28
  * - generate `esbuild.Plugin` for changing dev time file into runtime file;
10
- * - on using esbuild with this plugin, it will replace any module that have similiar file name but ended with Bundled(before extname);
29
+ * - on using esbuild with this plugin, it will:
30
+ * >- replace any module that have similiar file name but ended with Bundled(before extname);
11
31
  * >- works on `.mts`|`.ts`|`.mjs`|`.cjs`|`.js`;
12
- * >- `anyFileName.mjs` -> seek for and use `anyFileNameBundled.mjs`, if not found use `anyFileName.mjs`;
32
+ * >- `${fileName}.mjs` -> seek for and use `${fileName}Bundled.mjs`, if not found use `${fileName}.mjs`;
33
+ * >- removes `Dev.vivthDevCodeBlock` code block;
13
34
  * @param {string} includedInPath
14
- * - is generalized, you can freely uses forward or backward slash;
35
+ * - is generalized path, you can freely uses forward or backward slash;
15
36
  * @returns {ReturnType<CreateESPlugin>}
16
37
  * @example
17
38
  * import { ToBundledJSPlugin } from 'vivth';
@@ -39,38 +60,31 @@ export function ToBundledJSPlugin(includedInPath) {
39
60
  loader = 'js';
40
61
  break;
41
62
  }
42
- const originalContent = (await readFile(filePath)).toString('utf-8');
43
- if (loader === undefined) {
44
- return {
45
- contents: originalContent,
46
- loader,
47
- };
48
- }
49
- if (filePath.endsWith(`Bundled${fileExt}`)) {
50
- return {
51
- contents: originalContent,
52
- loader,
53
- };
63
+ /**
64
+ * @type {ReturnType<Parameters<import('esbuild').PluginBuild["onLoad"]>[1]>}
65
+ */
66
+ const ret = { loader };
67
+ if (loader === undefined || filePath.endsWith(`Bundled${fileExt}`)) {
68
+ const originalContent = (await readFile(filePath)).toString('utf-8');
69
+ ret.contents = removeVivthDevCodeBlock(originalContent);
70
+ return ret;
54
71
  }
55
72
  const originalBaseName = basename(filePath);
56
73
  const originalFileName = originalBaseName.replace(fileExt, '');
57
74
  const realRef = `${originalFileName}Bundled`;
58
- const runtimeFile = filePath.replace(
75
+ const bundledFile = filePath.replace(
59
76
  new RegExp(`${originalBaseName}\$`.replace('.', '\\.'), ''),
60
77
  `${realRef}${fileExt}`
61
78
  );
62
- const isExist = await exists(runtimeFile);
63
- if (isExist === false) {
64
- return {
65
- contents: originalContent,
66
- loader,
67
- };
79
+ const [, error] = await FileSafe.exist(bundledFile);
80
+ if (error) {
81
+ const originalContent = (await readFile(filePath)).toString('utf-8');
82
+ ret.contents = removeVivthDevCodeBlock(originalContent);
83
+ return ret;
68
84
  }
69
- const contents = (await readFile(runtimeFile)).toString('utf-8');
70
- return {
71
- contents,
72
- loader,
73
- };
85
+ const originalContent = (await readFile(bundledFile)).toString('utf-8');
86
+ ret.contents = removeVivthDevCodeBlock(originalContent);
87
+ return ret;
74
88
  });
75
89
  });
76
90
  }
@@ -2,4 +2,6 @@
2
2
 
3
3
  import { ToBundledJSPlugin } from './ToBundledJSPlugin.mjs';
4
4
 
5
- export const pluginVivthBundle = ToBundledJSPlugin('/vivth/src/');
5
+ const path = '/vivth/src/';
6
+
7
+ export const pluginVivthBundle = ToBundledJSPlugin(path);
@@ -135,17 +135,20 @@ export class EventSignal {
135
135
  * @description
136
136
  * - remove subscriber from the named `EventSignal_instance`;
137
137
  * @param {string} name
138
- * @param {import('./Effect.mjs').Effect} effect
138
+ * @param {import('./Effect.mjs').Effect} effectInstance
139
139
  * @returns {void}
140
140
  * @example
141
141
  * import { EventSignal } from 'vivth';
142
142
  *
143
143
  * EventSignal.remove.subscriber('yourEventSignalName', myEffectInstance);
144
144
  */
145
- subscriber: (name, effect) => {
146
- effect.options.removeEffect();
145
+ subscriber: (name, effectInstance) => {
146
+ /**
147
+ * this part is not needed, as the effect might need to react to other signals
148
+ // effectInstance.options.removeEffect();
149
+ */
147
150
  EventSignal.get(name).then(({ remove }) => {
148
- remove.subscriber(effect);
151
+ remove.subscriber(effectInstance);
149
152
  });
150
153
  },
151
154
  /**
@@ -186,7 +189,7 @@ export class EventSignal {
186
189
  * @instance remove
187
190
  * @description
188
191
  * - remove subscriber from the `EventSignal_instance`;
189
- * @param {import('./Effect.mjs').Effect} effect
192
+ * @param {import('./Effect.mjs').Effect} effectInstance
190
193
  * @returns {void}
191
194
  * @example
192
195
  * import { EventSignal, Effect, Console } from 'vivth';
@@ -200,10 +203,13 @@ export class EventSignal {
200
203
  * myEventSignal.dispatch.value = 'hey';
201
204
  * eventSignal_instance.remove.subscriber(myEffectInstance);
202
205
  */
203
- subscriber: (effect) => {
204
- effect.options.removeEffect();
205
- this.dispatch.remove.subscriber(effect);
206
- this.listen.remove.subscriber(effect);
206
+ subscriber: (effectInstance) => {
207
+ /**
208
+ * this part is not needed, as the effect might need to react to other signals
209
+ // effectInstance.options.removeEffect();
210
+ */
211
+ this.dispatch.remove.subscriber(effectInstance);
212
+ this.listen.remove.subscriber(effectInstance);
207
213
  },
208
214
  /**
209
215
  * @instance remove