native-document 1.0.57 → 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 +2 -2
- package/package.json +1 -1
- package/src/{hrm → devtools/hrm}/ComponentRegistry.js +14 -17
- package/src/devtools/hrm/hrm.orbservable.hook.template.js +58 -0
- package/src/devtools/hrm/transformComponent.js +129 -0
- package/src/hrm/transformComponent.js +0 -73
- /package/src/{hrm → devtools/hrm}/hrm.hook.template.js +0 -0
- /package/src/{hrm → devtools/hrm}/nd-vite-hot-reload.js +0 -0
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,33 +1,31 @@
|
|
|
1
|
-
import {Anchor} from "
|
|
1
|
+
import {Anchor} from "../../../elements.js";
|
|
2
2
|
|
|
3
3
|
const ComponentRegistry = (function() {
|
|
4
|
-
console.log('ça s excecute')
|
|
5
|
-
|
|
6
4
|
const registry = new Map();
|
|
7
5
|
|
|
8
6
|
const wrapper = function(id, factory, metadata, registryItem) {
|
|
9
7
|
const factoryName = factory.name;
|
|
10
8
|
|
|
11
9
|
return function(...args) {
|
|
12
|
-
const
|
|
13
|
-
console.log(
|
|
14
|
-
if(
|
|
15
|
-
const instance =
|
|
16
|
-
const
|
|
17
|
-
const newInstance = factory(...componentArgs);
|
|
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);
|
|
18
15
|
instance.anchor.setContent(newInstance);
|
|
19
16
|
return;
|
|
20
17
|
}
|
|
21
|
-
const instance = factory(...args);
|
|
22
18
|
const anchor = Anchor(factoryName);
|
|
23
|
-
|
|
24
|
-
registryItem.instances.add({
|
|
19
|
+
const instance = {
|
|
25
20
|
anchor,
|
|
26
21
|
context: {
|
|
27
|
-
args
|
|
22
|
+
args,
|
|
23
|
+
states: new Map()
|
|
28
24
|
}
|
|
29
|
-
}
|
|
30
|
-
|
|
25
|
+
};
|
|
26
|
+
const factoryInstance = factory(...args, instance);
|
|
27
|
+
anchor.setContent(factoryInstance);
|
|
28
|
+
registryItem.instances.add(instance);
|
|
31
29
|
return anchor;
|
|
32
30
|
};
|
|
33
31
|
}
|
|
@@ -74,9 +72,8 @@ const ComponentRegistry = (function() {
|
|
|
74
72
|
}
|
|
75
73
|
},
|
|
76
74
|
updateInstance(instance, newFactory) {
|
|
77
|
-
console.log(Array.from(registry.entries()))
|
|
78
75
|
return newFactory({ __instance: instance });
|
|
79
|
-
}
|
|
76
|
+
},
|
|
80
77
|
};
|
|
81
78
|
}());
|
|
82
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,73 +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
|
-
console.log(traverse)
|
|
28
|
-
traverse.default(codeParsed, {
|
|
29
|
-
ExportDefaultDeclaration(path) {
|
|
30
|
-
hasDefaultExport = true;
|
|
31
|
-
const declaration = path.node.declaration;
|
|
32
|
-
|
|
33
|
-
if (declaration.id) {
|
|
34
|
-
componentName = declaration.id.name;
|
|
35
|
-
} else if (declaration.type === 'Identifier') {
|
|
36
|
-
componentName = declaration.name;
|
|
37
|
-
} else {
|
|
38
|
-
componentName = 'AnonymousComponent';
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
exportStart = path.node.start;
|
|
42
|
-
exportEnd = path.node.end;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
if (!hasDefaultExport) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const originalExport = code.slice(exportStart, exportEnd);
|
|
50
|
-
const hrmComponentName = `__HRM_${componentName}__`;
|
|
51
|
-
const newExport = originalExport.replace(
|
|
52
|
-
'export default',
|
|
53
|
-
`const ${hrmComponentName} =`
|
|
54
|
-
);
|
|
55
|
-
s.overwrite(exportStart, exportEnd, newExport);
|
|
56
|
-
|
|
57
|
-
const data = {
|
|
58
|
-
id: id,
|
|
59
|
-
};
|
|
60
|
-
let hrmHookTemplateFormatted = hrmHookTemplate;
|
|
61
|
-
for(const key in data) {
|
|
62
|
-
hrmHookTemplateFormatted = hrmHookTemplateFormatted.replace(new RegExp("\\$\{"+key+"}", 'ig'), data[key]);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
s.prepend('import ComponentRegistry from "native-document/src/hrm/ComponentRegistry";');
|
|
66
|
-
s.append(`export default ComponentRegistry.register('${id}', ${hrmComponentName}, { preserveState: ${options.preserveState} });`);
|
|
67
|
-
s.append(hrmHookTemplateFormatted);
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
code: s.toString(),
|
|
71
|
-
map: s.generateMap({ source: id, hires: true })
|
|
72
|
-
};
|
|
73
|
-
}
|
|
File without changes
|
|
File without changes
|