How to read an external file list with grunt

Viewed 263

I'm trying to concatenate and then babelify and uglify files with Grunt.

I'd like to read an external file list, from a file where the files are written one for each line, newline separated.

I'm trying to use the following GruntFile.js, but Grunt says (after I added the src=['<%= jsFiles.toString().split("\n") %>'] line):

Running "browserify:dist1" (browserify) task
Warning: An error occurred while processing a template (Invalid or unexpected token). Use --force to continue.

Where is the error?

This is the GruntFile.js

module.exports = function(grunt) {
  grunt.initConfig({
    jsFiles: grunt.file.read('scripts/s.list'),
    env: {
        prod: {
            NODE_ENV: 'production'
        }
    },
    browserify: {
      dist1: {
        options: {
           transform: [
             ['babelify', {presets: ['es2015']}]
           ]
        },
        src: ['<%= jsFiles.toString().split("\n") %>'],
        dest: '/WebContent/js/libs/s.bundle.js'
      },
    },
    uglify: {
        my_target1: {
          files: {
            '/WebContent/js/libs/s.bundleuglified.js': ['/WebContent/js/libs/s.bundle.js']
          }
        },
      }    
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-browserify');
  grunt.loadNpmTasks('grunt-env');
  grunt.registerTask('default', ['release']);
  grunt.registerTask('release', ['env', 'browserify', 'uglify']);
};

Edit: I added a backslash to \n and the error has gone, but the babelify task gives me an empty file...

Edit2: I was able to read the file list using the following two lines at the beginning of the GruntFile.js

  const jsFiles = grunt.file.read('scripts/s.list');
  const jsFilesArray = jsFiles.toString().split("\n");

and then

src: jsFilesArray.slice(0, jsFilesArray.length-1),

because the last element was '' and it gave the error Warning: must provide pattern” as Beniamin H suggested.

Edit3: I found that the babelify task was reading the files in alphabetical order, so I had to first concat them, as explained here, and then babelify and uglify

1 Answers
Related