Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
File grunt将文件树注入javascript对象_File_Tree_Gruntjs_Inject - Fatal编程技术网

File grunt将文件树注入javascript对象

File grunt将文件树注入javascript对象,file,tree,gruntjs,inject,File,Tree,Gruntjs,Inject,有没有一种方法可以使用Grunt将文件树的结构读取到JSON对象中,然后将其注入javascript文件中 例如,假设我的项目有一个结构为 Assets ├╴docs │ └╴doc1.txt │ └╴doc2.txt │ ├╴effects │ └╴sound.mp3 我将能够注入一些.js文件 Assets = { docs : ['doc1.txt', 'd

有没有一种方法可以使用Grunt将文件树的结构读取到JSON对象中,然后将其注入javascript文件中

例如,假设我的项目有一个结构为

Assets
  ├╴docs                             
  │ └╴doc1.txt  
  │ └╴doc2.txt                  
  │ 
  ├╴effects 
  │ └╴sound.mp3
我将能够注入一些.js文件

Assets = { 
  docs : ['doc1.txt', 'doc2.txt.'],
  effects: [sound.mp3]       
}

其中初始资产等于一些while卡,如“此处”。

确定。我把这个过程分为两步

首先,我使用grunt tree模块构建文件系统的JSON表示。我将JSON文件保存到.tmp文件夹中的临时文件中

tree: {
  options: {
  },
  soundFileTree: {
    files: [
        {
            src: ['<%= yeoman.client %>/assets/audio/'],
            dest: '.tmp/soundTree.json',
        }
    ],
  },
},
injector: {
      // Inject JSON file into client and server files
      soundFileTree: {
        options: {
          transform: function(filePath) {
            var soundTreeJSONString = require(filePath); //you might have to adjust the files path
            return JSON.stringify(soundTreeJSONString);
          },
          starttag: '/* injector:soundFileTree */',
          endtag: '/* endinjector */',
        },
        files: {
          '<%= yeoman.client %>/treeTarget.js': ['.tmp/soundTree.json'],
        },
      }, 
   ...
}
var myVar=
/* injector:soundFileTree */
{"boxing":{"bell":"boxing/bell.wav","warning":"boxing/warning.wav"}}
/* endinjector */
;