-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathreadme.md
207 lines (141 loc) · 6.39 KB
/
readme.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Node File Trace
[![CI Status](https://github.com/vercel/nft/actions/workflows/ci.yml/badge.svg)](https://github.com/vercel/nft/actions/workflows/ci.yml)
Used to determine exactly which files (including `node_modules`) are necessary for the application runtime.
This is similar to [@vercel/ncc](https://npmjs.com/package/@vercel/ncc) except there is no bundling performed and therefore no reliance on webpack. This achieves the same tree-shaking benefits without moving any assets or binaries.
## Usage
### Installation
```bash
npm i @vercel/nft
```
### Usage
Provide the list of source files as input:
```js
const { nodeFileTrace } = require('@vercel/nft');
const files = ['./src/main.js', './src/second.js'];
const { fileList } = await nodeFileTrace(files);
```
The list of files will include all `node_modules` modules and assets that may be needed by the application code.
### Options
#### Base
The base path for the file list - all files will be provided as relative to this base.
By default the `process.cwd()` is used:
```js
const { fileList } = await nodeFileTrace(files, {
base: process.cwd()
}
```
Any files/folders above the `base` are ignored in the listing and analysis.
#### Process Cwd
When applying analysis certain functions rely on the `process.cwd()` value, such as `path.resolve('./relative')` or even a direct `process.cwd()`
invocation.
Setting the `processCwd` option allows this analysis to be guided to the right path to ensure that assets are correctly detected.
```js
const { fileList } = await nodeFileTrace(files, {
processCwd: path.resolve(__dirname)
}
```
By default `processCwd` is the same as `base`.
#### Exports & Imports
By default tracing of the [Node.js "exports" and "imports" fields](https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_package_entry_points) is supported, with the `"node"`, `"require"`, `"import"` and `"default"` conditions traced as defined.
Alternatively the explicit list of conditions can be provided:
```js
const { fileList } = await nodeFileTrace(files, {
conditions: ['node', 'production']
});
```
Only the `"node"` export should be explicitly included (if needed) when specifying the exact export condition list. The `"require"`, `"import"` and `"default"` conditions will always be traced as defined, no matter what custom conditions are set.
#### Exports Only
When tracing exports the `"main"` / index field will still be traced for Node.js versions without `"exports"` support.
This can be disabled with the `exportsOnly` option:
```js
const { fileList } = await nodeFileTrace(files, {
exportsOnly: true
});
```
Any package with `"exports"` will then only have its exports traced, and the main will not be included at all. This can reduce the output size when targeting [Node.js 12.17.0](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#12.17.0) or newer.
#### Paths
> Status: Experimental. May change at any time.
Custom resolution path definitions to use.
```js
const { fileList } = await nodeFileTrace(files, {
paths: {
'utils/': '/path/to/utils/'
}
});
```
Trailing slashes map directories, exact paths map exact only.
#### Hooks
The following FS functions can be hooked by passing them as options:
* `readFile(path): Promise<string>`
* `stat(path): Promise<FS.Stats>`
* `readlink(path): Promise<string>`
* `resolve(id: string, parent: string): Promise<string | string[]>`
#### TypeScript
The internal resolution supports resolving `.ts` files in traces by default.
By its nature of integrating into existing build systems, the TypeScript
compiler is not included in this project - rather the TypeScript transform
layer requires separate integration into the `readFile` hook.
#### File IO Concurrency
In some large projects, the file tracing logic may process many files at the same time. In this case, if you do not limit the number of concurrent files IO, OOM problems are likely to occur.
We use a default of 1024 concurrency to balance performance and memory usage for fs operations. You can increase this value to a higher number for faster speed, but be aware of the memory issues if the concurrency is too high.
```js
const { fileList } = await nodeFileTrace(files, {
fileIOConcurrency: 2048,
});
```
#### Analysis
Analysis options allow customizing how much analysis should be performed to exactly work out the dependency list.
By default as much analysis as possible is done to ensure no possibly needed files are left out of the trace.
To disable all analysis, set `analysis: false`. Alternatively, individual analysis options can be customized via:
```js
const { fileList } = await nodeFileTrace(files, {
// default
analysis: {
// whether to glob any analysis like __dirname + '/dir/' or require('x/' + y)
// that might output any file in a directory
emitGlobs: true,
// whether __filename and __dirname style
// expressions should be analyzed as file references
computeFileReferences: true,
// evaluate known bindings to assist with glob and file reference analysis
evaluatePureExpressions: true,
}
});
```
#### Ignore
Custom ignores can be provided to skip file inclusion (and consequently analysis of the file for references in turn as well).
```js
const { fileList } = await nodeFileTrace(files, {
ignore: ['./node_modules/pkg/file.js']
});
```
Ignore will also accept a function or globs.
Note that the path provided to ignore is relative to `base`.
#### Cache
To persist the file cache between builds, pass an empty `cache` object:
```js
const cache = Object.create(null);
const { fileList } = await nodeFileTrace(['index.ts'], { cache });
// later:
{
const { fileList } = await nodeFileTrace(['index.ts'], { cache });
}
```
Note that cache invalidations are not supported so the assumption is that the file system is not changed between runs.
#### Reasons
To get the underlying reasons for individual files being included, a `reasons` object is also provided by the output:
```js
const { fileList, reasons } = await nodeFileTrace(files);
```
The `reasons` output will then be an object of the following form:
```js
{
[file: string]: {
type: 'dependency' | 'asset' | 'sharedlib',
ignored: true | false,
parents: string[]
}
}
```
`reasons` also includes files that were ignored as `ignored: true`, with their `ignoreReason`.
Every file is included because it is referenced by another file. The `parents` list will contain the list of all files that caused this file to be included.