简体   繁体   English

Grunt-无法读取GruntFile.js中的外部文本文件

[英]Grunt - Not able to read external text file in GruntFile.js

I have directory structure as below apps 我有以下应用程序的目录结构

 |--GruntFile.js 
 |--package.json
 |--js
     |--js.txt
     |--global
         |--backbone.js
         |--jquery.js
         |--lodash.js
         |--base.js 
|--css
    |--a.css
    |--b.css
    |--c.css

And have gruntFile.js as below: 并具有如下所示的gruntFile.js:

module.exports = function(grunt){
  require("matchdep").filterDev("grunt-*").forEach(grunt.loadNpmTasks);
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jsFiles: grunt.file.read('apps/js.txt'),

    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: [ '<%= jsFiles %>' ],
        dest: 'build/js/main_base.js'
      }
    },
 });

  grunt.registerTask('default', ['concat']);
};

My js.txt file contains js files list, which needed to be processed in sequence. 我的js.txt文件包含js文件列表,需要按顺序处理。 js.txt: js.txt:

'assets/js/global/lodash.js',
'assets/js/global/underscore.js',
'assets/js/global/backbone.js',
'assets/js/base.js

And my package.json is below: 我的package.json在下面:

{
  "name": "abc",
  "version": "1.0.0",
  "author": "Allen",
  "private": true,
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-concat": "^1.0.0",
    "grunt-contrib-cssmin": "^1.0.0",
    "grunt-contrib-handlebars": "^1.0.0",
    "grunt-contrib-less": "^1.2.0",
    "grunt-contrib-uglify": "^1.0.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-cssc": "^0.2.6",
    "grunt-htmlhint": "^0.9.12-fix",
    "matchdep": "^1.0.1"
  },
}

Now when i am compiling the files using grunt file, it is getting compile successfully, but the main_base.js in build folder is of size 0. Actually my concept goes like this, whatever the js files i am adding in my repo, i am maintaining it sequentially in js.txt , rather than maintaining them in GruntFile.js in concat task. 现在,当我使用grunt文件编译文件时,它已成功编译,但是build文件夹中的main_base.js的大小为0。实际上,无论我在回购中添加的js文件如何,我的概念都是这样的在js.txt中按顺序维护它,而不是在concat任务中在GruntFile.js中维护它们。 Similar thing i want to do will all my less files. 我想做的类似的事情将减少我所有的文件。 But it is not getting process. 但这还没有得到处理。 Any idea, where i am missing? 任何想法,我在哪里想念? Thanks. 谢谢。

According to your structure I guess: 根据您的结构,我猜:

grunt.file.read('apps/js.txt'),

should be: 应该:

grunt.file.read('js/js.txt'),

and instead of: 而不是:

[ '<%= jsFiles %>' ],

you should probably have: 您可能应该具有:

<%= jsFiles.toString().split("\n") %>,

to create an array out of the files (convert buffer to string, convert to array based on linebreaks) 从文件中创建一个数组(将缓冲区转换为字符串,根据换行符转换为数组)

maybe you could include the jsFiles array in your grunt file, or have it as an json file to simplify parsing. 也许您可以在您的grunt文件中包含jsFiles数组,或者将其作为json文件来简化解析。

I would suggest that you check out webpack, this is what I currently use to build my apps these days, and require the modules you need directly from your javascript source files. 我建议您检查一下webpack,这是我最近用于构建我的应用程序的功能,并且需要直接从javascript源文件中获取所需的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM