使用grunt编译coffeescript文件 [英] Compiling coffeescript files with grunt
问题描述
我试图编写一个grunt任务,使用grunt将大量的.coffee文件编译为相应的.js文件和.map文件。我有grunt coffee插件,但有一些问题:
- 它将所有文件编译到一个公共目标文件夹,而不是保存.js
- 将不同目录中相同名称的.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:
- It compiles all files into one common destination folder instead of keeping the .js files in same folder as the .coffee file.
- 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屋!