bare-module 3.1.4 → 3.1.6
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 +210 -0
- package/index.js +39 -19
- package/lib/errors.js +4 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,14 +12,179 @@ npm i bare-module
|
|
|
12
12
|
const Module = require('bare-module')
|
|
13
13
|
````
|
|
14
14
|
|
|
15
|
+
## Packages
|
|
16
|
+
|
|
17
|
+
A package is directory with a `package.json` file.
|
|
18
|
+
|
|
19
|
+
### Fields
|
|
20
|
+
|
|
21
|
+
#### `"name"`
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
{
|
|
25
|
+
"name": "my-package"
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The name of the package. This is used for [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm), [self-referencing](#self-referencing), and importing packages by name.
|
|
30
|
+
|
|
31
|
+
#### `"version"`
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"version": "1.2.3"
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The current version of the package. This is used for [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm).
|
|
40
|
+
|
|
41
|
+
#### `"type"`
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"type": "module"
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The module format used for `.js` files. If not defined, `.js` files are interpreted as CommonJS. If set to `"module"`, `.js` files are instead interpreted as ES modules.
|
|
50
|
+
|
|
51
|
+
#### `"exports"`
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"exports": {
|
|
56
|
+
".": "./index.js"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The entry points of the package. If defined, only the modules explicitly exported by the package may be imported when importing the package by name.
|
|
62
|
+
|
|
63
|
+
##### Subpath exports
|
|
64
|
+
|
|
65
|
+
A package may define more than one entry point by declaring several subpaths with the main export being `"."`:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"exports": {
|
|
70
|
+
".": "./index.js",
|
|
71
|
+
"./submodule": "./lib/submodule.js"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
When importing the package by name, `require('my-package')` will resolve to `<modules>/my-package/index.js` whereas `require('my-package/submodule')` will resolve to `<modules>/my-package/lib/submodule.js`.
|
|
77
|
+
|
|
78
|
+
##### Conditional exports
|
|
79
|
+
|
|
80
|
+
Conditional exports allow packages to provide different exports depending for different conditions, such as the module format of the importing module:
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{
|
|
84
|
+
"exports": {
|
|
85
|
+
".": {
|
|
86
|
+
"import": "./index.mjs",
|
|
87
|
+
"require": "./index.cjs"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
When importing the package by name, `require('my-package')` will resolve to `<modules>/my-package/index.cjs` whereas `import 'my-package'` will resolve to `<modules>/my-package/index.mjs`.
|
|
94
|
+
|
|
95
|
+
Similarly, conditional exports can be used to provide different entry points for different runtimes:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"exports": {
|
|
100
|
+
".": {
|
|
101
|
+
"bare": "./bare.js",
|
|
102
|
+
"node": "./node.js"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
To provide a fallback for when no other conditions match, the `"default"` condition can be declared:
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"exports": {
|
|
113
|
+
".": {
|
|
114
|
+
"bare": "./bare.js",
|
|
115
|
+
"node": "./node.js",
|
|
116
|
+
"default": "./fallback.js"
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The following conditions are supported, listed in order from most specific to least specific as conditions should be defined:
|
|
123
|
+
|
|
124
|
+
Condition | Description
|
|
125
|
+
:-- | :--
|
|
126
|
+
`"bare"` |
|
|
127
|
+
`"node"` |
|
|
128
|
+
`"import"` |
|
|
129
|
+
`"require"` |
|
|
130
|
+
`"default"` |
|
|
131
|
+
|
|
132
|
+
##### Self-referencing
|
|
133
|
+
|
|
134
|
+
Within a package, exports defined in the `"exports"` field can be referenced by importing the package by name. For example, given the following `package.json`...
|
|
135
|
+
|
|
136
|
+
```json
|
|
137
|
+
{
|
|
138
|
+
"name": "my-package",
|
|
139
|
+
"exports": {
|
|
140
|
+
".": "./index.js",
|
|
141
|
+
"./submodule": "./lib/submodule.js"
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
...any module within `my-package` may reference these entry points using either `require('my-package')` or `require('my-package/submodule')`.
|
|
147
|
+
|
|
148
|
+
##### Exports sugar
|
|
149
|
+
|
|
150
|
+
If a package defines only a single export, `"."`, it may leave out the subpath entirely:
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"exports": "./index.js"
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### `"imports"`
|
|
159
|
+
|
|
160
|
+
##### Subpath imports
|
|
161
|
+
|
|
162
|
+
##### Conditional imports
|
|
163
|
+
|
|
164
|
+
##### Private imports
|
|
165
|
+
|
|
15
166
|
## API
|
|
16
167
|
|
|
17
168
|
#### `Module.constants`
|
|
18
169
|
|
|
19
170
|
#### `Module.constants.states`
|
|
20
171
|
|
|
172
|
+
Constant | Description
|
|
173
|
+
:-- | :--
|
|
174
|
+
`EVALUATED` |
|
|
175
|
+
`SYNTHESIZED` |
|
|
176
|
+
`DESTROYED` |
|
|
177
|
+
|
|
21
178
|
#### `Module.constants.types`
|
|
22
179
|
|
|
180
|
+
Constant | Description
|
|
181
|
+
:-- | :--
|
|
182
|
+
`SCRIPT` |
|
|
183
|
+
`MODULE` |
|
|
184
|
+
`JSON` |
|
|
185
|
+
`BUNDLE` |
|
|
186
|
+
`ADDON` |
|
|
187
|
+
|
|
23
188
|
#### `Module.cache`
|
|
24
189
|
|
|
25
190
|
#### `const url = Module.resolve(specifier, parentURL[, options])`
|
|
@@ -28,6 +193,12 @@ Options include:
|
|
|
28
193
|
|
|
29
194
|
```js
|
|
30
195
|
{
|
|
196
|
+
referrer = null,
|
|
197
|
+
protocol,
|
|
198
|
+
imports,
|
|
199
|
+
resolutions,
|
|
200
|
+
builtins,
|
|
201
|
+
conditions
|
|
31
202
|
}
|
|
32
203
|
```
|
|
33
204
|
|
|
@@ -37,6 +208,16 @@ Options include:
|
|
|
37
208
|
|
|
38
209
|
```js
|
|
39
210
|
{
|
|
211
|
+
referrer = null,
|
|
212
|
+
type,
|
|
213
|
+
defaultType = constants.types.SCRIPT,
|
|
214
|
+
cache,
|
|
215
|
+
main,
|
|
216
|
+
protocol,
|
|
217
|
+
imports,
|
|
218
|
+
resolutions,
|
|
219
|
+
builtins,
|
|
220
|
+
conditions
|
|
40
221
|
}
|
|
41
222
|
```
|
|
42
223
|
|
|
@@ -50,12 +231,16 @@ Options include:
|
|
|
50
231
|
|
|
51
232
|
#### `module.defaultType`
|
|
52
233
|
|
|
234
|
+
#### `module.cache`
|
|
235
|
+
|
|
53
236
|
#### `module.main`
|
|
54
237
|
|
|
55
238
|
#### `module.exports`
|
|
56
239
|
|
|
57
240
|
#### `module.imports`
|
|
58
241
|
|
|
242
|
+
#### `module.resolutions`
|
|
243
|
+
|
|
59
244
|
#### `module.builtins`
|
|
60
245
|
|
|
61
246
|
#### `module.conditions`
|
|
@@ -68,6 +253,23 @@ Options include:
|
|
|
68
253
|
|
|
69
254
|
#### `const require = Module.createRequire(parentURL[, options])`
|
|
70
255
|
|
|
256
|
+
Options include:
|
|
257
|
+
|
|
258
|
+
```js
|
|
259
|
+
{
|
|
260
|
+
referrer = null,
|
|
261
|
+
type = constants.types.SCRIPT,
|
|
262
|
+
defaultType = constants.types.SCRIPT,
|
|
263
|
+
cache,
|
|
264
|
+
main,
|
|
265
|
+
protocol,
|
|
266
|
+
imports,
|
|
267
|
+
resolutions,
|
|
268
|
+
builtins,
|
|
269
|
+
conditions
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
71
273
|
### Protocols
|
|
72
274
|
|
|
73
275
|
#### `const protocol = new Module.Protocol(options)`
|
|
@@ -76,6 +278,12 @@ Options include:
|
|
|
76
278
|
|
|
77
279
|
```js
|
|
78
280
|
{
|
|
281
|
+
preresolve,
|
|
282
|
+
postresolve,
|
|
283
|
+
resolve,
|
|
284
|
+
exists,
|
|
285
|
+
read,
|
|
286
|
+
load
|
|
79
287
|
}
|
|
80
288
|
```
|
|
81
289
|
|
|
@@ -83,6 +291,8 @@ Options include:
|
|
|
83
291
|
|
|
84
292
|
#### `const bundle = new Module.Bundle()`
|
|
85
293
|
|
|
294
|
+
See <https://github.com/holepunchto/bare-bundle>.
|
|
295
|
+
|
|
86
296
|
## License
|
|
87
297
|
|
|
88
298
|
Apache-2.0
|
package/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/* global Bare */
|
|
2
2
|
const path = require('bare-path')
|
|
3
|
-
const url = require('bare-url')
|
|
4
3
|
const resolve = require('bare-module-resolve')
|
|
5
4
|
const Bundle = require('bare-bundle')
|
|
5
|
+
const { fileURLToPath, pathToFileURL } = require('url-file-url')
|
|
6
6
|
const Protocol = require('./lib/protocol')
|
|
7
7
|
const constants = require('./lib/constants')
|
|
8
8
|
const errors = require('./lib/errors')
|
|
9
9
|
const binding = require('./binding')
|
|
10
10
|
|
|
11
|
+
const isWindows = Bare.platform === 'win32'
|
|
12
|
+
|
|
11
13
|
const { startsWithWindowsDriveLetter } = resolve
|
|
12
14
|
|
|
13
15
|
const Module = module.exports = exports = class Module {
|
|
@@ -544,14 +546,12 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
544
546
|
|
|
545
547
|
module._exports = {}
|
|
546
548
|
|
|
547
|
-
const filename = urlToPath(module._url)
|
|
548
|
-
|
|
549
549
|
binding.createFunction(module._url.href, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
550
550
|
require,
|
|
551
551
|
module,
|
|
552
552
|
module._exports,
|
|
553
|
-
|
|
554
|
-
|
|
553
|
+
urlToPath(module._url),
|
|
554
|
+
urlToDirname(module._url)
|
|
555
555
|
)
|
|
556
556
|
|
|
557
557
|
function require (specifier) {
|
|
@@ -665,16 +665,16 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
665
665
|
}
|
|
666
666
|
|
|
667
667
|
Module._protocols['file:'] = new Protocol({
|
|
668
|
-
postresolve (
|
|
669
|
-
return
|
|
668
|
+
postresolve (url) {
|
|
669
|
+
return pathToFileURL(binding.realpath(fileURLToPath(url)))
|
|
670
670
|
},
|
|
671
671
|
|
|
672
|
-
exists (
|
|
673
|
-
return binding.exists(
|
|
672
|
+
exists (url) {
|
|
673
|
+
return binding.exists(fileURLToPath(url))
|
|
674
674
|
},
|
|
675
675
|
|
|
676
|
-
read (
|
|
677
|
-
return Buffer.from(binding.read(
|
|
676
|
+
read (url) {
|
|
677
|
+
return Buffer.from(binding.read(fileURLToPath(url)))
|
|
678
678
|
}
|
|
679
679
|
})
|
|
680
680
|
|
|
@@ -687,14 +687,34 @@ Bare
|
|
|
687
687
|
binding.destroy(Module._handle)
|
|
688
688
|
})
|
|
689
689
|
|
|
690
|
-
function urlToPath (
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
690
|
+
function urlToPath (url) {
|
|
691
|
+
if (url.protocol === 'file:') return fileURLToPath(url)
|
|
692
|
+
|
|
693
|
+
if (isWindows) {
|
|
694
|
+
if (/%2f|%5c/i.test(url.pathname)) {
|
|
695
|
+
throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
|
|
696
|
+
}
|
|
697
|
+
} else {
|
|
698
|
+
if (/%2f/i.test(url.pathname)) {
|
|
699
|
+
throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
return decodeURIComponent(url.pathname)
|
|
694
704
|
}
|
|
695
705
|
|
|
696
|
-
function urlToDirname (
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
706
|
+
function urlToDirname (url) {
|
|
707
|
+
if (url.protocol === 'file:') return path.dirname(fileURLToPath(url))
|
|
708
|
+
|
|
709
|
+
if (isWindows) {
|
|
710
|
+
if (/%2f|%5c/i.test(url.pathname)) {
|
|
711
|
+
throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
|
|
712
|
+
}
|
|
713
|
+
} else {
|
|
714
|
+
if (/%2f/i.test(url.pathname)) {
|
|
715
|
+
throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
return decodeURIComponent((new URL('.', url)).pathname).replace(/\/$/, '')
|
|
700
720
|
}
|
package/lib/errors.js
CHANGED
|
@@ -23,4 +23,8 @@ module.exports = class ModuleError extends Error {
|
|
|
23
23
|
static INVALID_BUNDLE_EXTENSION (msg) {
|
|
24
24
|
return new ModuleError(msg, 'INVALID_BUNDLE_EXTENSION', ModuleError.INVALID_BUNDLE_EXTENSION)
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
static INVALID_URL_PATH (msg) {
|
|
28
|
+
return new ModuleError(msg, 'INVALID_URL_PATH', ModuleError.INVALID_URL_PATH)
|
|
29
|
+
}
|
|
26
30
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.6",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -29,7 +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
|
-
"
|
|
32
|
+
"url-file-url": "^1.0.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"brittle": "^3.1.1",
|