forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_import_meta.js
More file actions
109 lines (97 loc) · 3.27 KB
/
initialize_import_meta.js
File metadata and controls
109 lines (97 loc) · 3.27 KB
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
'use strict';
const {
ObjectDefineProperties,
ObjectDefineProperty,
StringPrototypeStartsWith,
} = primordials;
const { getOptionValue } = require('internal/options');
const { fileURLToPath } = require('internal/url');
const { dirname } = require('path');
const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve');
/**
* Generate a function to be used as import.meta.resolve for a particular module.
* @param {string} defaultParentURL The default base to use for resolution
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
* @param {bool} allowParentURL Whether to permit parentURL second argument for contextual resolution
* @returns {(specifier: string) => string} Function to assign to import.meta.resolve
*/
function createImportMetaResolve(defaultParentURL, loader, allowParentURL) {
/**
* @param {string} specifier
* @param {URL['href']} [parentURL] When `--experimental-import-meta-resolve` is specified, a
* second argument can be provided.
*/
return function resolve(specifier, parentURL = defaultParentURL) {
let url;
if (!allowParentURL) {
parentURL = defaultParentURL;
}
try {
({ url } = loader.resolveSync(specifier, parentURL));
return url;
} catch (error) {
switch (error?.code) {
case 'ERR_UNSUPPORTED_DIR_IMPORT':
case 'ERR_MODULE_NOT_FOUND':
({ url } = error);
if (url) {
return url;
}
}
throw error;
}
};
}
/**
* Create the `import.meta` object for a module.
* @param {object} meta
* @param {{url: string}} context
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
* @returns {{dirname?: string, filename?: string, url: string, resolve?: Function}}
*/
function initializeImportMeta(meta, context, loader) {
const { url } = context;
// Alphabetical
if (StringPrototypeStartsWith(url, 'file:') === true) {
// These only make sense for locally loaded modules,
// i.e. network modules are not supported.
// Avoid paying the cost to derive these unless they're actually accessed.
ObjectDefineProperties(meta, {
dirname: {
get() {
const value = dirname(this.filename);
ObjectDefineProperty(this, 'dirname', { __proto__: null, value, writable: true });
return value;
},
set(value) {
ObjectDefineProperty(this, 'dirname', { __proto__: null, value, writable: true });
},
configurable: true,
enumerable: true,
__proto__: null,
},
filename: {
get() {
const value = fileURLToPath(url);
ObjectDefineProperty(this, 'filename', { __proto__: null, value, writable: true });
return value;
},
set(value) {
ObjectDefineProperty(this, 'filename', { __proto__: null, value, writable: true });
},
configurable: true,
enumerable: true,
__proto__: null,
},
__proto__: null,
});
}
if (!loader || loader.allowImportMetaResolve) {
meta.resolve = createImportMetaResolve(url, loader, experimentalImportMetaResolve);
}
meta.url = url;
return meta;
}
module.exports = {
initializeImportMeta,
};