https://github.com/cheton/find-imports
Find all imported modules in JavaScript files.
https://github.com/cheton/find-imports
Last synced: about 1 year ago
JSON representation
Find all imported modules in JavaScript files.
- Host: GitHub
- URL: https://github.com/cheton/find-imports
- Owner: cheton
- License: mit
- Created: 2016-05-19T15:22:02.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-04-14T19:00:41.000Z (about 6 years ago)
- Last Synced: 2025-04-10T19:50:34.238Z (about 1 year ago)
- Language: JavaScript
- Size: 28.3 KB
- Stars: 37
- Watchers: 3
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome_frontend_development_resources - find-imports - Find all imported modules in JavaScript files. (Utilities / React Components)
- awesome - find-imports - Find all imported modules in JavaScript files. (Utilities / React Components)
README
# find-imports [](https://travis-ci.org/cheton/find-imports) [](https://coveralls.io/github/cheton/find-imports?branch=master)
[](https://www.npmjs.com/package/find-imports)
Find all imported modules in JavaScript files. It's useful for bundling 3rd-party libraries into a vendor.js using webpack. For example:
```js
var webpack = require('webpack');
var findImports = require('find-imports');
// Webpack Configuration
module.exports = {
entry: {
app: [
'./src/index.js'
],
vendor: findImports('src/**/*.{js,jsx}', { flatten: true })
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js')
]
};
```
## Installation
```bash
npm install --save-dev find-imports
```
## Usage
The default options only return package imports:
```js
import findImports from 'find-imports';
const files = [
// glob pattern
'src/**/*.{js,jsx}',
// use negative glob pattern to exclude files
'!src/**/*.spec.js'
];
findImports(files);
// → { 'src/index.jsx':
// [ 'lodash',
// 'async',
// 'jsuri',
// 'react',
// 'react-dom',
// 'react-router' ] }
```
To flatten the output:
```js
findImports(files, { flatten: true });
// → [ 'lodash',
// 'async',
// 'jsuri',
// 'react',
// 'react-dom',
// 'react-router' ]
```
To return absolute and relative imports:
```js
findImports(files, {
absoluteImports: true,
relativeImports: true
});
// → { 'src/index.jsx':
// [ 'lodash',
// 'async',
// 'jsuri',
// 'react',
// 'react-dom',
// 'react-router',
// '/index.styl',
// './index.css' ] }
```
To only return absolute and relative imports (no packages):
```js
findImports(files, {
absoluteImports: true,
relativeImports: true,
packageImports: false
});
// → { 'src/index.jsx':
// [ '/index.styl',
// './index.css' ] }
```
## Options
Below are the options with their default values:
```js
{
flatten: false,
packageImports: true,
absoluteImports: false,
relativeImports: false
}
```
### flatten
Type: `Boolean` Default: false
Sets true to flatten the output and filter duplicate ones.
### packageImports
Type: `Boolean` Default: true
Sets true to return package imports.
### absoluteImports
Type: `Boolean` Default: false
Sets true to return absolute imports.
### relativeImports
Type: `Boolean` Default: false
Sets false to return relative imports.
## License
Copyright (c) 2016 Cheton Wu
Licensed under the [MIT License](LICENSE).