編譯器
編譯器方法
run
開始編譯,並在編譯完成或因錯誤中止時回呼。
run(callback: (
error: Error, // Only including compiler-related errors, such as configuration errors, not including compilation errors
stats: Stats, // detailed information generated during the compilation
) => void): void;
警告
此 API 一次僅支援一個編譯。請在 compiler.run
回呼中呼叫 compiler.close
,並等待其完成,然後再執行 compiler.run
。並行編譯會損壞輸出檔案。
compiler.run((err, stats) => {
// Deal with the compiler errors
handlerCompilerError(err);
// Deal with the compilation errors
handlerModuleErrors(stats.toJson().errors);
// Deal with the result
handleBuildResult(stats);
// End this compilation
compiler.close(closeErr => {
// Start a new compilation
compiler.run((err, stats) => {});
});
});
watch
監視檔案和目錄,在它們變更後啟動編譯程序,並在每次編譯完成或因錯誤中止時回呼。
watch(
watchOptions: WatchOptions, // options for starting the watching
handler: (error: Error, stats: Stats) => void // callback when every compilation ends
): Watching; // watching controller
警告
此 API 一次僅支援一個編譯。請在 compiler.watch
回呼中呼叫 compiler.close
,並等待其完成,然後再執行 compiler.watch
。並行編譯會損壞輸出檔案。
const watching = compiler.watch(
{
aggregateTimeout: 300,
poll: undefined,
},
(err, stats) => {
// Deal with the result
handleBuildResult(stats);
},
);
Watching 物件提供下列方法
watch
:
- 類型:
(files: string[], dirs: string[], missing: string[]): void
- 用法: 新增需要監視的檔案和目錄。
invalidate
:
- 類型:
(callback: () => void): void
- 用法: 立即結束此輪監視,並使用目前記錄的檔案變更啟動編譯,而不停止監視器。
suspend
:
- 類型:
(): void
- 用法: 進入僅監視的狀態,且不會啟動新的編譯。
resume
:
- 類型:
(): void
- 用法: 退出僅監視的狀態,並使用目前記錄的檔案變更啟動編譯。
close
:
- 類型:
(callback: () => void): void
- 用法: 停止監視器。
compile
建立編譯並執行它,這是 compiler.run
和 compiler.watch
所依賴的基本方法。
compile(
callback: (compilation: Compilation) => void // callback after this compilation
): void
close
關閉目前的編譯器,並在此期間處理快取等低優先順序工作。
close(
callback: (err: Error) => void // callback after closing
): void;
getInfrastructureLogger
建立一個與任何編譯無關的記錄器物件,用於列印全域記錄。
getInfrastructureLogger(name: string): Logger;
getCache
建立快取物件,以在建置過程中分享資料。
getCache(name: string): CacheFacade;
purgeInputFileSystem
停止輸入檔案系統的讀取迴圈,其內部包含計時器,並可能導致在呼叫 compiler.close
後,程序仍然無法退出。
purgeInputFileSystem(): void;
createChildCompiler
允許在 Rspack 內部執行另一個 Rspack 實例。但是,作為具有不同設定和組態的子項。它會複製父項(或頂層編譯器)的所有 hooks 和外掛程式,並建立子 Compiler
實例。傳回建立的 Compiler
。
createChildCompiler(
compilation: Compilation,
compilerName: string,
compilerIndex: number,
outputOptions: OutputOptions,
plugins: RspackPlugin[]
): Compiler;
runAsChild
執行子編譯器,它將執行完整的編譯並產生資產。
runAsChild(
callback(
err: Error, // error related to the child compiler
entries: Chunk[], // chunks generated by the child compiler
compilation: Compilation, // the compilation created by the child compiler
): void;
): void;
isChild
此編譯器是否為子編譯器。
編譯器屬性
hooks
請參閱編譯器 hooks以取得更多詳細資訊。
rspack
取得 @rspack/core 的 exports 以取得相關的內部物件。當您無法直接參考 @rspack/core
或有多個 Rspack 實例時,這特別有用。
常見的範例是在 Rspack 外掛程式中存取 sources 物件
const { RawSource } = compiler.rspack.sources;
const source = new RawSource('console.log("Hello, world!");');
webpack
與 compiler.rspack
等效,此屬性用於與 webpack 外掛程式相容。
如果您正在開發的 Rspack 外掛程式需要與 webpack 相容,您可以使用此屬性來取代 compiler.rspack
。
console.log(compiler.webpack === compiler.rspack); // true
name
取得名稱
- 對於根編譯器,它等同於
name
。
- 對於子編譯器,它是傳遞到
createChildCompiler
的值。
- 對於 MultiCompiler 和 KV 形式,它是索引鍵。
context
目前專案根目錄
- 透過
new Compiler
建立,它是傳入的值。
- 透過
rspack({})
建立,它是context 設定。
root
取得子編譯器樹狀結構的根。
options
- 類型:
RspackOptionsNormalized
取得此編譯器使用的完整選項。
watchMode
是否透過 compiler.watch
啟動。
watching
取得 watching 物件,請參閱watch 方法以取得更多詳細資訊。
running
目前是否正在執行編譯。
inputFileSystem
取得用於從檔案系統讀取的 proxy 物件,其內部具有快取等最佳化,可減少重複讀取相同檔案。
outputFileSystem
取得用於寫入檔案系統的 proxy 物件,預設為 fs
。
watchFileSystem
取得用於監看檔案或目錄變更的代理物件,該物件提供一個 watch
方法來開始監看,並在回呼函式中傳入已變更和已移除的項目。
MultiCompiler
MultiCompiler
模組允許 Rspack 在不同的編譯器中執行多個配置。如果 Rspack 的 JavaScript API 中的 options 參數是一個選項陣列,Rspack 會應用不同的編譯器,並在所有編譯器執行後呼叫回呼函式。
const rspack = require('@rspack/core');
rspack(
[
{ entry: './index1.js', output: { filename: 'bundle1.js' } },
{ entry: './index2.js', output: { filename: 'bundle2.js' } },
],
(err, stats) => {
process.stdout.write(stats.toString() + '\n');
},
);
它也可以透過 new MultiCompiler
建立。
const compiler1 = new Compiler({
/* */
});
const compiler2 = new Compiler({
/* */
});
new MultiCompiler([compiler1, compiler2]);
new MultiCompiler([compiler1, compiler2], {
parallelism: 1, // the maximum number of parallel compilers
});
new MultiCompiler({
name1: compiler1,
name2: compiler2,
});
MultiCompiler
也提供 Compiler
的一些方法和屬性。
MultiCompiler 方法
setDependencies
使用 compiler.name
作為識別符,指定編譯器之間的依賴關係,以確保編譯器的執行順序。
setDependencies(compiler: Compiler, dependencies: string[]): void;
validateDependencies
檢查編譯器之間的依賴關係是否合法。如果存在循環依賴或缺少依賴,它將觸發回呼函式。
validateDependencies(
callback: (err: Error) => void; // callback when there is an error
): boolean
run
根據依賴關係執行每個編譯器的 run
方法,以啟動編譯過程。
run(callback: (err: Error, stats: MultiStats) => void): void;
watch
根據依賴關係執行每個編譯器的 watch
方法,以開始監看,並在檔案變更後啟動編譯過程。
watch(
watchOptions: WatchOptions,
handler: (err: Error, stats: MultiStats) => void,
): MultiWatching
close
執行每個編譯器的 close
方法以關閉它們,並在此期間處理低優先級任務,例如快取。
close(callback: (err: Error) => void): void;
purgeInputFileSystem
執行每個編譯器的 purgeInputFileSystem
以停止檔案系統的讀取迴圈。
purgeInputFileSystem(): void;
getInfrastructureLogger
建立一個與任何編譯無關的記錄器物件,用於列印全域記錄。
getInfrastructureLogger(name: string): Logger;
與 compilers[0].getInfrastructureLogger()
相同。
MultiCompiler 屬性
compilers
取得所有包含的編譯器。
options
唯讀
- 類型:
RspackOptionsNormalized[]
取得編譯器使用的所有完整選項。
inputFileSystem
僅寫
設定用於從每個編譯器的檔案系統讀取的代理物件。
outputFileSystem
僅寫
設定用於從每個編譯器的檔案系統寫入的代理物件。
watchFileSystem
僅寫
設定用於監看每個編譯器的檔案或目錄變更的代理物件。
running
目前是否正在執行編譯。