# parse-es-import

> Parse the ESM dependencies of code snippets based on acorn

Latest version **0.6.0** (published 2023-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install parse-es-import
pnpm add parse-es-import
yarn add parse-es-import
bun add parse-es-import
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2023-07-28 |
| First published | 2021-06-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 21 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | zhihengzuo@gmail.com |
| Maintainers | misterluffy |
| Keywords | esmodule, import, dependency, browser |

## Links

- npm: https://www.npmjs.com/package/parse-es-import
- Repository: https://github.com/MisterLuffy/parse-es-import
- Homepage: https://github.com/MisterLuffy/parse-es-import#readme
- Issues: https://github.com/MisterLuffy/parse-es-import/issues
- npm.io page: https://npm.io/package/parse-es-import

## Dependencies (2)

- [acorn](https://npm.io/package/acorn.md) ^8.9.0
- [acorn-jsx](https://npm.io/package/acorn-jsx.md) ^5.3.2

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2023-07-28
- 0.5.0 — 2022-05-07
- 0.4.0 — 2022-05-07
- 0.3.2 — 2021-11-12
- 0.3.1 — 2021-08-31
- 0.3.0 — 2021-08-30
- 0.2.1 — 2021-07-05
- 0.2.0 — 2021-06-30
- 0.1.0 — 2021-06-30

## README

# parse-es-import

Inspired by [parse-static-imports](https://www.npmjs.com/package/parse-static-imports),
and have the same return result as it, and can run both on browser/node sides because it is based on [acron](https://www.npmjs.com/package/acorn).

Will properly parse:

- Default import. e.g. `import utils from 'utils';`
- Star imports. e.g. `import * as utils from 'utils';`
- Named imports. e.g. `import { Foo as MyFoo } from 'utils';`
- Side effect only imports, e.g. `import './App.css';`
- Multi-line imports, like

  ```jsx
  import React, { useState, useCallback, useEffect } from 'react';
  ```

## Installation

```sh
npm install --save parse-es-import
```

## Usage

```js
import fs from 'fs';
import parse from 'parse-es-import';

const file = fs.readFileSync('./path/to/file.js', 'utf8');
const results = parse(file);

console.log(JSON.stringify(results, null, 2));
```

### API

- `content`: `String` - Contents of code to parse.
- `options`: `Object` - Receive all parameters of [`acorn.parse`](https://github.com/acornjs/acorn/tree/master/acorn#interface). Its default value is `{ ecmaVersion: 2021, sourceType: 'module' }`.

### Returns

```json
{
  "imports": {},
  "exports": {}
}
```

#### imports

| Attribute      | Type       | Default Value | Description                                                             |
| -------------- | ---------- | ------------- | ----------------------------------------------------------------------- |
| moduleName     | `String`   | `''`          | The name of the module imported or a relative path (e.g. `'react-dom'`) |
| starImport     | `String`   | `''`          | The name of the star imported module object, if present                 |
| namedImports   | `Object[]` | `[]`          | List of named imports as a list of objects                              |
| defaultImport  | `String`   | `''`          | The name of the default import, if present                              |
| sideEffectOnly | `Boolean`  | false         | If the import was side-effect only (e.g. `import './App.css';`)         |
| startIndex     | `Number`   | 0             | Index of the starting character of the import statement                 |
| endIndex       | `Number`   | 0             | Index of the ending character + 1 of the import statement               |

Named import objects have the form:

| Attribute | Type     | Default Value | Description                                                                                                                                                                      |
| --------- | -------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name      | `String` | `''`          | The name of the named import (e.g. `{ useState }`)                                                                                                                               |
| alias     | `String` | name          | Will be the alias of a named import if aliased, otherwise defaults to the named import (e.g. `import { foo /* the named import */ as bar /* the alias */ } from 'module-name';`) |

#### exports

| Attribute  | Type                  | Default Value | Description                                                          |
| ---------- |-----------------------|---------------|----------------------------------------------------------------------|
| moduleName | `String`              | `''`          | The name of the module exported                                      |
| type       | `String`              | `''`          | The type of module exported                                          |
| value      | `String`              | `''`          | The value of module exported or a relative path (e.g. `'../add.js'`) |
| startIndex | `Number`              | 0             | Index of the starting character of the export statement              |
| endIndex   | `Number`              | 0             | Index of the ending character + 1 of the export statement            |
| identifierList   | `String`              | null          | The identifiers exported by this file                                |
| identifierTree   | `Record<string, any>` | null          | The identifier tree exported by this file                            |


## Example

Content to parse:

```jsx
import React from 'react';
import antd, { Button as AntButton, Alert } from 'antd';
import * as Hello from 'hello';

import 'xx.less';

export function Demo () {
  return <div>Hello World...</div>;
};

// this identifier is exported by obj
const A = 'A';
export const obj = { A: A };
```

The parse result will be:

```json
{
  "imports": [
    {
      "moduleName": "react",
      "starImport": "",
      "defaultImport": "React",
      "namedImports": [],
      "sideEffectOnly": false,
      "startIndex": 1,
      "endIndex": 27
    },
    {
      "moduleName": "antd",
      "starImport": "",
      "defaultImport": "antd",
      "namedImports": [
        {
          "name": "Button",
          "alias": "AntButton"
        },
        {
          "name": "Alert",
          "alias": "Alert"
        }
      ],
      "sideEffectOnly": false,
      "startIndex": 28,
      "endIndex": 84
    },
    {
      "moduleName": "hello",
      "starImport": "Hello",
      "defaultImport": "",
      "namedImports": [],
      "sideEffectOnly": false,
      "startIndex": 85,
      "endIndex": 116
    },
    {
      "moduleName": "xx.less",
      "starImport": "",
      "defaultImport": "",
      "namedImports": [],
      "sideEffectOnly": true,
      "startIndex": 118,
      "endIndex": 135
    }
  ],
  "exports": [
    {
      "type": "FunctionDeclaration",
      "moduleName": "Demo",
      "value": "function Demo () {\n  return <div>Hello World...</div>;\n}",
      "startIndex": 137,
      "endIndex": 200
    },
    {
      "type": "VariableDeclaration",
      "moduleName": "obj",
      "value": "{ A: A }",
      "identifierList": [
        "A"
      ],
      "identifierTree": {
        "A": "A"
      },
      "startIndex": 256,
      "endIndex": 284
    }
  ]
}
```

---
_Source: https://npm.io/package/parse-es-import · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
