使用 grunt 编译 coffeescript 文件 [英] Compiling coffeescript files with grunt
问题描述
我正在尝试编写一个 grunt 任务来使用 grunt 将大量 .coffee 文件编译为相应的 .js 文件和 .map 文件.我有咕噜咖啡插件,但有一些问题:
- 它将所有文件编译到一个公共目标文件夹中,而不是将 .js 文件与 .coffee 文件保存在同一文件夹中.
- 将不同目录下的两个同名.coffee文件合并为目标目录下的一个文件.
请帮助解决这些问题:
Grunt 插件:.
module.exports = (grunt) ->grunt.initConfigpkg: grunt.file.readJSON 'package.json'咖啡:coffee_to_js:选项:裸露:真实源地图:真展开:真展平:假cwd:客户"src: ["**/*.coffee"]目的地:'客户'分机:.js"#加载任务grunt.loadNpmTasks 'grunt-contrib-coffee'grunt.registerTask '编译', ['咖啡']
这是未展平时的输出,我相信这是您所追求的:
$ grunt 编译运行coffee:coffee_to_js"(咖啡)任务文件 client/main.js 创建.文件 client/main.js.map 创建(源地图).文件 client/models/question.js 创建.文件 client/models/question.js.map 创建(源地图).文件 client/views/question.js 创建.文件 client/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屋!