{"id":26591919,"url":"https://github.com/cody-dot-js/parse-static-imports","last_synced_at":"2026-04-11T06:32:02.710Z","repository":{"id":35077334,"uuid":"204103997","full_name":"cody-dot-js/parse-static-imports","owner":"cody-dot-js","description":"Gracefully parse ECMAScript static imports 💃","archived":false,"fork":false,"pushed_at":"2023-01-04T08:00:29.000Z","size":822,"stargazers_count":2,"open_issues_count":19,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-12T19:48:32.497Z","etag":null,"topics":["ecmascript","es6","import","javascript","js"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cody-dot-js.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-24T03:38:53.000Z","updated_at":"2020-10-28T20:55:20.000Z","dependencies_parsed_at":"2023-01-15T13:21:25.118Z","dependency_job_id":null,"html_url":"https://github.com/cody-dot-js/parse-static-imports","commit_stats":null,"previous_names":["dev-cprice/parse-static-imports"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cody-dot-js%2Fparse-static-imports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cody-dot-js%2Fparse-static-imports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cody-dot-js%2Fparse-static-imports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cody-dot-js%2Fparse-static-imports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cody-dot-js","download_url":"https://codeload.github.com/cody-dot-js/parse-static-imports/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245115970,"owners_count":20563263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecmascript","es6","import","javascript","js"],"created_at":"2025-03-23T14:31:44.850Z","updated_at":"2025-12-30T23:32:04.949Z","avatar_url":"https://github.com/cody-dot-js.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parse-static-imports\n\nGracefully parse ECMAScript [static imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) 💃\n\nWill properly parse:\n\n- default imports\n- star imports, e.g. `import * as Foo from \"foo\";`\n- named imports, even with an alias!\n- side effect only imports, e.g. `import \"./App.css\";`\n- multi-line imports, like:\n\n  ```jsx\n  import React, {\n    useState,\n    useCallback,\n    useEffect\n  } from \"react\";\n  ```\n\nwhile ignoring commented out imports (both line and block comments).\n\n## Installation\n\n```sh\nnpm install --save parse-static-imports\n```\n\n## Usage\n\n```js\nimport fs from \"fs\";\nimport parseStaticImports from \"parse-static-imports\";\n\nconst file = fs.readFileSync(\"./path/to/file.js\", \"utf8\");\n\nconst results = parseStaticImports(file);\n\nconsole.log(JSON.stringify(results, null, 2));\n```\n\n## parseStaticImports\n\n- `file`: `String` - Contents of a file containing static imports\n- returns: `Object[]` - List of static imports found in the given file contents\n\nThe parseStaticImports() method returns a a list of objects whose properties\nrepresent significant elements of the static import.\n\nThe returned list of objects will have the following properties:\n\n| Attribute      | Type       | Default Value | Description                                                             |\n| -------------- | ---------- | ------------- | ----------------------------------------------------------------------- |\n| moduleName     | `String`   | N/A           | The name of the module imported or a relative path (e.g. `\"react-dom\"`) |\n| starImport     | `String`   | `\"\"`          | The name of the star imported module object, if present                 |\n| namedImports   | `Object[]` | `[]`          | List of named imports as a list of objects                              |\n| defaultImport  | `String`   | `\"\"`          | The name of the default import, if present                              |\n| sideEffectOnly | `Boolean`  | false         | If the import was side-effect only (e.g. `import \"./App.css\";`)         |\n\nNamed import objects have the form:\n\n| Attribute | Type     | Default Value | Description                                                                                                                                                                      |\n| --------- | -------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| name      | `String` | N/A           | The name of the named import (e.g. `{ useState }`)                                                                                                                               |\n| 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\";`) |\n\n## Example\n\nGiven the typical `create-react-app` scaffold file `src/App.js` ([source](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/src/App.js)):\n\n```jsx\nimport React from 'react';\nimport logo from './logo.svg';\nimport './App.css';\n\nfunction App() {\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003cheader className=\"App-header\"\u003e\n        \u003cimg src={logo} className=\"App-logo\" alt=\"logo\" /\u003e\n        \u003cp\u003e\n          Edit \u003ccode\u003esrc/App.js\u003c/code\u003e and save to reload.\n        \u003c/p\u003e\n        \u003ca\n          className=\"App-link\"\n          href=\"https://reactjs.org\"\n          target=\"_blank\"\n          rel=\"noopener noreferrer\"\n        \u003e\n          Learn React\n        \u003c/a\u003e\n      \u003c/header\u003e\n    \u003c/div\u003e\n  );\n}\n\nexport default App;\n```\n\n`parse-static-imports` will output the following:\n\n```json\n[\n  {\n    \"moduleName\": \"react\",\n    \"starImport\": \"\",\n    \"namedImports\": [],\n    \"defaultImport\": \"React\",\n    \"sideEffectOnly\": false\n  },\n  {\n    \"moduleName\": \"./logo.svg\",\n    \"starImport\": \"\",\n    \"namedImports\": [],\n    \"defaultImport\": \"logo\",\n    \"sideEffectOnly\": false\n  },\n  {\n    \"moduleName\": \"./App.css\",\n    \"starImport\": \"\",\n    \"namedImports\": [],\n    \"defaultImport\": \"\",\n    \"sideEffectOnly\": true\n  }\n]\n```\n\nBy modifying the `create-react-app` `src/index.js` a bit ([source](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/src/index.js)), we can show the full power of `static-import-parser`:\n\n```jsx\nimport React, { useState as useFoo } from 'react';\nimport { render } from 'react-dom';\nimport './index.css';\nimport App from './App';\nimport * as serviceWorker from './serviceWorker';\n\n// let's even throw in a commonjs require for good measure 😉\nconst fs = require(\"fs\");\n\nrender(\u003cApp /\u003e, document.getElementById('root'));\n\n// If you want your app to work offline and load faster, you can change\n// unregister() to register() below. Note this comes with some pitfalls.\n// Learn more about service workers: https://bit.ly/CRA-PWA\nserviceWorker.unregister();\n```\n\n`parse-static-imports` will output the following:\n\n```json\n[\n  {\n    \"moduleName\": \"react\",\n    \"starImport\": \"\",\n    \"namedImports\": [\n      {\n        \"name\": \"useState\",\n        \"alias\": \"useFoo\"\n      }\n    ],\n    \"defaultImport\": \"React\",\n    \"sideEffectOnly\": false\n  },\n  {\n    \"moduleName\": \"react-dom\",\n    \"starImport\": \"\",\n    \"namedImports\": [\n      {\n        \"name\": \"render\",\n        \"alias\": \"render\"\n      }\n    ],\n    \"defaultImport\": \"\",\n    \"sideEffectOnly\": false\n  },\n  {\n    \"moduleName\": \"./index.css\",\n    \"starImport\": \"\",\n    \"namedImports\": [],\n    \"defaultImport\": \"\",\n    \"sideEffectOnly\": true\n  },\n  {\n    \"moduleName\": \"./App\",\n    \"starImport\": \"\",\n    \"namedImports\": [],\n    \"defaultImport\": \"App\",\n    \"sideEffectOnly\": false\n  },\n  {\n    \"moduleName\": \"./serviceWorker\",\n    \"starImport\": \"serviceWorker\",\n    \"namedImports\": [],\n    \"defaultImport\": \"\",\n    \"sideEffectOnly\": false\n  }\n]\n```\n\nNotice that `ReactDOM.render` was changed to a named import and we also name imported and aliased `React.useState` to `useFoo`. These both show up in the named exports locations of their respective packages where the former's `name` and `alias` are identical and the latter shows the alias that was used for `useState`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcody-dot-js%2Fparse-static-imports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcody-dot-js%2Fparse-static-imports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcody-dot-js%2Fparse-static-imports/lists"}