質問

I have included a library written in Javascript, and also have some coffeescript code which depends on these libraries.

When I publish using GRUNT I want to create a single Javascript file which is made by combining all the JS and Coffee files together. Apparently neither grunt-contrib-uglifyjs nor Grunt-contrib-coffee supports such kind of behavior. What is the solution to this problem?

役に立ちましたか?

解決

You can use concat to do that.

For example, in your Gruntfile.js:

module.exports = function(grunt) {
  return grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    // EDIT: TO COMPILE YOUR COFFEESCRIPT
    coffee: {
      "default": {
        src: paths.coffee_src,
        dest: paths.js_dir,
        options: {
          preserve_dirs: true,
          base_path: paths.coffee_dir
        }
      }
    },
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['assets/javascripts/libs/underscore/underscore.js', 'assets/javascripts/libs/jquery/jquery.js', 'assets/javascripts/libs/json2/json2.js', 'assets/javascripts/libs/handlebars-wycats/dist/handlebars.js', 'assets/javascripts/libs/backbone/backbone.js', 'assets/javascripts/libs/marionette/lib/backbone.marionette.js', 'assets/javascripts/*.js', 'assets/javascripts/utilities/*.js', 'assets/javascripts/models/*.js', 'assets/javascripts/collections/*.js', 'assets/javascripts/modules/**/**/*.js'],
        dest: '<%= pkg.name %>.js'
      }
    },
    // ...
    // EDIT: TO MINIFY YOUR CONCATENATED JS
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          '<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },
    // ...

Here I'm concatenating all files in that order (starting with underscore.js), so, you'll have a single js file with all those files, in the specific order that you desire.

Check this documentation for more information.

他のヒント

Grunt is a nodejs file and execute like nodejs. So try this:

require('child_process').exec('coffee --compile --output lib/ src/',  function () {
    /*add callback or use execSync */
});

Just edit coffee command.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top