-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathindex.ts
199 lines (165 loc) · 4.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
posix as Path
} from 'path'
import {
loadModuleInternal
} from './tools'
import {
ModuleExport,
PathResolve,
Options,
File,
Resource,
PathContext,
LangProcessor,
AbstractPath,
} from './types'
export * from './types'
/**
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
*/
export const version : string = process.env.VERSION;
/**
* the version of Vue that is expected by the library
*/
export { vueVersion } from './createSFCModule'
export { createCJSModule } from './tools'
/**
* @internal
*/
function throwNotDefined(details : string) : never {
throw new ReferenceError(`${ details } is not defined`);
}
/**
* Default resolve implementation
* resolve() should handle 3 situations :
* - resolve a relative path ( eg. import './details.vue' )
* - resolve an absolute path ( eg. import '/components/card.vue' )
* - resolve a module name ( eg. import { format } from 'date-fns' )
*/
const defaultPathResolve : PathResolve = ({ refPath, relPath } : PathContext) => {
// initial resolution: refPath is not defined
if ( refPath === undefined )
return relPath;
const relPathStr = relPath.toString();
// is non-relative path ?
if ( relPathStr[0] !== '.' )
return relPath;
// note :
// normalize('./test') -> 'test'
// normalize('/test') -> '/test'
return Path.normalize(Path.join(Path.dirname(refPath.toString()), relPathStr));
}
/**
* Default getResource implementation
* by default, getContent() use the file extension as file type.
*/
function defaultGetResource(pathCx : PathContext, options : Options) : Resource {
const { pathResolve, getFile, log } = options;
const path = pathResolve(pathCx);
const pathStr = path.toString();
return {
id: pathStr,
path: path,
getContent: async () => {
const res = await getFile(path);
if ( typeof res === 'string' || res instanceof ArrayBuffer ) {
return {
type: Path.extname(pathStr),
getContentData: async (asBinary) => {
if ( res instanceof ArrayBuffer !== asBinary )
log?.('warn', `unexpected data type. ${ asBinary ? 'binary' : 'string' } is expected for "${ path }"`);
return res;
},
}
}
return {
type: res.type ?? Path.extname(pathStr),
getContentData: res.getContentData,
}
}
};
}
/**
* This is the main function.
* This function is intended to be used only to load the entry point of your application.
* If for some reason you need to use it in your components, be sure to share at least the options.`compiledCache` object between all calls.
*
* @param path The path of the `.vue` file. If path is not a path (eg. an string ID), your [[getFile]] function must return a [[File]] object.
* @param options The options
* @returns A Promise of the component
*
* **example using `Vue.defineAsyncComponent`:**
*
* ```javascript
*
* const app = Vue.createApp({
* components: {
* 'my-component': Vue.defineAsyncComponent( () => loadModule('./myComponent.vue', options) )
* },
* template: '<my-component></my-component>'
* });
*
* ```
*
* **example using `await`:**
*
* ```javascript
* ;(async () => {
*
* const app = Vue.createApp({
* components: {
* 'my-component': await loadModule('./myComponent.vue', options)
* },
* template: '<my-component></my-component>'
* });
*
* })()
* .catch(ex => console.error(ex));
*
* ```
*
*/
export async function loadModule(path : AbstractPath, options : Options = throwNotDefined('options')) : Promise<ModuleExport> {
const {
moduleCache = throwNotDefined('options.moduleCache'),
getFile = throwNotDefined('options.getFile()'),
addStyle = throwNotDefined('options.addStyle()'),
pathResolve = defaultPathResolve,
getResource = defaultGetResource,
} = options;
// moduleCache should be defined with Object.create(null). require('constructor') etc... should not be a default module
if ( moduleCache instanceof Object )
Object.setPrototypeOf(moduleCache, null);
const normalizedOptions = {
moduleCache,
pathResolve,
getResource,
...options,
};
return await loadModuleInternal( { refPath: undefined, relPath: path }, normalizedOptions);
}
/**
* Convert a function to template processor interface (consolidate)
*/
export function buildTemplateProcessor(processor: LangProcessor) {
return {
render: (source: string, preprocessOptions: string, cb: (_err : any, _res : any) => void) => {
try {
const ret = processor(source, preprocessOptions)
if (typeof ret === 'string') {
cb(null, ret)
} else {
ret.then(processed => {
cb(null, processed)
})
ret.catch(err => {
cb(err, null)
})
}
} catch (err) {
cb(err, null)
}
}
}
}