使用grunt编译coffeescript文件 [英] Compiling coffeescript files with grunt

查看:180
本文介绍了使用grunt编译coffeescript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个grunt任务,使用grunt将大量的.coffee文件编译为相应的.js文件和.map文件。我有grunt coffee插件,但有一些问题:


  1. 它将所有文件编译到一个公共目标文件夹,而不是保存.js

  2. 将不同目录中相同名称的.coffee文件合并到目标目录中的一个文件中。

请帮助解决这些问题:



Grunt插件:。

  module.exports =(grunt) - > 
grunt.initConfig
pkg:grunt.file.readJSON'package.json'
coffee:
coffee_to_js:
选项:
bare:true
sourceMap:true
expand:true
flatten:false
cwd:client
src:[** / *。coffee]
dest: 'client'
ext:.js

#Load Tasks
grunt.loadNpmTasks'grunt-contrib-coffee'
grunt.registerTask'compile' 'coffee']

这是不扁平的输出,我相信是你的后:

  $ grunt compile 
运行coffee:coffee_to_js任务
文件客户端/ js创建。
创建文件客户端/ main.js.map(源映射)。
创建文件客户端/ models / question.js。
创建文件客户端/ models / question.js.map(源映射)。
创建文件客户端/ views / question.js。
文件客户端/ views / question.js.map创建(源映射)。


I am trying to write a grunt task to compile numerous .coffee files to corresponding .js files and .map files using grunt. I have the grunt coffee plugin, but there are some problems:

  1. It compiles all files into one common destination folder instead of keeping the .js files in same folder as the .coffee file.
  2. It merges two .coffee files of the same name in different directories into one file in the destination directory.

Please help solving these:

Grunt plugin: https://www.npmjs.org/package/grunt-contrib-coffee

Gruntfile.coffee:

module.exports = (grunt) ->
  grunt.initConfig(
    pkg: grunt.file.readJSON 'package.json'
    coffee:
      coffee_to_js:
        options:
          bare: true
          sourceMap: true
        expand: true
        flatten: true
        cwd: "client"
        src: ["**/*.coffee"]
        dest: 'client'
        ext: ".js"
  )

  #Load Tasks
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.registerTask('compile', ['coffee']);

  null

Compiled Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    coffee: {
      coffee_to_js: {
        options: {
          bare: true,
          sourceMap: true
        },
        expand: true,
        flatten: true,
        cwd: "client",
        src: ["**/*.coffee"],
        dest: 'client',
        ext: ".js"
      }
    }
  });
  grunt.loadNpmTasks('grunt-contrib-coffee');
  grunt.registerTask('compile', ['coffee']);
  return null;
};

File structure before compile:

File structure after compile:

Compilation message:

解决方案

If you want to maintain the structure of where your JS is compiled, you should set the flatten flag to false. See Grunt Configuring tasks - Building the files object dynamically.

module.exports = (grunt) ->
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'
    coffee:
      coffee_to_js:
        options:
          bare: true
          sourceMap: true
        expand: true
        flatten: false
        cwd: "client"
        src: ["**/*.coffee"]
        dest: 'client'
        ext: ".js"

  #Load Tasks
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.registerTask 'compile', ['coffee']

This is the output when not flattened which I believe is what you're after:

$ grunt compile
Running "coffee:coffee_to_js" (coffee) task
File client/main.js created.
File client/main.js.map created (source map).
File client/models/question.js created.
File client/models/question.js.map created (source map).
File client/views/question.js created.
File client/views/question.js.map created (source map).

这篇关于使用grunt编译coffeescript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆