以下 Node.js 選項設定是否要 polyfill 或模擬某些 Node.js 全域變數。
此功能由 webpack 內部的 NodeStuffPlugin
外掛提供。
false
物件
webpack.config.js
module.exports = {
//...
node: {
global: false,
__filename: false,
__dirname: false,
},
};
node
選項可以設定為 false
以完全關閉 NodeStuffPlugin
外掛程式。
布林值
'warn'
請參閱 Node.js 文件 以了解此物件的確切行為。
選項
true
:提供一個多載。false
:不提供任何東西。預期此物件的程式碼可能會因 ReferenceError
而崩潰。'warn'
:在使用 global
時顯示警告。布林值
'mock' | 'warn-mock' | 'node-module' | 'eval-only'
選項
true
:相對於 context
選項 的輸入檔案檔名。false
:Webpack 不會變更您的 __filename
程式碼,這表示您有常規的 Node.js __filename
行為。在 Node.js 環境中執行時的輸出檔案檔名。'mock'
:固定值 '/index.js'
。'warn-mock'
:使用固定值 '/index.js'
但顯示警告。'node-module'
:當啟用 output.module
時,將 CommonJS 模組中的 __filename
替換為 fileURLToPath(import.meta.url)
。'eval-only'
布林值
'mock' | 'warn-mock' | 'node-module' | 'eval-only'
選項
true
:相對於 context
選項 的輸入檔案目錄名稱。false
:Webpack 不會變更您的 __dirname
程式碼,這表示您有常規的 Node.js __dirname
行為。在 Node.js 環境中執行時的輸出檔案目錄名稱。'mock'
:固定值 '/'
。'warn-mock'
:使用固定值 '/'
但顯示警告。'node-module'
:當啟用 output.module
時,將 CommonJS 模組中的 __dirname
替換為 fileURLToPath(import.meta.url + "/..")
。'eval-only'