bare-module 3.1.6 → 3.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/binding.c +61 -9
- package/index.js +99 -76
- package/package.json +2 -1
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ When importing the package by name, `require('my-package')` will resolve to `<mo
|
|
|
77
77
|
|
|
78
78
|
##### Conditional exports
|
|
79
79
|
|
|
80
|
-
Conditional exports allow packages to provide different exports
|
|
80
|
+
Conditional exports allow packages to provide different exports for different conditions, such as the module format of the importing module:
|
|
81
81
|
|
|
82
82
|
```json
|
|
83
83
|
{
|
package/binding.c
CHANGED
|
@@ -21,6 +21,10 @@ on_static_import (js_env_t *env, js_value_t *specifier, js_value_t *assertions,
|
|
|
21
21
|
|
|
22
22
|
int err;
|
|
23
23
|
|
|
24
|
+
js_handle_scope_t *scope;
|
|
25
|
+
err = js_open_handle_scope(env, &scope);
|
|
26
|
+
assert(err == 0);
|
|
27
|
+
|
|
24
28
|
js_value_t *ctx;
|
|
25
29
|
err = js_get_reference_value(env, context->ctx, &ctx);
|
|
26
30
|
assert(err == 0);
|
|
@@ -36,20 +40,29 @@ on_static_import (js_env_t *env, js_value_t *specifier, js_value_t *assertions,
|
|
|
36
40
|
js_value_t *args[4] = {specifier, assertions};
|
|
37
41
|
|
|
38
42
|
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[2]);
|
|
39
|
-
if (err < 0)
|
|
43
|
+
if (err < 0) goto err;
|
|
40
44
|
|
|
41
45
|
err = js_get_boolean(env, false, &args[3]);
|
|
42
46
|
assert(err == 0);
|
|
43
47
|
|
|
44
48
|
js_value_t *result;
|
|
45
49
|
err = js_call_function(env, ctx, on_import, 4, args, &result);
|
|
46
|
-
if (err < 0)
|
|
50
|
+
if (err < 0) goto err;
|
|
47
51
|
|
|
48
52
|
js_module_t *module;
|
|
49
53
|
err = js_get_value_external(env, result, (void **) &module);
|
|
50
|
-
if (err < 0)
|
|
54
|
+
if (err < 0) goto err;
|
|
55
|
+
|
|
56
|
+
err = js_close_handle_scope(env, scope);
|
|
57
|
+
assert(err == 0);
|
|
51
58
|
|
|
52
59
|
return module;
|
|
60
|
+
|
|
61
|
+
err:
|
|
62
|
+
err = js_close_handle_scope(env, scope);
|
|
63
|
+
assert(err == 0);
|
|
64
|
+
|
|
65
|
+
return NULL;
|
|
53
66
|
}
|
|
54
67
|
|
|
55
68
|
static js_module_t *
|
|
@@ -58,6 +71,10 @@ on_dynamic_import (js_env_t *env, js_value_t *specifier, js_value_t *assertions,
|
|
|
58
71
|
|
|
59
72
|
int err;
|
|
60
73
|
|
|
74
|
+
js_handle_scope_t *scope;
|
|
75
|
+
err = js_open_handle_scope(env, &scope);
|
|
76
|
+
assert(err == 0);
|
|
77
|
+
|
|
61
78
|
js_value_t *ctx;
|
|
62
79
|
err = js_get_reference_value(env, context->ctx, &ctx);
|
|
63
80
|
assert(err == 0);
|
|
@@ -73,13 +90,22 @@ on_dynamic_import (js_env_t *env, js_value_t *specifier, js_value_t *assertions,
|
|
|
73
90
|
|
|
74
91
|
js_value_t *result;
|
|
75
92
|
err = js_call_function(env, ctx, on_import, 4, args, &result);
|
|
76
|
-
if (err < 0)
|
|
93
|
+
if (err < 0) goto err;
|
|
77
94
|
|
|
78
95
|
js_module_t *module;
|
|
79
96
|
err = js_get_value_external(env, result, (void **) &module);
|
|
80
|
-
if (err < 0)
|
|
97
|
+
if (err < 0) goto err;
|
|
98
|
+
|
|
99
|
+
err = js_close_handle_scope(env, scope);
|
|
100
|
+
assert(err == 0);
|
|
81
101
|
|
|
82
102
|
return module;
|
|
103
|
+
|
|
104
|
+
err:
|
|
105
|
+
err = js_close_handle_scope(env, scope);
|
|
106
|
+
assert(err == 0);
|
|
107
|
+
|
|
108
|
+
return NULL;
|
|
83
109
|
}
|
|
84
110
|
|
|
85
111
|
static void
|
|
@@ -88,6 +114,10 @@ on_evaluate (js_env_t *env, js_module_t *module, void *data) {
|
|
|
88
114
|
|
|
89
115
|
int err;
|
|
90
116
|
|
|
117
|
+
js_handle_scope_t *scope;
|
|
118
|
+
err = js_open_handle_scope(env, &scope);
|
|
119
|
+
assert(err == 0);
|
|
120
|
+
|
|
91
121
|
js_value_t *ctx;
|
|
92
122
|
err = js_get_reference_value(env, context->ctx, &ctx);
|
|
93
123
|
assert(err == 0);
|
|
@@ -103,11 +133,20 @@ on_evaluate (js_env_t *env, js_module_t *module, void *data) {
|
|
|
103
133
|
js_value_t *args[1];
|
|
104
134
|
|
|
105
135
|
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
|
|
106
|
-
if (err < 0)
|
|
136
|
+
if (err < 0) goto err;
|
|
107
137
|
|
|
108
138
|
js_value_t *result;
|
|
109
139
|
err = js_call_function(env, ctx, on_evaluate, 1, args, &result);
|
|
110
|
-
if (err < 0)
|
|
140
|
+
if (err < 0) goto err;
|
|
141
|
+
|
|
142
|
+
err = js_close_handle_scope(env, scope);
|
|
143
|
+
assert(err == 0);
|
|
144
|
+
|
|
145
|
+
return;
|
|
146
|
+
|
|
147
|
+
err:
|
|
148
|
+
err = js_close_handle_scope(env, scope);
|
|
149
|
+
assert(err == 0);
|
|
111
150
|
}
|
|
112
151
|
|
|
113
152
|
static void
|
|
@@ -116,6 +155,10 @@ on_meta (js_env_t *env, js_module_t *module, js_value_t *meta, void *data) {
|
|
|
116
155
|
|
|
117
156
|
int err;
|
|
118
157
|
|
|
158
|
+
js_handle_scope_t *scope;
|
|
159
|
+
err = js_open_handle_scope(env, &scope);
|
|
160
|
+
assert(err == 0);
|
|
161
|
+
|
|
119
162
|
js_value_t *ctx;
|
|
120
163
|
err = js_get_reference_value(env, context->ctx, &ctx);
|
|
121
164
|
assert(err == 0);
|
|
@@ -131,13 +174,22 @@ on_meta (js_env_t *env, js_module_t *module, js_value_t *meta, void *data) {
|
|
|
131
174
|
js_value_t *args[2];
|
|
132
175
|
|
|
133
176
|
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
|
|
134
|
-
if (err < 0)
|
|
177
|
+
if (err < 0) goto err;
|
|
135
178
|
|
|
136
179
|
args[1] = meta;
|
|
137
180
|
|
|
138
181
|
js_value_t *result;
|
|
139
182
|
err = js_call_function(env, ctx, on_meta, 2, args, &result);
|
|
140
|
-
if (err < 0)
|
|
183
|
+
if (err < 0) goto err;
|
|
184
|
+
|
|
185
|
+
err = js_close_handle_scope(env, scope);
|
|
186
|
+
assert(err == 0);
|
|
187
|
+
|
|
188
|
+
return;
|
|
189
|
+
|
|
190
|
+
err:
|
|
191
|
+
err = js_close_handle_scope(env, scope);
|
|
192
|
+
assert(err == 0);
|
|
141
193
|
}
|
|
142
194
|
|
|
143
195
|
static js_value_t *
|
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const path = require('bare-path')
|
|
3
3
|
const resolve = require('bare-module-resolve')
|
|
4
4
|
const Bundle = require('bare-bundle')
|
|
5
|
+
const { parse } = require('cjs-module-lexer')
|
|
5
6
|
const { fileURLToPath, pathToFileURL } = require('url-file-url')
|
|
6
7
|
const Protocol = require('./lib/protocol')
|
|
7
8
|
const constants = require('./lib/constants')
|
|
@@ -27,6 +28,8 @@ const Module = module.exports = exports = class Module {
|
|
|
27
28
|
this._conditions = null
|
|
28
29
|
this._protocol = null
|
|
29
30
|
this._bundle = null
|
|
31
|
+
this._function = null
|
|
32
|
+
this._names = null
|
|
30
33
|
this._handle = null
|
|
31
34
|
|
|
32
35
|
Module._modules.add(this)
|
|
@@ -112,42 +115,102 @@ const Module = module.exports = exports = class Module {
|
|
|
112
115
|
_transform (isImport, isDynamicImport) {
|
|
113
116
|
if (isDynamicImport) {
|
|
114
117
|
this._synthesize()
|
|
115
|
-
this._evaluate()
|
|
118
|
+
this._evaluate(true /* eagerRun */)
|
|
116
119
|
} else if (isImport) {
|
|
117
120
|
this._synthesize()
|
|
118
|
-
} else
|
|
121
|
+
} else {
|
|
119
122
|
this._evaluate()
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
return this
|
|
123
126
|
}
|
|
124
127
|
|
|
125
|
-
|
|
126
|
-
if ((this._state & constants.states.
|
|
128
|
+
_synthesize () {
|
|
129
|
+
if ((this._state & constants.states.SYNTHESIZED) !== 0) return
|
|
130
|
+
|
|
131
|
+
this._state |= constants.states.SYNTHESIZED
|
|
127
132
|
|
|
128
|
-
|
|
133
|
+
if (this._type === constants.types.MODULE) return
|
|
129
134
|
|
|
130
|
-
|
|
131
|
-
|
|
135
|
+
const names = ['default']
|
|
136
|
+
const queue = [this]
|
|
137
|
+
const seen = new Set()
|
|
138
|
+
|
|
139
|
+
while (queue.length) {
|
|
140
|
+
const module = queue.pop()
|
|
141
|
+
|
|
142
|
+
if (seen.has(module)) continue
|
|
143
|
+
|
|
144
|
+
seen.add(module)
|
|
145
|
+
|
|
146
|
+
switch (module._type) {
|
|
147
|
+
case constants.types.SCRIPT: {
|
|
148
|
+
const result = parse(module._function.toString())
|
|
149
|
+
|
|
150
|
+
names.push(...result.exports)
|
|
151
|
+
|
|
152
|
+
const referrer = module
|
|
153
|
+
|
|
154
|
+
for (const specifier of result.reexports) {
|
|
155
|
+
const resolved = Module.resolve(specifier, referrer._url, { isImport: true, referrer })
|
|
156
|
+
|
|
157
|
+
const module = Module.load(resolved, { isImport: true, referrer })
|
|
158
|
+
|
|
159
|
+
queue.push(module)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
break
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
case constants.types.MODULE: {
|
|
166
|
+
module._evaluate()
|
|
167
|
+
|
|
168
|
+
for (const name of Object.keys(module._exports)) {
|
|
169
|
+
names.push(name)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
break
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
case constants.types.JSON: {
|
|
176
|
+
for (const name of Object.keys(module._exports)) {
|
|
177
|
+
names.push(name)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
132
181
|
}
|
|
133
182
|
|
|
134
|
-
this.
|
|
183
|
+
this._names = names
|
|
184
|
+
|
|
185
|
+
this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
|
|
135
186
|
}
|
|
136
187
|
|
|
137
|
-
|
|
138
|
-
if ((this._state & constants.states.
|
|
188
|
+
_evaluate (eagerRun = false) {
|
|
189
|
+
if ((this._state & constants.states.EVALUATED) !== 0) return
|
|
190
|
+
|
|
191
|
+
this._state |= constants.states.EVALUATED
|
|
139
192
|
|
|
140
|
-
if (this._type
|
|
141
|
-
const
|
|
193
|
+
if (this._type === constants.types.SCRIPT) {
|
|
194
|
+
const require = createRequire(this._url, { module: this })
|
|
142
195
|
|
|
143
|
-
|
|
144
|
-
if (key !== 'default') names.push(key)
|
|
145
|
-
}
|
|
196
|
+
this._exports = {}
|
|
146
197
|
|
|
147
|
-
this.
|
|
198
|
+
this._function(
|
|
199
|
+
require,
|
|
200
|
+
this,
|
|
201
|
+
this._exports,
|
|
202
|
+
urlToPath(this._url),
|
|
203
|
+
urlToDirname(this._url)
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
if (eagerRun) binding.runModule(this._handle, Module._handle)
|
|
148
207
|
}
|
|
149
208
|
|
|
150
|
-
this.
|
|
209
|
+
if (this._type === constants.types.MODULE) {
|
|
210
|
+
binding.runModule(this._handle, Module._handle)
|
|
211
|
+
|
|
212
|
+
this._exports = binding.getNamespace(this._handle)
|
|
213
|
+
}
|
|
151
214
|
}
|
|
152
215
|
|
|
153
216
|
[Symbol.for('bare.inspect')] () {
|
|
@@ -218,10 +281,10 @@ const Module = module.exports = exports = class Module {
|
|
|
218
281
|
throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`)
|
|
219
282
|
}
|
|
220
283
|
|
|
221
|
-
|
|
284
|
+
module._evaluate()
|
|
222
285
|
|
|
223
|
-
for (const
|
|
224
|
-
binding.setExport(module._handle,
|
|
286
|
+
for (const name of module._names) {
|
|
287
|
+
binding.setExport(module._handle, name, name === 'default' ? module._exports : module._exports[name])
|
|
225
288
|
}
|
|
226
289
|
}
|
|
227
290
|
|
|
@@ -415,7 +478,7 @@ exports.isBuiltin = function isBuiltin () {
|
|
|
415
478
|
return false
|
|
416
479
|
}
|
|
417
480
|
|
|
418
|
-
exports.createRequire = function createRequire (parentURL, opts = {}) {
|
|
481
|
+
const createRequire = exports.createRequire = function createRequire (parentURL, opts = {}) {
|
|
419
482
|
const self = Module
|
|
420
483
|
|
|
421
484
|
if (typeof parentURL === 'string') {
|
|
@@ -427,6 +490,8 @@ exports.createRequire = function createRequire (parentURL, opts = {}) {
|
|
|
427
490
|
}
|
|
428
491
|
|
|
429
492
|
let {
|
|
493
|
+
module = null,
|
|
494
|
+
|
|
430
495
|
referrer = null,
|
|
431
496
|
type = constants.types.SCRIPT,
|
|
432
497
|
defaultType = referrer ? referrer._defaultType : constants.types.SCRIPT,
|
|
@@ -439,17 +504,19 @@ exports.createRequire = function createRequire (parentURL, opts = {}) {
|
|
|
439
504
|
conditions = referrer ? referrer._conditions : self._conditions
|
|
440
505
|
} = opts
|
|
441
506
|
|
|
442
|
-
|
|
507
|
+
if (module === null) {
|
|
508
|
+
module = new Module(parentURL)
|
|
443
509
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
510
|
+
module._type = type
|
|
511
|
+
module._defaultType = defaultType
|
|
512
|
+
module._cache = cache
|
|
513
|
+
module._main = main || module
|
|
514
|
+
module._protocol = protocol
|
|
515
|
+
module._imports = imports
|
|
516
|
+
module._resolutions = resolutions
|
|
517
|
+
module._builtins = builtins
|
|
518
|
+
module._conditions = conditions
|
|
519
|
+
}
|
|
453
520
|
|
|
454
521
|
referrer = module
|
|
455
522
|
|
|
@@ -522,8 +589,6 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
522
589
|
}
|
|
523
590
|
|
|
524
591
|
Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
525
|
-
const self = Module
|
|
526
|
-
|
|
527
592
|
const protocol = module._protocol
|
|
528
593
|
|
|
529
594
|
module._type = constants.types.SCRIPT
|
|
@@ -535,49 +600,7 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
535
600
|
|
|
536
601
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
537
602
|
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
addon.host = Bare.Addon.host
|
|
541
|
-
|
|
542
|
-
require.main = module._main
|
|
543
|
-
require.cache = module._cache
|
|
544
|
-
require.resolve = resolve
|
|
545
|
-
require.addon = addon
|
|
546
|
-
|
|
547
|
-
module._exports = {}
|
|
548
|
-
|
|
549
|
-
binding.createFunction(module._url.href, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
550
|
-
require,
|
|
551
|
-
module,
|
|
552
|
-
module._exports,
|
|
553
|
-
urlToPath(module._url),
|
|
554
|
-
urlToDirname(module._url)
|
|
555
|
-
)
|
|
556
|
-
|
|
557
|
-
function require (specifier) {
|
|
558
|
-
const resolved = self.resolve(specifier, referrer._url, { referrer })
|
|
559
|
-
|
|
560
|
-
const module = self.load(resolved, { referrer })
|
|
561
|
-
|
|
562
|
-
return module._exports
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
function resolve (specifier) {
|
|
566
|
-
const resolved = self.resolve(specifier, referrer._url, { referrer })
|
|
567
|
-
|
|
568
|
-
switch (resolved.protocol) {
|
|
569
|
-
case 'builtin:': return resolved.pathname
|
|
570
|
-
default: urlToPath(resolved)
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
function addon (specifier = '.') {
|
|
575
|
-
const resolved = Bare.Addon.resolve(specifier, referrer._url, { referrer })
|
|
576
|
-
|
|
577
|
-
const addon = Bare.Addon.load(resolved, { referrer })
|
|
578
|
-
|
|
579
|
-
return addon._exports
|
|
580
|
-
}
|
|
603
|
+
module._function = binding.createFunction(module._url.href, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)
|
|
581
604
|
}
|
|
582
605
|
}
|
|
583
606
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.8",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"bare-bundle": "^1.0.0",
|
|
30
30
|
"bare-module-resolve": "^1.4.4",
|
|
31
31
|
"bare-path": "^2.0.0",
|
|
32
|
+
"cjs-module-lexer": "^1.2.3",
|
|
32
33
|
"url-file-url": "^1.0.2"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|