【问题标题】:Dom_munger issue with Node 7.7.3 - Path must be a stringNode 7.7.3 的 Dom_munger 问题 - 路径必须是字符串
【发布时间】:2017-10-04 04:39:56
【问题描述】:

我正在尝试更新应用程序以支持 Node -v 7.7.3。但是当我按照以下方式运行 grunt 任务 dom_munger 时:

dom_munger:{
  read: {
    options: {
      read:[
        {selector:'script[data-concat!="false"]',attribute:'src',writeto:'appjs', isPath: true},
        {selector:'link[rel="stylesheet"][data-concat!="false"]',attribute:'href',writeto:'appcss'}
      ]
    },
    src: 'app/index.html'
  }
}

我收到错误:

Warning: Path must be a string. Received [ 'app/index.html' ] Use --force to continue.

我想知道是否有办法重写上面的 grunt 任务,或者是否有替代 dom_munger 的好方法。任何帮助将不胜感激。

【问题讨论】:

    标签: node.js npm gruntjs


    【解决方案1】:

    根据grunt-dom-munger Github:

    isPath 为真时,提取的值假定为文件 引用及其路径是相对于 Gruntfile.js 而不是 而不是他们从中读取的文件。

    尝试删除 isPath 属性,或更改它以匹配从 Gruntfile 到 index.html 文件的路径。

    【讨论】:

    • 谢谢!但这似乎只有在 Grunt 和 Index 位于同一文件夹结构中时才有效。我的结构如下所示: - /app -index.html - gruntfile.js 如果没有属性“isPath”,dom_munger 将在与 Gruntfile 所在位置相同的目录中查找 js 文件。
    • @sandrasvensson 但是您是否尝试删除 isPath 属性?好像解决了this similar issue
    【解决方案2】:

    谢谢!但这似乎只有在 Grunt 和 Index 位于同一文件夹结构中时才有效。我的结构如下所示:

    - /app
        -index.html
    - gruntfile.js
    

    如果没有属性“isPath”,dom_munger 将在与 Gruntfile 所在的目录相同的目录中查找 js 文件。

    【讨论】:

      【解决方案3】:

      移除 isPath: true,并确保 src 属性中的路径相对于 Gruntfile.js 而不是读取它们的文件。

      如果需要在路径中进行替换:

      dom_munger: {
        replacePath: {
          options: {
            callback: function($, file){
              var scripts = $('script[data-concat!="false"]');
              // NOTE: path is made relative to the Gruntfile.js rather than the file they're read from
              for(var i=0, s, il=scripts.length; i<il; i++){
                s = scripts[i];
                if(s.attribs.src){
                  s.attribs.src = s.attribs.src.replace('../', '');
                }
              }
            }
          },
          src: 'temp/index.html'
        },
        read: {
          options: {
            read: [
              {selector:'script[data-concat!="false"]',attribute:'src',writeto:'appjs'},
              {selector:'link[rel="stylesheet"][data-concat!="false"]',attribute:'href',writeto:'appcss'}
            ]
          },
          src: 'temp/index.html'
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-23
        • 2017-05-18
        • 1970-01-01
        • 2018-12-08
        • 1970-01-01
        • 2017-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多