Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cheton/find-imports
Find all imported modules in JavaScript files.
https://github.com/cheton/find-imports
Last synced: 10 days 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 (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-14T19:00:41.000Z (over 4 years ago)
- Last Synced: 2024-10-30T00:00:12.968Z (20 days ago)
- Language: JavaScript
- Size: 28.3 KB
- Stars: 37
- Watchers: 4
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# find-imports [![build status](https://travis-ci.org/cheton/find-imports.svg?branch=master)](https://travis-ci.org/cheton/find-imports) [![Coverage Status](https://coveralls.io/repos/github/cheton/find-imports/badge.svg?branch=master)](https://coveralls.io/github/cheton/find-imports?branch=master)
[![NPM](https://nodei.co/npm/find-imports.png?downloads=true&stars=true)](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: falseSets true to flatten the output and filter duplicate ones.
### packageImports
Type: `Boolean` Default: trueSets true to return package imports.
### absoluteImports
Type: `Boolean` Default: falseSets true to return absolute imports.
### relativeImports
Type: `Boolean` Default: falseSets false to return relative imports.
## License
Copyright (c) 2016 Cheton Wu
Licensed under the [MIT License](LICENSE).