用於確切確定應用程序運行時需要確切確定哪些文件(包括node_modules )。
這類似於 @vercel/ncc,除了沒有執行捆綁,因此也不依賴WebPack。這在不移動任何資產或二進製文件的情況下實現了相同的震撼人心的好處。
npm i @vercel/nft提供源文件列表作為輸入:
const { nodeFileTrace } = require ( '@vercel/nft' ) ;
const files = [ './src/main.js' , './src/second.js' ] ;
const { fileList } = await nodeFileTrace ( files ) ;文件列表將包括應用代碼可能需要的所有node_modules和資產。
文件列表的基本路徑 - 所有文件將相對於此基礎提供。
默認情況下,使用process.cwd() :
const { fileList } = await nodeFileTrace ( files , {
base : process . cwd ( ) ,
} ) ;在清單和分析中忽略了base上方的任何文件/文件夾。
應用分析時path.resolve('./relative')某些函數依賴process.cwd() process.cwd() 。
設置processCwd選項允許將此分析引導到正確的路徑,以確保正確檢測到資產。
const { fileList } = await nodeFileTrace ( files , {
processCwd : path . resolve ( __dirname ) ,
} ) ;默認情況下, processCwd與base相同。
默認情況下,支持node.js“導出”和“導入”字段的跟踪,並在"node" , "require" , "import"和"default"條件下按照定義的定義。
另外,可以提供明確的條件清單:
const { fileList } = await nodeFileTrace ( files , {
conditions : [ 'node' , 'production' ] ,
} ) ;在指定確切的導出條件列表時,僅應明確包含"node"導出。無論設置哪種自定義條件,都將始終將"require" , "import"和"default"條件按定義。
跟踪導出時, "main" /索引字段仍將被追溯到沒有"exports"支持的Node.js版本。
可以通過exportsOnly選項禁用這一點:
const { fileList } = await nodeFileTrace ( files , {
exportsOnly : true ,
} ) ;然後,任何帶有"exports"軟件包都只能跟踪其出口,並且根本不包括在內。當定位Node.js 12.17.0或更新時,這可以減小輸出大小。
狀態:實驗。可能隨時改變。
自定義分辨率路徑定義要使用。
const { fileList } = await nodeFileTrace ( files , {
paths : {
'utils/' : '/path/to/utils/' ,
} ,
} ) ;拖延斜線映射目錄,精確路徑僅映射精確。
以下FS函數可以通過將它們作為選項誘導:
readFile(path): Promise<string>stat(path): Promise<FS.Stats>readlink(path): Promise<string>resolve(id: string, parent: string): Promise<string | string[]>在提供自定義分解掛鉤時,您將負責根據id輸入返回一個或多個絕對路徑以解決文件。但是,您可能只想在某些情況下增加或覆蓋解決行為。您可以通過導入nft的基礎解析器。內置的resolve函數期望需要從鉤子轉發的其他參數
resolve(id: string, parent: string, job: Job, isCjs: boolean): Promise<string | string[]>這是一個示例,顯示一個ID已解決到定制路徑,而所有其他路徑都被內置的解析器解決
const { nodeFileTrace , resolve } = require ( '@vercel/nft' ) ;
const files = [ './src/main.js' , './src/second.js' ] ;
const { fileList } = await nodeFileTrace ( files , {
resolve : async ( id , parent , job , isCjs ) => {
if ( id === './src/main.js' ) {
return '/path/to/some/resolved/main/file.js' ;
} else {
return resolve ( id , parent , job , isCjs ) ;
}
} ,
} ) ; 默認情況下,內部分辨率支持在跟踪中解析.ts文件。
通過將打字稿編譯器集成到現有的構建系統中的性質,該項目不包括在此項目中 - 而不是打字稿轉換層需要將整合到readFile掛鉤中。
在某些大型項目中,文件跟踪邏輯可能同時處理許多文件。在這種情況下,如果您不限制並發文件的數量IO,則可能會出現OOM問題。
我們使用1024個並發的默認值來平衡FS操作的性能和內存使用量。您可以將此值提高到更高的速度,但是如果並發太高,請注意內存問題。
const { fileList } = await nodeFileTrace ( files , {
fileIOConcurrency : 2048 ,
} ) ; 分析選項允許自定義應進行多少分析,以精確地解決依賴項列表。
默認情況下,盡可能多地進行分析,以確保沒有任何可能的文件被排除在跟踪中。
為了禁用所有分析,請設置analysis: false 。另外,可以通過以下方式自定義單個分析選項:
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 ,
} ,
} ) ; 可以提供自定義忽略以跳過文件包含(因此,對文件的分析也依次分析)。
const { fileList } = await nodeFileTrace ( files , {
ignore : [ './node_modules/pkg/file.js' ] ,
} ) ;忽略也將接受功能或地球。
請注意,提供的路徑相對於base 。
要堅持構建之間的文件緩存,請傳遞一個空的cache對象:
const cache = Object . create ( null ) ;
const { fileList } = await nodeFileTrace ( [ 'index.ts' ] , { cache } ) ;
// later:
{
const { fileList } = await nodeFileTrace ( [ 'index.ts' ] , { cache } ) ;
}請注意,不支持緩存無效,因此假設是在運行之間沒有更改文件系統。
為了獲取包含各個文件的根本原因,輸出也提供了一個reasons :
const { fileList , reasons } = await nodeFileTrace ( files ) ;然後,輸出將成為以下形式的對象的reasons :
{
[ file : string ] : {
type : 'dependency' | 'asset' | 'sharedlib' ,
ignored : true | false ,
parents : string [ ]
}
} reasons還包括被ignored: true ,以其ignoreReason 。
每個文件都包含在另一個文件中。 parents列表將包含所有導致此文件的文件的列表。