bare-module 3.1.5 → 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.
Files changed (3) hide show
  1. package/README.md +151 -0
  2. package/index.js +2 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -12,6 +12,157 @@ 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`
package/index.js CHANGED
@@ -688,7 +688,7 @@ Bare
688
688
  })
689
689
 
690
690
  function urlToPath (url) {
691
- if (url.protocol) return fileURLToPath(url)
691
+ if (url.protocol === 'file:') return fileURLToPath(url)
692
692
 
693
693
  if (isWindows) {
694
694
  if (/%2f|%5c/i.test(url.pathname)) {
@@ -704,7 +704,7 @@ function urlToPath (url) {
704
704
  }
705
705
 
706
706
  function urlToDirname (url) {
707
- if (url.protocol) return path.dirname(fileURLToPath(url))
707
+ if (url.protocol === 'file:') return path.dirname(fileURLToPath(url))
708
708
 
709
709
  if (isWindows) {
710
710
  if (/%2f|%5c/i.test(url.pathname)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "3.1.5",
3
+ "version": "3.1.6",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [