Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 网页包+;把手自定义辅助对象-不支持require.extension_Webpack_Handlebars.js - Fatal编程技术网

Webpack 网页包+;把手自定义辅助对象-不支持require.extension

Webpack 网页包+;把手自定义辅助对象-不支持require.extension,webpack,handlebars.js,Webpack,Handlebars.js,所以我有一个文本截断的小助手 'use strict'; var Handlebars = require('handlebars'); module.exports = function truncate(str, len, words) { var safe = Handlebars.Utils.escapeExpression(str); var tooLong = safe.length > len; var s_ = tooLong ? safe.substr(0,

所以我有一个文本截断的小助手

'use strict';
var Handlebars = require('handlebars');

module.exports = function truncate(str, len, words) {
  var safe = Handlebars.Utils.escapeExpression(str);
  var tooLong = safe.length > len;
  var s_ = tooLong ? safe.substr(0, len) : safe;
  if (words && tooLong) {
    var index = s_.lastIndexOf(' ');
    if (index !== -1) {
      s_ = s_.substr(0, index);
    }
  }
  return  new Handlebars.SafeString(tooLong ? s_ + ' …' : s_);
};
它在编译期间在控制台中抛出三个警告(但输出按预期工作)

我在谷歌上搜索,发现我必须以不同的方式在助手中导入把手

import * as Handlebars from 'handlebars/dist/cjs/handlebars';
但这更糟糕,并且会抛出错误

ERROR in Template execution failed: TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

ERROR in   TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
模板执行失败时出错:TypeError:无法分配给对象“#”的只读属性“exports”
TypeError中出错:无法分配给对象“#”的只读属性“exports”

那么,为助手添加把手的正确方式是什么呢。或者更好的方法-如何在网页包中全局包含所有正在使用的助手的把手,例如
handlebar.Utils

首先在nodejs中不可能在一个文件中使用
import
module.exports
。要导入某些内容,您必须使用
require
。也许这会有帮助:

var车把=需要(‘车把/dist/cjs/车把’)

您可以使用在webpack中实现把手。但要让它进入,需要很多配置

ERROR in Template execution failed: TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

ERROR in   TypeError: Cannot assign to read only property 'exports' of object '#<Object>'