-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathimport.js
More file actions
159 lines (149 loc) · 4.16 KB
/
import.js
File metadata and controls
159 lines (149 loc) · 4.16 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
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
/**
* Provides functions for evaluating a module and its transitive
* dependencies given the URL of the entry module and assuming packages laid
* out according to the `node_modules` conventions.
*
* To import modules according to any other convention, use `import-lite.js`
* and provide a compartment map with a custom analog to `mapNodeModules` from
* `node-modules.js`.
*
* The default `parserForLanguage` is `import-parsers.js`, which is suitable
* for most cases.
*
* @module
*/
/**
* @import {
* Application,
* SyncImportLocationOptions,
* ImportLocationOptions,
* LoadLocationOptions,
* SomeObject,
* ReadNowPowers,
* ReadFn,
* ReadPowers,
SyncLoadLocationOptions,
* } from './types.js'
*/
import { loadFromMap } from './import-lite.js';
import { defaultParserForLanguage } from './import-parsers.js';
import { mapNodeModules } from './node-modules.js';
const { assign, create, freeze } = Object;
/**
* Add the default parserForLanguage option.
* @param {LoadLocationOptions} [options]
* @returns {LoadLocationOptions}
*/
const assignParserForLanguage = (options = {}) => {
const { parserForLanguage: parserForLanguageOption, ...rest } = options;
const parserForLanguage = freeze(
assign(create(null), defaultParserForLanguage, parserForLanguageOption),
);
const languages = Object.keys(parserForLanguage);
return { ...rest, parserForLanguage, languages };
};
/**
* @overload
* @param {ReadNowPowers} readPowers
* @param {string} moduleLocation
* @param {SyncLoadLocationOptions} options
* @returns {Promise<Application>}
*/
/**
* @overload
* @param {ReadFn | ReadPowers} readPowers
* @param {string} moduleLocation
* @param {LoadLocationOptions} [options]
* @returns {Promise<Application>}
*/
/**
* @param {ReadFn|ReadPowers|ReadNowPowers} readPowers
* @param {string} moduleLocation
* @param {LoadLocationOptions} [options]
* @returns {Promise<Application>}
*/
export const loadLocation = async (
readPowers,
moduleLocation,
options = {},
) => {
const {
dev,
tags,
strict,
commonDependencies,
policy,
parserForLanguage,
log,
languages,
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
unknownCanonicalNameHook,
packageDataHook,
packageDependenciesHook,
moduleSourceHook,
...otherOptions
} = assignParserForLanguage(options);
// conditions are not present in SyncArchiveOptions
const conditions =
'conditions' in options ? options.conditions || tags : tags;
const compartmentMap = await mapNodeModules(readPowers, moduleLocation, {
dev,
strict,
conditions,
commonDependencies,
policy,
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
languages,
log,
unknownCanonicalNameHook,
packageDataHook,
packageDependenciesHook,
});
return loadFromMap(readPowers, compartmentMap, {
parserForLanguage,
log,
moduleSourceHook,
...otherOptions,
});
};
/**
* Allows dynamic requires
*
* @overload
* @param {ReadNowPowers} readPowers
* @param {string} moduleLocation
* @param {SyncImportLocationOptions} options
* @returns {Promise<SomeObject>} the object of the imported modules exported
* names.
*/
/**
* Disallows dynamic requires
*
* @overload
* @param {ReadPowers|ReadFn} readPowers
* @param {string} moduleLocation
* @param {ImportLocationOptions} [options]
* @returns {Promise<SomeObject>} the object of the imported modules exported
* names.
*/
/**
* @param {ReadPowers|ReadFn|ReadNowPowers} readPowers
* @param {string} moduleLocation
* @param {ImportLocationOptions|SyncImportLocationOptions} [options]
* @returns {Promise<SomeObject>} the object of the imported modules exported
* names.
*/
export const importLocation = async (readPowers, moduleLocation, options) => {
const application = await loadLocation(readPowers, moduleLocation, options);
return application.import(options);
};