node.js - GruntJS + contrib-咖啡 : compiling but keeping the folder structure

标签 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

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

但是当我运行 GruntJS 时,它将所有文件放在同一个文件夹(ppalmeida)中。所以 Controller 、核心、模型文件被扔在同一个文件夹中,这是我不想要的。

这是我的 glob_to_multiple 配置:

coffee:
        glob_to_multiple:
            expand: true,
            flatten: true,
            cwd: 'src_client/',
            src: ['**/*.coffee'],
            dest: 'public/ppalmeida/',
            ext: '.js'

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

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

所以。有没有办法用 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']

最佳答案

哦,现在我明白了。答案就在我面前:只需将“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'

希望它对某人有帮助。 谢谢。

关于node.js - GruntJS + contrib-咖啡 : compiling but keeping the folder structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254458/

相关文章:

javascript - AWS Lambda 函数多次处理相同的 dynamodb 流。我错过了什么?

node.js - 锁定全局 npm 包

node.js - 使用node.js和lazy时了解EOF

javascript - 如何查看一个类是否已经被实例化

javascript - 将 async.eachLimit 转换为 Promise

node.js - Gruntjs - 以特定顺序运行多个阻塞任务(Mongo 和 Node.js)

javascript - Grunt usemin 转换 Assets 路径

ios - 解析迁移噩梦语句前缺少分号

node.js - 无法将 Sails.js 应用程序投入生产

node.js - Azure 上的 Strapi 无法运行