使用grunt编译coffeescript文件

Ete*_*ous 2 coffeescript gruntjs grunt-contrib-coffee

我正在尝试编写一个grunt任务,使用grunt将许多.coffee文件编译为相应的.js文件和.map文件.我有咕噜咕噜的咖啡插件,但有一些问题:

  1. 它将所有文件编译到一个公共目标文件夹中,而不是将.js文件保存在与.coffee文件相同的文件夹中.
  2. 它将不同目录中的两个同名的.coffee文件合并到目标目录中的一个文件中.

请帮助解决这些问题:

Grunt插件: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
Run Code Online (Sandbox Code Playgroud)

编译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;
};
Run Code Online (Sandbox Code Playgroud)

编译前的文件结构:

在此输入图像描述

编译后的文件结构:

在此输入图像描述

编译信息:

在此输入图像描述

max*_*tty 6

如果要维护编译JS的结构,应将flatten标志设置为false.请参阅Grunt配置任务 - 动态构建文件对象.

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']
Run Code Online (Sandbox Code Playgroud)

这是不平整的输出,我相信你所追求的是:

$ 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).
Run Code Online (Sandbox Code Playgroud)