Skip to content

Commit

Permalink
Add support for finding main file w/ export map
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 25, 2023
1 parent b46d8f5 commit 19a2c08
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
* Heading to look for (default: `'usage'`);
* wrapped in `new RegExp('^(' + value + ')$', 'i');`.
* @property {string | null | undefined} [main]
* Path to the main file (default: `pkg.main` or `'./index.js'`);
* Path to the main file (default: `pkg.exports`, `pkg.main`, `'index.js'`);
* resolved from `file.cwd`;
* used to rewrite `import x from './main.js'` to `import x from 'name'`.
* @property {string | null | undefined} [name]
Expand Down Expand Up @@ -192,8 +192,22 @@ export default function remarkUsage(options) {
let main

try {
const exports = pkg?.value.exports
const primary =
/* c8 ignore next 2 -- seems useless to have an array, but types have it. */
exports && typeof exports === 'object' && Array.isArray(exports)
? exports[0]
: exports
const item =
primary && typeof primary === 'object' ? primary['.'] : primary

main = resolve(
relativeModule(settings.main || pkg?.value.main || 'index.js'),
relativeModule(
settings.main ||
(typeof item === 'string' ? item : undefined) ||
pkg?.value.main ||
'index.js'
),
pathToFileURL(settings.main ? cwd : pkg?.file.dirname || cwd).href + '/'
)
} catch {}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/normal-custom-exports-object/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Import `pi`:
import {pi} from './pi.js'

// Logs:
console.log('text', pi)
15 changes: 15 additions & 0 deletions test/fixtures/normal-custom-exports-object/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PI

## Usage

Import `pi`:

```javascript
import {pi} from 'pi'
```

Logs:

```text
3.1415
```
7 changes: 7 additions & 0 deletions test/fixtures/normal-custom-exports-object/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "pi",
"type": "module",
"exports": {
".": "./pi.js"
}
}
1 change: 1 addition & 0 deletions test/fixtures/normal-custom-exports-object/pi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pi = Math.PI.toString().slice(0, 6)
3 changes: 3 additions & 0 deletions test/fixtures/normal-custom-exports-object/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PI

## Usage
5 changes: 5 additions & 0 deletions test/fixtures/normal-custom-exports-plain/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Import `pi`:
import {pi} from './pi.js'

// Logs:
console.log('text', pi)
15 changes: 15 additions & 0 deletions test/fixtures/normal-custom-exports-plain/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PI

## Usage

Import `pi`:

```javascript
import {pi} from 'pi'
```

Logs:

```text
3.1415
```
5 changes: 5 additions & 0 deletions test/fixtures/normal-custom-exports-plain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "pi",
"type": "module",
"exports": "./pi.js"
}
1 change: 1 addition & 0 deletions test/fixtures/normal-custom-exports-plain/pi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pi = Math.PI.toString().slice(0, 6)
3 changes: 3 additions & 0 deletions test/fixtures/normal-custom-exports-plain/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PI

## Usage

0 comments on commit 19a2c08

Please sign in to comment.