問題描述
用於文件、子文件的 Webpack 加載器並將它們添加到跟踪列表 (Webpack loader for file, subfiles and add them to tracking list)
我在第 3 方應用程序中創建了地圖+tilemap 項目。整個項目是一組文件,主文件(XML)代表 2D 遊戲關卡地圖,其他一些文件(子文件)代表圖形和瓷磚地圖。
我正在嘗試創建一個 Webpack Loader,它將將整個地圖/tilemap 項目編譯並轉換為 JSON 對象,在 javascript 中使用起來很舒服。而且我仍然不知道如何:
如何訪問子文件(取自主文件的相對路徑),訪問當前目錄的方法是什麼(在哪裡放置主文件),以及主文件的名稱?
如何向Webpack解釋跟踪所有子文件的變化,
參考解法
方法 1:
For the first question, webpack loader is expose the file info by
this
, you can do like this:module.exports = function (source) { const loaderContext = this; const { sourceMap, rootContext, resourcePath } = loaderContext; const sourceRoot = path.dirname(path.relative(context, resourcePath)); ... }
For the second question, i think that maybe you can use the
module.hot
to track the subfiles change, like this:if (module.hot) { module.hot.accept('filepath', ()=>{ ... }) }
(by Epsiloncool、wuliu)
參考文件