GruntJS + contrib-coffee:编译但保留文件夹结构

ppa*_*ida 1 node.js coffeescript gruntjs grunt-contrib-watch

我正在使用GruntJSgrunt-contrib-coffee.它很棒!在Watch插件的帮助下,它甚至更好.但是我怀疑:

我有两个文件夹,一个是我的coffeescript文件,另一个是我的(编译过的)js文件.看起来像这样

root_folder
|--- public
     |--- libs
     |--- ppalmeida <= here are my .js compiled files from src_client coffeescripts
          |--- controllers
          |--- core
               |--- Main.js
          |--- models

|--- src_client <= here are my .coffee files
        |--- controllers
        |--- core
             |--- Main.coffee
        |--- models
Run Code Online (Sandbox Code Playgroud)

当我使用coffee --output --compile命令构建时,文件夹结构被保留:Coffeescript cli编译"core"文件夹并在Main.js中.

但是当我运行GruntJS时,它将所有文件放在同一个文件夹(ppalmeida)中.所以控制器,核心,模型文件被扔在同一个文件夹中,我不想要.

这是我的glob_to_multiple配置:

coffee:
        glob_to_multiple:
            expand: true,
            flatten: true,
            cwd: 'src_client/',
            src: ['**/*.coffee'],
            dest: 'public/ppalmeida/',
            ext: '.js'
Run Code Online (Sandbox Code Playgroud)

所以,最后我得到了这样的东西:

root_folder
|--- public
     |--- libs
     |--- ppalmeida <= here are my .js compiled files from src_client coffeescripts
          |--- controllers (empty)
          |--- core (empty)
          |--- models (empty)
          |--- Main.js
          |--- ControllerA.js
          |--- ControllerB.js
          |--- ModelA.js
          |--- all_other_js_files_here.js


|--- src_client <= here are my .coffee files
        |--- controllers
             |--- ControllerA.coffee
             |--- ControllerB.coffee
        |--- core
             |--- Main.coffee
        |--- models
             |--- ModelA.coffee
             |--- ModelA.coffee
Run Code Online (Sandbox Code Playgroud)

所以.有没有办法用contrib-coffee编译并避免所有js文件放在同一个文件夹中?

谢谢你们.

PS:我不知道它是否有帮助,但这是我完整的Gruntfile.coffee:

module.exports = (grunt) ->

# Grunt config object
config =
    pkg: grunt.file.readJSON("package.json")

    # Grunt must watch the src_client folder for modifications in its files and run coffee task when some file is changed/created
    watch:
        coffee:
            files: 'src_client/**/*.coffee',
            tasks: ['coffee']

    # Create the 'coffee' task, to compile all *.coffee files to *.js
    coffee:
        glob_to_multiple:
            expand: true,
            flatten: true,
            cwd: 'src_client/',
            src: ['**/*.coffee'],
            dest: 'public/ppalmeida/',
            ext: '.js'

# Init grunt with config object
grunt.initConfig config

# Load grunt plugins
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-contrib-watch'

# Register coffee as a default task:
grunt.registerTask 'default', ['coffee']
Run Code Online (Sandbox Code Playgroud)

ppa*_*ida 9

哦,现在我明白了.答案就在我面前:只需将"flatten"属性更改为"false",这样就会保留文件的路径,因此coffeescript将使用它进行编译并保留文件夹结构:

# Create the 'coffee' task, to compile all *.coffee files to *.js
coffee:
    glob_to_multiple:
        expand: true,
        flatten: false, #here, it must be false to keep folder structure when compiling
        cwd: 'src_client/',
        src: ['**/*.coffee'],
        dest: 'public/ppalmeida/',
        ext: '.js'
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人.谢谢.