1
1
import {
2
2
UnsupportedSourceError ,
3
3
ItemsLoader ,
4
+ PathLoader ,
4
5
} from "@aedart/config" ;
5
6
import makeLoaderFactory from "../helpers/makeLoaderFactory" ;
6
7
@@ -30,9 +31,50 @@ describe('@aedart/config', () => {
30
31
. toThrowError ( UnsupportedSourceError ) ;
31
32
} ) ;
32
33
33
- it ( 'can make loaders and load items' , async ( ) => {
34
+ xit ( 'can make loaders and load items' , async ( ) => {
35
+
36
+ // ------------------------------------------------------------------------------------ //
37
+
38
+ console . info ( 'FILE URL' , import . meta. url ) ;
39
+ console . info ( 'FILE URL # 2' , ( new URL ( '../fixtures/my-config.js' , import . meta. url ) ) . toString ( ) ) ;
40
+ console . info ( 'FILE URL # 3' , ( new URL ( '../fixtures/my-config.js' , import . meta. url ) ) . href ) ;
41
+ console . info ( 'FILE URL # 4' , ( new URL ( '../fixtures/my-config.js' , import . meta. url ) ) . pathname ) ;
42
+
43
+ class MyLoader {
44
+ async load ( path ) {
45
+ try {
46
+ return await import ( path ) . default ;
47
+ } catch ( e ) {
48
+ throw new Error ( `Unable to load: ${ e . message } ` ) ;
49
+ }
50
+ }
51
+ }
34
52
35
- const factory = makeLoaderFactory ( ) ;
53
+ const fn = async ( p ) => {
54
+ const c = await import ( p ) ;
55
+ return c ?. default ;
56
+ }
57
+
58
+ // TODO: WORKS
59
+ const raw = ( await import ( '../fixtures/my-config.js' ) ) . default ;
60
+ console . log ( 'Raw import' , raw ) ;
61
+
62
+ // TODO:
63
+ const viaFn = await fn ( '../fixtures/my-config.js' ) ;
64
+ console . log ( 'Fn import' , viaFn ) ;
65
+
66
+ // const tmp = await (new MyLoader()).load('../fixtures/my-config.js');
67
+ // const tmp = await (new MyLoader()).load((new URL('../fixtures/my-config.js', import.meta.url)).href);
68
+ // const tmp = await (new MyLoader()).load(
69
+ // 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href
70
+ // );
71
+ // const tmp = await (new MyLoader()).load(
72
+ // (new URL('../fixtures/my-config.js', import.meta.url)).toString()
73
+ // );
74
+ const tmp = await ( new MyLoader ( ) ) . load (
75
+ ( new URL ( '../fixtures/my-config.js' , import . meta. url ) )
76
+ ) ;
77
+ console . log ( 'LOADED via MyLoader' , tmp ) ;
36
78
37
79
const data = [
38
80
{
@@ -42,10 +84,32 @@ describe('@aedart/config', () => {
42
84
} ,
43
85
loader : ItemsLoader ,
44
86
expected : { foo : 'bar' }
87
+ } ,
88
+ {
89
+ name : 'PathLoader' ,
90
+ //source: '../fixtures/my-config.js', // Relative does not seem to work here...
91
+ // source: path.resolve('../fixtures/my-config.js'),
92
+ // source: import.meta.url + '/../fixtures/my-config.js',
93
+ // source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js',
94
+ //source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js',
95
+ // source: (new URL('../fixtures/my-config.js', import.meta.url)).toString(),
96
+
97
+ // source: (new URL('../fixtures/my-config.js', import.meta.url)).href,
98
+
99
+ //source: 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href,
100
+
101
+ source : ( new URL ( '../fixtures/my-config.js' , import . meta. url ) ) . href ,
102
+ loader : PathLoader ,
103
+ expected : {
104
+ app : {
105
+ name : 'Foo'
106
+ }
107
+ }
45
108
}
46
109
] ;
47
110
48
111
// ------------------------------------------------------------------------------------ //
112
+ const factory = makeLoaderFactory ( ) ;
49
113
50
114
for ( const entry of data ) {
51
115
0 commit comments