7

I have been looking at examples of how to use the handlebars-loader with webpack but none seem to be working with webpack 4.

Error

ERROR in ./src/templates/property-list-item.hbs Module build failed: TypeError: Cannot read property 'handlebarsLoader' of undefined at getLoaderConfig (/Users/Sam/Desktop/mettamware/Public/js/node_modules/handlebars-loader/index.js:24:37) at Object.module.exports (/Users/Sam/Desktop/mettamware/Public/js/node_modules/handlebars-loader/index.js:32:15) @ ./src/property-list.js 5:0-58 40:23-31 @ ./src/app.js


When I look in node_modeules/handlerbars-loader/index.js, the offending function is this

function getLoaderConfig(loaderContext) {
  var query = loaderUtils.getOptions(loaderContext) || {};
  var configKey = query.config || 'handlebarsLoader';
  var config = loaderContext.options[configKey] || {};
  delete query.config;
  return assign({}, config, query);
}

My webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.hbs$/,
        use: [{
          loader: "handlebars-loader",
          options: {
            helperDirs: path.resolve(__dirname, "./src/helpers")
          }
        }]
      }
    ]
  },
  node: {
    fs: 'empty'
  }
};

If anyone can help I would greatly appreciate it. I've been searching for hours for a solution and have tried lots to things but am not getting anywhere.

4

2 回答 2

2

在 Webpack 4loaderContext.options中已被替换为loaderContext.rootConfig.

已经对包进行了此提交handlebars-loader以使其与 Webpack 4 兼容,但尚未发布。

目前我已经卸载handlebars-loader并正在使用这个 fork

以下步骤解决了我的问题:

运行这两个命令
npm uninstall handlebars-loader
npm install @icetee/handlebars-loader

webpack.config.js,替换

loader: "handlebars-loader"

loader: "@icetee/handlebars-loader"
于 2018-03-20T17:46:38.567 回答
2

同样在升级旧的 webpack 4...

显然,过去可以在配置上设置自定义属性。那就是它正在寻找的地方handlebarsLoader

当您在其上设置handleBarLoader属性时,它会发出此错误。

对于加载器选项:webpack 2 不再允许配置中的自定义属性。

应该更新加载器以允许通过 module.rules 中的加载器选项传递选项。

在更新加载器之前,可以使用 LoaderOptionsPlugin 将这些选项传递给加载器:

     plugins: [
       new webpack.LoaderOptionsPlugin({
         // test: /\.xxx$/, // may apply this only for some modules
         options: {
           handlebarsLoader: ...
         }
       })
     ]

就我而言,现在这样设置:

     plugins: [
       new webpack.LoaderOptionsPlugin({
         options: {
           handlebarsLoader: {}
         }
       })
     ]
于 2018-03-02T19:59:28.593 回答