native-document 1.0.55 → 1.0.56
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "native-document",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.56",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -12,10 +12,13 @@
|
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"description": "",
|
|
14
14
|
"devDependencies": {
|
|
15
|
+
"@babel/parser": "^7.28.5",
|
|
16
|
+
"@babel/traverse": "^7.28.5",
|
|
15
17
|
"@rollup/plugin-alias": "^5.1.1",
|
|
16
18
|
"@rollup/plugin-replace": "^6.0.2",
|
|
17
19
|
"@rollup/plugin-terser": "^0.4.4",
|
|
18
20
|
"eslint": "^9.33.0",
|
|
21
|
+
"magic-string": "^0.30.21",
|
|
19
22
|
"rollup": "^4.53.3"
|
|
20
23
|
}
|
|
21
|
-
}
|
|
24
|
+
}
|
|
@@ -4,14 +4,55 @@ import path from 'node:path';
|
|
|
4
4
|
import MagicString from 'magic-string';
|
|
5
5
|
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { parse } from '@babel/parser';
|
|
8
|
+
import { default as traverse } from '@babel/traverse';
|
|
7
9
|
|
|
8
10
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
11
|
const __dirname = path.dirname(__filename);
|
|
10
12
|
|
|
13
|
+
|
|
11
14
|
export default function transformComponent(id, code) {
|
|
15
|
+
let hasDefaultExport = false;
|
|
16
|
+
let componentName = null;
|
|
17
|
+
let exportStart = 0;
|
|
18
|
+
let exportEnd = 0;
|
|
12
19
|
// TODO: move this line outside the function
|
|
13
20
|
const hrmHookTemplate = fs.readFileSync(__dirname + '/hrm.hook.template.js', 'utf8');
|
|
14
21
|
const s = new MagicString('');
|
|
22
|
+
const codeParsed = parse(code, {
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
plugins: []
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
console.log(traverse)
|
|
28
|
+
traverse(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);
|
|
15
56
|
|
|
16
57
|
const data = {
|
|
17
58
|
id: id,
|
|
@@ -23,6 +64,7 @@ export default function transformComponent(id, code) {
|
|
|
23
64
|
|
|
24
65
|
s.append('import ComponentRegistry from "native-document/src/hrm/ComponentRegistry";');
|
|
25
66
|
s.append(code);
|
|
67
|
+
s.append(`export default ComponentRegistry.register('${id}', ${hrmComponentName}, { preserveState: ${options.preserveState} });`);
|
|
26
68
|
s.append(hrmHookTemplateFormatted);
|
|
27
69
|
|
|
28
70
|
return {
|