2

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

cdarwin
  • 4,141
  • 9
  • 42
  • 66

1 Answers1

1

You don't need to use any '<%= %>'. The file is read synchronously into jsFiles property and it can be used immediately. You may want to specify encoding for grunt.file.read to get a string: https://gruntjs.com/api/grunt.file#grunt.file.read

Beniamin H
  • 2,048
  • 1
  • 11
  • 17
  • So, what should I write for src in your opinion? Only src = 'jsFiles.toString().split("\n")' ? – cdarwin Jan 12 '19 at 19:20
  • I cannot check it now but something like this should work: `src: this.jsFiles.toString().split("\n"),` – Beniamin H Jan 12 '19 at 19:24
  • this gives `>> TypeError: Cannot read property 'toString' of undefined ` – cdarwin Jan 12 '19 at 19:26
  • Ahh - yes, you may read a file to a variable, not into config object. `const jsFiles = grunt.file.read('scripts/s.list');` and then: `src: jsFiles.toString().split("\n")` – Beniamin H Jan 12 '19 at 19:30
  • You can do it the first line of the function. – Beniamin H Jan 12 '19 at 19:31
  • This way it works if I put two backslashes before n, but unfortunately I get an empty bundle - with a single backslash I get this error : `Warning: must provide pattern` – cdarwin Jan 12 '19 at 19:38
  • Probably you need to specify `dist1` for your browserify task: `grunt.registerTask('release', ['env', 'browserify:dist1', 'uglify']);` – Beniamin H Jan 12 '19 at 19:47
  • Or omit wrapping browserify options into `dist1`: ```browserify: { options: {...}, src: ... },``` – Beniamin H Jan 12 '19 at 19:48
  • About _"Warning: must provide pattern"_: https://stackoverflow.com/questions/26233743/grunt-watch-repeatedly-showing-warning-must-provide-pattern – Beniamin H Jan 12 '19 at 19:51
  • The file was ok, I was able to read the file list as I explain in Edit2. Thank you for the link to the must provide pattern error. Unfortunately it seems the file list is being read in a different order that the one I specify. – cdarwin Jan 12 '19 at 21:34