Webpack (二) 自定义Loader

API文档

https://webpack.js.org/api/loaders/

Loader要素

  • 资源内容
    资源内容通过my-kotlin-loader.js导出的函数的形式参数传递进来.
function custom_loader(file_content) {
    console.log({ '编译Kotlin': file_content }); // 需要处理的文件内容通过形式参数传递
}
module.exports = custom_loader;
  • 资源路径this.resourcePath及其查询字符串this.resourceQuery
import './main.kt?ok';
// this.resourcePath === 'C:UsersAdministratorDesktop
ode_loadersrcmain.kt'
// this.resourceQuery === '?ok'
  • 加载器的查询字符串this.query
module: {
      rules: [
            { test: /.kt$/, use: path.resolve(DIR_SRC, 'my-kotlin-loader.js?main_function=main') }, // this.query === '?main_function=main'
      ]
}
  • 请求字符串this.request
    this.request提供一个完整的请求URI描述:
console.log('请求', this.request);
// 请求 C:UsersAdministratorDesktop
ode_loadersrcmy-kotlin-loader.js?main_function=main!C:UsersAdministratorDesktop
ode_loadersrcmain.kt?ok

Github示例

https://github.com/develon2015/eg-My-Kotlin-Loader

原文地址:https://www.cnblogs.com/develon/p/13588486.html