【问题标题】:<%= %> variable syntax in Gruntfile throws "unable to read"Gruntfile 中的 <%= %> 变量语法抛出“无法读取”
【发布时间】:2016-08-09 05:32:56
【问题描述】:

我在我的 Gruntfile 中使用以下内容:

grunt.initConfig({
    assets: grunt.option('assets'),
    config: grunt.file.readJSON(path.join('<%= assets %>', 'config.json')) || grunt.file.readJSON('./defaults.json'),
    ...
})

当我执行它时,它会抛出:

>> Error: Unable to read "<%= assets %>/config.json" file (Error code: ENOENT).
    >> at Object.util.error (/.../prj/node_modules/grunt-legacy-util/index.js:54:39)
    >> at Object.file.read (/.../prj/node_modules/grunt/lib/grunt/file.js:247:22)
    >> at Object.file.readJSON (/.../prj/node_modules/grunt/lib/grunt/file.js:253:18)
    >> at Object.module.exports (/.../prj/Gruntfile.js:10:28)
    >> at loadTask (/.../prj/node_modules/grunt/lib/grunt/task.js:325:10)
    >> at Task.task.init (/.../prj/node_modules/grunt/lib/grunt/task.js:437:5)
    >> at Object.grunt.tasks (/.../prj/node_modules/grunt/lib/grunt.js:120:8)
    >> at Object.module.exports [as cli] (/.../prj/node_modules/grunt/lib/grunt/cli.js:38:9)
    >> at Object.<anonymous> (/usr/local/lib/node_modules/grunt-cli/bin/grunt:45:20)
    >> at Module._compile (module.js:425:26)

想知道这是否是因为assets var 在我尝试使用它时没有定义?还是不允许以这种方式使用 语法?

根据this answer,它看起来应该可以工作——我发现这是因为以前我只是使用var assets = grunt.option('assets'),但是那个出于某种原因抛出了SyntaxError: Unexpected token var。 (在我弄乱它之前,它看起来是这样的:)

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt)

    var util = require('util'),
        path = require('path'),
        pkg = require('./package.json')

    var assets = grunt.option('assets'),
    var config = grunt.file.readJSON(path.join(assets, 'config.json')) || grunt.file.readJSON('./defaults.json')

    grunt.initConfig({
        ...
    })

像这样使用模块或在 gruntfile 中声明变量的正确的 grunt 方式是什么?和/或,我可以解决Unexpected token var 的问题吗?

注意:这不是我无法加载的配置文件,而是来自grunt.option() 的资产路径似乎没有被解释)

【问题讨论】:

  • 谁投了反对票,请说明你的理由。这是一个完全有效的编程问题 +1。
  • var assets = grunt.option('assets') , 后面多了一个逗号。删除它或删除下一行中的 var
  • 保持开放以奖励积分,但我履行了庄严的职责并投票结束。

标签: javascript gruntjs gruntfile


【解决方案1】:

似乎您想读取自定义 json 文件。请记住,即使是 grunt 脚本也只是 JavaScript,并且 node 需要正常工作。所以你可以做类似的事情

 var conf = grunt.file.exists(assets + '/ '+ 'config.json') ? require(assets + '/ '+ 'config.json') : {};

然后您可以在任何地方使用您的config 变量。

conf.foo || 'default'
conf.bar

在任何一种情况下,您都需要在使用前声明assets 变量。在 require 或 initConfig

更新

另外,
var assets = grunt.option('assets'), 之后有一个额外的逗号,要么删除它,要么删除下一行中的 var

【讨论】:

  • 配置文件加载正常;我认为它是grunt.option('assets'),它正在打破
  • 那你在哪里定义assets?是变量还是参数?
  • 原来我的名字是var assets = grunt.option('assets')。然后它开始盯着我看关于在 gruntfile 中使用 var,所以我转而尝试使用 assets: grunt.option('assets') 来加载 arg,以便 assets 成为 grunt 变量。
  • 我明白了。好的尝试不使用 path.join()。尝试使用资产自己加入字符串以查看它是否有效,例如config: grunt.file.readJSON('&lt;%= assets %&gt;/'config.json')
  • Hrm,不,即使那样也行不通。可能根本不能像这样使用它——就像@ahmadbamieh 的回答所说的那样。
【解决方案2】:

您不能在 grunt.init( { 大括号内声明 var assets = ..,因为这不是有效的 JS 语法。为了让它理解&lt;%= %&gt; 语法,您需要做的是这样声明:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
        options: {
            banner: '/*! <%= pkg.name %>'
            // pkg is a property on the json object passed into initConfig
...

【讨论】:

  • 如果你在 initCongfig 之前声明 assets 变量,你可以做pkg: assets,。不确定你用 grunt.option() 做什么
  • 不确定这与我正在做的事情有何不同?除此之外,您的示例读取了一个 json 文件,而我的示例正在加载命令行 arg?
【解决方案3】:

&lt;%= %&gt;是grunt指定的插值字符串,如果你在非grunt参数中使用它,它不会被插值(比如你在path.join中使用它的情况)。

要让它发挥作用,你必须使用

grunt.template.process('<%= asset %>', {
   data: {
      assets: grunt.option('assets')
   }
})

【讨论】:

  • 好的,这就解释了为什么插值语法不起作用。知道为什么我原来使用 var 的方式不再有效了吗?
  • 是的,插值取自对象本身的'this',因此为了获得var的值,您必须将其放入initConfig的对象中,例如initConfig({assets: assets, //...}
猜你喜欢
  • 1970-01-01
  • 2013-02-11
  • 2014-10-03
  • 1970-01-01
  • 2019-11-21
  • 2020-08-02
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
相关资源
最近更新 更多