native-document 1.0.58 → 1.0.59

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/hrm.js CHANGED
@@ -1,5 +1,5 @@
1
- import NdViteHotReload from "./src/hrm/nd-vite-hot-reload.js";
2
- import transformComponent from "./src/hrm/transformComponent.js";
1
+ import NdViteHotReload from "./src/devtools/hrm/nd-vite-hot-reload.js";
2
+ import transformComponent from "./src/devtools/hrm/transformComponent.js";
3
3
 
4
4
  export {
5
5
  transformComponent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.58",
3
+ "version": "1.0.59",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import {Anchor} from "../../elements.js";
1
+ import {Anchor} from "../../../elements.js";
2
2
 
3
3
  const ComponentRegistry = (function() {
4
4
  const registry = new Map();
@@ -7,22 +7,25 @@ const ComponentRegistry = (function() {
7
7
  const factoryName = factory.name;
8
8
 
9
9
  return function(...args) {
10
- const lastParams = args[0];
11
- if(lastParams?.__instance) {
12
- const instance = lastParams.__instance;
13
- const newInstance = factory(...instance.context.args);
10
+ const firstParam = args[0];
11
+ console.log( args)
12
+ if(firstParam?.__instance) {
13
+ const instance = firstParam.__instance;
14
+ const newInstance = factory(...instance.context.args, instance);
14
15
  instance.anchor.setContent(newInstance);
15
16
  return;
16
17
  }
17
- const instance = factory(...args);
18
18
  const anchor = Anchor(factoryName);
19
- anchor.setContent(instance);
20
- registryItem.instances.add({
19
+ const instance = {
21
20
  anchor,
22
21
  context: {
23
- args
22
+ args,
23
+ states: new Map()
24
24
  }
25
- });
25
+ };
26
+ const factoryInstance = factory(...args, instance);
27
+ anchor.setContent(factoryInstance);
28
+ registryItem.instances.add(instance);
26
29
  return anchor;
27
30
  };
28
31
  }
@@ -70,7 +73,7 @@ const ComponentRegistry = (function() {
70
73
  },
71
74
  updateInstance(instance, newFactory) {
72
75
  return newFactory({ __instance: instance });
73
- }
76
+ },
74
77
  };
75
78
  }());
76
79
 
@@ -0,0 +1,58 @@
1
+
2
+ function Observable(name, instance, value, ...args) {
3
+ if(instance.context.states.has(name)) {
4
+ const item = instance.context.states.get(name);
5
+ if(item.value !== value) {
6
+ item.value = value;
7
+ item.observable.set(value);
8
+ }
9
+ return item.observable;
10
+ }
11
+
12
+ const observable = __OriginalObservable__(value, ...args);
13
+ instance.context.states.set(name, {
14
+ observable,
15
+ value
16
+ });
17
+ return observable;
18
+ };
19
+
20
+ Observable.init = function(name, instance, value, ...args) {
21
+ if(instance.context.states.has(name)) {
22
+ const item = instance.context.states.get(name);
23
+
24
+ if(item.value !== JSON.stringify(value)) {
25
+ item.value = value;
26
+ item.observable.set(value);
27
+ }
28
+
29
+ return item.observable;
30
+ }
31
+ const item = __OriginalObservable__.init(value, ...args);
32
+ instance.context.states.set(name, {
33
+ item,
34
+ value: JSON.stringify(value)
35
+ });
36
+ return item;
37
+ };
38
+
39
+ Observable.array = function(name, instance, value, ...args) {
40
+ if(instance.context.states.has(name)) {
41
+ const item = instance.context.states.get(name);
42
+ if(item.value !== JSON.stringify(value)) {
43
+ item.value = value;
44
+ item.observable.set(value);
45
+ }
46
+ return item.observable;
47
+ }
48
+ const item = __OriginalObservable__.array(value, ...args);
49
+ instance.context.states.set(name, {
50
+ item,
51
+ value: JSON.stringify(value)
52
+ });
53
+ return item;
54
+ };
55
+
56
+ Observable.object = Observable.init;
57
+ Observable.json = Observable.init;
58
+ const $ = Observable;
@@ -0,0 +1,129 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ // import { parse } from '@babel/parser';
4
+ import MagicString from 'magic-string';
5
+
6
+ import { fileURLToPath } from 'node:url';
7
+ import { parse } from '@babel/parser';
8
+ import { default as traverse } from '@babel/traverse';
9
+
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = path.dirname(__filename);
12
+
13
+ const renameImportedObservables = (content, match) => {
14
+ let isObservableFound = false;
15
+ let isObservableShortFound = false;
16
+ const transformedContentArray = match.split(',').map(item => {
17
+ let trimmed = item.trim();
18
+
19
+ if (trimmed === 'Observable') {
20
+ isObservableFound = true;
21
+ return 'Observable as __OriginalObservable__';
22
+ }
23
+ if (trimmed === '$') {
24
+ isObservableShortFound = true;
25
+ return '$ as __$__';
26
+ }
27
+
28
+ return ` ${trimmed}`;
29
+ });
30
+ if(!isObservableFound && isObservableShortFound) {
31
+ transformedContentArray.push('Observable as __OriginalObservable__')
32
+ }
33
+ const transformedContent = transformedContentArray.join(', ');
34
+
35
+ return `import {${transformedContent} } from 'native-document'`;
36
+ };
37
+
38
+ function transformObservableImports(code, params) {
39
+ const renameImportationCode = code.replace(/import\s+\{([^}]+?)\}\s+from\s+['"]native-document['"]/g, renameImportedObservables);
40
+ const isRenamed = renameImportationCode !== code;
41
+
42
+ const s = new MagicString(renameImportationCode);
43
+ if(isRenamed) {
44
+ //Todo: move this code outside the function
45
+ const hrmObservableHookTemplate = fs.readFileSync(__dirname + '/hrm.orbservable.hook.template.js', 'utf8');
46
+ s.append(template(
47
+ hrmObservableHookTemplate,
48
+ params ?? {}
49
+ ));
50
+ }
51
+
52
+ return s.toString();
53
+ }
54
+
55
+ function transformObservableDeclarations(code) {
56
+ const regex = /const\s+(\w+)\s*=\s*(\$|Observable)(\.(?:init|array|json))?\s*\(/g;
57
+
58
+ return code.replace(regex, (match, varName, caller, method) => {
59
+ const obsName = method ? `${caller}${method}` : 'Observable';
60
+ return `console.log(arguments); const ${varName} = ${obsName}('${varName}', arguments[arguments.length - 1], `;
61
+ });
62
+ }
63
+
64
+ function template(code, params) {
65
+ let codeFormatted = code;
66
+ for(const key in params) {
67
+ codeFormatted = codeFormatted.replace(new RegExp("\\$\{"+key+"}", 'ig'), params[key]);
68
+ }
69
+ return codeFormatted;
70
+ }
71
+
72
+ export default function transformComponent(id, code, options) {
73
+ let hasDefaultExport = false;
74
+ let componentName = null;
75
+ let exportStart = 0;
76
+ let exportEnd = 0;
77
+ // TODO: move this line outside the function
78
+ const hrmHookTemplate = fs.readFileSync(__dirname + '/hrm.hook.template.js', 'utf8');
79
+ const formattedCode = transformObservableDeclarations(
80
+ transformObservableImports(code, { id, ...options }),
81
+ );
82
+ const s = new MagicString(formattedCode);
83
+ const codeParsed = parse(formattedCode, {
84
+ sourceType: 'module',
85
+ plugins: []
86
+ });
87
+
88
+ traverse.default(codeParsed, {
89
+ ExportDefaultDeclaration(path) {
90
+ hasDefaultExport = true;
91
+ const declaration = path.node.declaration;
92
+
93
+ if (declaration.id) {
94
+ componentName = declaration.id.name;
95
+ } else if (declaration.type === 'Identifier') {
96
+ componentName = declaration.name;
97
+ } else {
98
+ componentName = 'AnonymousComponent';
99
+ }
100
+
101
+ exportStart = path.node.start;
102
+ exportEnd = path.node.end;
103
+ },
104
+ });
105
+ if (!hasDefaultExport) {
106
+ return null;
107
+ }
108
+
109
+ const originalExport = formattedCode.slice(exportStart, exportEnd);
110
+ const hrmComponentName = `__HRM_${componentName}__`;
111
+ const newExport = originalExport.replace(
112
+ 'export default',
113
+ `const ${hrmComponentName} =`
114
+ );
115
+ s.overwrite(exportStart, exportEnd, newExport);
116
+
117
+ const hrmHookTemplateFormatted = template(hrmHookTemplate, {
118
+ id
119
+ });
120
+
121
+ s.prepend('import ComponentRegistry from "native-document/src/hrm/ComponentRegistry";');
122
+ s.append(`export default ComponentRegistry.register('${id}', ${hrmComponentName}, { preserveState: ${options.preserveState} });`);
123
+ s.append(hrmHookTemplateFormatted);
124
+
125
+ return {
126
+ code: s.toString(),
127
+ map: s.generateMap({ source: id, hires: true })
128
+ };
129
+ }
@@ -1,72 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- // import { parse } from '@babel/parser';
4
- import MagicString from 'magic-string';
5
-
6
- import { fileURLToPath } from 'node:url';
7
- import { parse } from '@babel/parser';
8
- import { default as traverse } from '@babel/traverse';
9
-
10
- const __filename = fileURLToPath(import.meta.url);
11
- const __dirname = path.dirname(__filename);
12
-
13
-
14
- export default function transformComponent(id, code, options) {
15
- let hasDefaultExport = false;
16
- let componentName = null;
17
- let exportStart = 0;
18
- let exportEnd = 0;
19
- // TODO: move this line outside the function
20
- const hrmHookTemplate = fs.readFileSync(__dirname + '/hrm.hook.template.js', 'utf8');
21
- const s = new MagicString(code);
22
- const codeParsed = parse(code, {
23
- sourceType: 'module',
24
- plugins: []
25
- });
26
-
27
- traverse.default(codeParsed, {
28
- ExportDefaultDeclaration(path) {
29
- hasDefaultExport = true;
30
- const declaration = path.node.declaration;
31
-
32
- if (declaration.id) {
33
- componentName = declaration.id.name;
34
- } else if (declaration.type === 'Identifier') {
35
- componentName = declaration.name;
36
- } else {
37
- componentName = 'AnonymousComponent';
38
- }
39
-
40
- exportStart = path.node.start;
41
- exportEnd = path.node.end;
42
- }
43
- });
44
- if (!hasDefaultExport) {
45
- return null;
46
- }
47
-
48
- const originalExport = code.slice(exportStart, exportEnd);
49
- const hrmComponentName = `__HRM_${componentName}__`;
50
- const newExport = originalExport.replace(
51
- 'export default',
52
- `const ${hrmComponentName} =`
53
- );
54
- s.overwrite(exportStart, exportEnd, newExport);
55
-
56
- const data = {
57
- id: id,
58
- };
59
- let hrmHookTemplateFormatted = hrmHookTemplate;
60
- for(const key in data) {
61
- hrmHookTemplateFormatted = hrmHookTemplateFormatted.replace(new RegExp("\\$\{"+key+"}", 'ig'), data[key]);
62
- }
63
-
64
- s.prepend('import ComponentRegistry from "native-document/src/hrm/ComponentRegistry";');
65
- s.append(`export default ComponentRegistry.register('${id}', ${hrmComponentName}, { preserveState: ${options.preserveState} });`);
66
- s.append(hrmHookTemplateFormatted);
67
-
68
- return {
69
- code: s.toString(),
70
- map: s.generateMap({ source: id, hires: true })
71
- };
72
- }
File without changes
File without changes