static-analysis
Performs Static Analysis On JavaScript Programs To Find Out All Dependencies That Stem From The Given File.
yarn add -E static-analysis
- Table Of Contents
- API
-
async staticAnalysis(path: string, config: Config): Array<Detection>
sort(detections: Array<Detection>): {}
- Copyright
The package is available by importing its default function:
import staticAnalysis from 'static-analysis'
Detects all dependencies in a file and their dependencies recursively. If the package exports main
over module
, the hasMain
property will be added. This function can be useful to find out all files to pass to the Google Closure Compiler, for example, which is what Depack does to bundle frontend code and compile Node.js packages.
Config
: The configuration for staticAnalysis.
Name | Type | Description | Default |
---|---|---|---|
nodeModules | boolean | Whether to include packages from node_modules in the output. |
true |
For example, for the given file:
import read from '@wrote/read'
import { resolve } from 'path'
import { render } from 'preact'
const Component = require('./Component');
(async () => {
const file = await read(resolve('example'))
render(<Component>{file}</Component>, document.body)
})()
Static Analysis can detect matches using the following script:
/* yarn example/ */
import staticAnalysis from 'static-analysis'
(async () => {
const res = await staticAnalysis('example/source.js')
console.log(res)
})()
[ { entry: 'node_modules/@wrote/read/src/index.js',
packageJson: 'node_modules/@wrote/read/package.json',
version: '1.0.2',
name: '@wrote/read',
from: [ 'example/source.js' ] },
{ internal: 'path', from: [ 'example/source.js' ] },
{ entry: 'node_modules/preact/dist/preact.mjs',
packageJson: 'node_modules/preact/package.json',
version: '8.4.2',
name: 'preact',
from: [ 'example/source.js' ] },
{ entry: 'example/Component.jsx',
from: [ 'example/source.js' ] },
{ internal: 'fs',
from: [ 'node_modules/@wrote/read/src/index.js' ] },
{ entry: 'node_modules/@wrote/read/node_modules/catchment/src/index.js',
packageJson: 'node_modules/@wrote/read/node_modules/catchment/package.json',
version: '3.2.1',
name: 'catchment',
from: [ 'node_modules/@wrote/read/src/index.js' ] },
{ internal: 'stream',
from:
[ 'node_modules/@wrote/read/node_modules/catchment/src/index.js' ] },
{ entry: 'node_modules/erotic/src/index.js',
packageJson: 'node_modules/erotic/package.json',
version: '2.0.2',
name: 'erotic',
from:
[ 'node_modules/@wrote/read/node_modules/catchment/src/index.js' ] },
{ entry: 'node_modules/@artdeco/clean-stack/src/index.js',
packageJson: 'node_modules/@artdeco/clean-stack/package.json',
version: '1.0.1',
name: '@artdeco/clean-stack',
from:
[ 'node_modules/@wrote/read/node_modules/catchment/src/index.js',
'node_modules/erotic/src/callback.js' ] },
{ entry: 'node_modules/@wrote/read/node_modules/catchment/src/lib/index.js',
from:
[ 'node_modules/@wrote/read/node_modules/catchment/src/index.js' ] },
{ entry: 'node_modules/erotic/src/lib.js',
from:
[ 'node_modules/erotic/src/index.js',
'node_modules/erotic/src/callback.js' ] },
{ entry: 'node_modules/erotic/src/callback.js',
from: [ 'node_modules/erotic/src/index.js' ] },
{ internal: 'os',
from: [ 'node_modules/@artdeco/clean-stack/src/index.js' ] } ]
Detection
: The module detection result.
Name | Type | Description |
---|---|---|
entry | string | The path to the JavaScript file to be required. If an internal Node.js package is required, it's name is found in the internal field. |
from* | Array<string> | The file in which the dependency was found. |
packageJson | string | The path to the package.json file of the dependency if it's a module. |
name | string | The name of the package. |
version | string | The version of the package. |
internal | string | If it's an internal NodeJS dependency, such as fs or path , contains its name. |
hasMain | boolean | Whether the entry from the package was specified via the main field and not module field. |
It is possible to ignore node_modules
folders. In this case, only dependencies that start with ./
or /
will be included in the output.
import staticAnalysis from 'static-analysis'
(async () => {
const res = await staticAnalysis('example/source.js', {
nodeModules: false,
})
console.log(res)
})()
[ { entry: 'example/Component.jsx',
from: [ 'example/source.js' ] } ]
Sorts the detected dependencies into commonJS modules, packageJsons and internals.
import staticAnalysis, { sort } from 'static-analysis'
(async () => {
const d = await staticAnalysis('example/source.js')
const sorted = sort(d)
console.log(sorted)
})()
{ commonJsPackageJsons: [],
packageJsons:
[ 'node_modules/@wrote/read/package.json',
'node_modules/preact/package.json',
'node_modules/@wrote/read/node_modules/catchment/package.json',
'node_modules/erotic/package.json',
'node_modules/@artdeco/clean-stack/package.json' ],
commonJs: [],
js:
[ 'node_modules/@wrote/read/src/index.js',
'node_modules/preact/dist/preact.mjs',
'example/Component.jsx',
'node_modules/@wrote/read/node_modules/catchment/src/index.js',
'node_modules/erotic/src/index.js',
'node_modules/@artdeco/clean-stack/src/index.js',
'node_modules/@wrote/read/node_modules/catchment/src/lib/index.js',
'node_modules/erotic/src/lib.js',
'node_modules/erotic/src/callback.js' ],
internals: [ 'path', 'fs', 'stream', 'os' ],
deps:
[ '@wrote/read',
'preact',
'catchment',
'erotic',
'@artdeco/clean-stack' ] }
![]() |
© Art Deco for Depack 2019 |
![]() |
Tech Nation Visa Sucks |
---|