0

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:

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:

'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:

{
  "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. Similar thing i want to do will all my less files. But it is not getting process. Any idea, where i am missing? Thanks.

kcs
  • 31
  • 2
  • 7

1 Answers1

0

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.

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.

user1245172
  • 153
  • 2
  • 5