1
1
/** quotes strings */
2
2
const q = JSON . stringify ;
3
3
4
- const exportsCellRecord = exportMap =>
5
- '' . concat (
6
- ...Object . keys ( exportMap ) . map (
7
- exportName => `\
8
- ${ exportName } : cell(${ q ( exportName ) } ),
9
- ` ,
10
- ) ,
11
- ) ;
12
-
13
4
const importsCellSetter = ( exportMap , index ) =>
14
5
'' . concat (
15
6
...Object . entries ( exportMap ) . map (
@@ -19,31 +10,56 @@ const importsCellSetter = (exportMap, index) =>
19
10
) ,
20
11
) ;
21
12
22
- const adaptReexport = reexportMap => {
13
+ const importImplementation = indexedImports => {
14
+ const knownEntries = Object . entries ( indexedImports ) ;
15
+ if ( knownEntries . length === 0 ) {
16
+ return `imports() {},` ;
17
+ }
18
+ return `\
19
+ imports(entries) {
20
+ const map = new Map(entries);
21
+ observeImports(map, [
22
+ ${ '' . concat (
23
+ ...knownEntries . map (
24
+ ( [ importName , importIndex ] ) => `\
25
+ [${ q ( importName ) } , ${ importIndex } ],
26
+ ` ,
27
+ ) ,
28
+ ) } \
29
+ ]);},` ;
30
+ } ;
31
+
32
+ const getReexportKeys = reexportMap => {
23
33
if ( ! reexportMap ) {
24
34
return { } ;
25
35
}
26
- const ret = Object . fromEntries (
27
- Object . values ( reexportMap )
28
- . flat ( )
29
- . map ( ( [ local , exported ] ) => [ exported , [ local ] ] ) ,
30
- ) ;
31
- return ret ;
36
+ return Object . values ( reexportMap )
37
+ . flat ( )
38
+ . map ( ( [ local , _exported ] ) => local ) ;
32
39
} ;
33
40
34
- export const runtime = `\
35
- function observeImports(map, importName, importIndex) {
36
- for (const [name, observers] of map.get(importName)) {
37
- const cell = cells[importIndex][name];
38
- if (cell === undefined) {
39
- throw new ReferenceError(\`Cannot import name \${name}\`);
40
- }
41
- for (const observer of observers) {
42
- cell.observe(observer);
41
+ // vvv runtime to inline in the bundle vvv
42
+ /* eslint-disable no-undef */
43
+ function observeImports ( map , pairs ) {
44
+ for ( const [ importName , importIndex ] of pairs ) {
45
+ for ( const [ name , observers ] of map . get ( importName ) ) {
46
+ const cell = cells [ importIndex ] [ name ] ;
47
+ if ( cell === undefined ) {
48
+ throw new ReferenceError ( `Cannot import name ${ name } ` ) ;
49
+ }
50
+ for ( const observer of observers ) {
51
+ cell . observe ( observer ) ;
52
+ }
43
53
}
44
54
}
45
55
}
46
- ` ;
56
+
57
+ /* eslint-enable no-undef */
58
+
59
+ const runtime = `\
60
+ ${ observeImports }
61
+ ${ observeImports } `;
62
+ // ^^^ runtime to inline in the bundle ^^^
47
63
48
64
export default {
49
65
runtime,
@@ -55,6 +71,7 @@ export default {
55
71
__fixedExportMap__ = { } ,
56
72
__liveExportMap__ = { } ,
57
73
__reexportMap__ = { } ,
74
+ __needsImportMeta__ = false ,
58
75
reexports,
59
76
} ,
60
77
} ) {
@@ -63,19 +80,17 @@ export default {
63
80
// === functors[${ index } ] ===
64
81
${ __syncModuleProgram__ } ,
65
82
` ,
66
- getCells : ( ) => `\
67
- {
68
- ${ exportsCellRecord ( __fixedExportMap__ ) } ${ exportsCellRecord (
69
- __liveExportMap__ ,
70
- ) } ${ exportsCellRecord ( adaptReexport ( __reexportMap__ ) ) } \
71
- },
72
- ` ,
83
+ getCells : ( ) => [
84
+ ...Object . keys ( __fixedExportMap__ ) ,
85
+ ...Object . keys ( __liveExportMap__ ) ,
86
+ ...getReexportKeys ( __reexportMap__ ) ,
87
+ ] ,
88
+ reexportedCells : reexports . map ( importSpecifier => [
89
+ index ,
90
+ indexedImports [ importSpecifier ] ,
91
+ ] ) ,
73
92
getReexportsWiring : ( ) => {
74
- const mappings = reexports . map (
75
- importSpecifier => `\
76
- Object.defineProperties(cells[${ index } ], Object.getOwnPropertyDescriptors(cells[${ indexedImports [ importSpecifier ] } ]));
77
- ` ,
78
- ) ;
93
+ const mappings = [ ] ;
79
94
// Create references for export name as newname
80
95
const namedReexportsToProcess = Object . entries ( __reexportMap__ ) ;
81
96
if ( namedReexportsToProcess . length > 0 ) {
@@ -96,23 +111,14 @@ ${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord(
96
111
} ,
97
112
getFunctorCall : ( ) => `\
98
113
functors[${ index } ]({
99
- imports(entries) {
100
- const map = new Map(entries);
101
- ${ '' . concat (
102
- ...Object . entries ( indexedImports ) . map (
103
- ( [ importName , importIndex ] ) => `\
104
- observeImports(map, ${ q ( importName ) } , ${ importIndex } );
105
- ` ,
106
- ) ,
107
- ) } \
108
- },
114
+ ${ importImplementation ( indexedImports ) }
109
115
liveVar: {
110
116
${ importsCellSetter ( __liveExportMap__ , index ) } \
111
117
},
112
118
onceVar: {
113
119
${ importsCellSetter ( __fixedExportMap__ , index ) } \
114
- },
115
- importMeta: {},
120
+ },\
121
+ ${ __needsImportMeta__ ? '\n importMeta: {},' : '' }
116
122
});
117
123
` ,
118
124
} ;
0 commit comments