如何从Webpack 4中的加载程序上下文访问编译器/编译/模块

如何从Webpack 4中的加载程序上下文访问编译器/编译/模块,webpack,webpack-4,Webpack,Webpack 4,我正在尝试编写一个自定义的网页加载程序 网页包文档提到,不推荐访问加载程序上下文上的\u编译器、\u编译和\u模块属性(),应避免访问 是否有更新的API允许我访问它们?加载依赖项后,您可以访问生成模块的源: // Index the modules generated in the child compiler by raw request. let byRawRequest = new Map for (let asset of compilation.modules) byRawReq

我正在尝试编写一个自定义的网页加载程序

网页包文档提到,不推荐访问加载程序上下文上的
\u编译器
\u编译
\u模块
属性(),应避免访问


是否有更新的API允许我访问它们?

加载依赖项后,您可以访问生成模块的源:

// Index the modules generated in the child compiler by raw request.
let byRawRequest = new Map
for (let asset of compilation.modules)
  byRawRequest.set(asset.rawRequest, asset)

// Replace the template requests with the result from modules generated in
// the child compiler.
for (let {node, request} of this._getAssetRequests()) {
  if (!byRawRequest.has(request))
    continue

  const ASSET = byRawRequest.get(request)
  const SOURCE = ASSET.originalSource().source()
  const NEW_REQUEST = execAssetModule(SOURCE)
  setResourceRequest(node, NEW_REQUEST)

  log(`Changed: ${prettyFormat({from: request, to: NEW_REQUEST})}`)
}
And execute the module's source with a VM:

function execAssetModule(code, path) {
  let script = new Script(code)
  let exports = {}
  let sandbox = {
    __webpack_public_path__: '',
    module: {exports},
    exports,
  }
  script.runInNewContext(sandbox)
  return sandbox.module.exports
}

对于任何想弄明白同样事情的人。我认为没有任何替代API。这个想法可能是加载程序不应该真的需要访问这些对象,如果您确实需要它们,您应该使用插件来代替(或者另外)

如果这些属性将来会被删除,下面是如何使用插件“将它们添加回去”:

//custom-plugin.js
常量NormalModule=require('webpack/lib/NormalModule')
类自定义插件{
应用(编译器){
compiler.hooks.compilation.tap('CustomPlugin',(编译)=>{
NormalModule.getCompilationHooks(compilation.loader.tap('CustomPlugin',(loader,module)=>{
加载器。\u customPlugin={
模块:模块,
编译器:编译器,
汇编:汇编,
}
})
});
}
}
module.exports=CustomPlugin
//custom-loader.js
module.exports=函数(源){
console.log({
模块:this.\u customPlugin.module==this.\u模块,//true
编译器:this.\u customPlugin.compiler==this.\u编译器,//true
编译:this.\u customPlugin.compilation==this.\u compilation,//true
});
返回源
}

我可能太累了,但我认为这并不能回答我的问题。我需要访问编译器/编译/模块的实例。模块的代码可以通过loader params访问,而无需任何这些。而且,这似乎是从这个答案复制过来的,这个答案是关于完全不同的东西。