我在工作中开始一个项目,并想知道要使用的最佳构建工具是什么。

整件事是用coffeescript编写的,使用AngularJS为客户端和服务器的NodeJ。

应用程序有几个组件:

  • ipad app
  • iPhone应用程序(iPad的不同功能)
  • 应用程序的CMS
  • 一个nodejs服务器

在所有这些之间有很多共享代码,再次写在CoffeeScript中。

我想要一个构建工具,我可以列出哪个应用程序使用哪个代码(其中大部分共享),它将将每个应用程序的javascript文件构建到一个单独的文件夹中。

例如,我会设置一个名为'/ compiled / ipad /'的文件夹,它具有index.html,以及js,css,img等的文件夹。我会列出我想要抛入/编译/编译/编译的编译咖啡文件/ ipad / js(来自/src/shared/*.coffee中的一些,其中一些来自/src/ipad/*.coffee等)以及我想要抛出/编译/ ipad / css的文件。我希望它能够轻松地连接文件我想要的方式。

还将编译我的测试,从/ src / test / ipad进入/编译/ test / ipad / *。js。

所有客户端单元测试都使用 testacular ,我不确定我' LL尚未写入服务器端单元测试。

什么构建工具/配置是这里最好的方法? makefile?像咕噜咕噜的东西?我对整个建造场景诚实的新态度。

编辑:决定使用browserify。您可以找到我的解决方案,使其在这里与Angular合作: https://组。 Google.com/Forum/# !!topic/angular/ytovaikocs

有帮助吗?

解决方案

我会将所有共享代码放入Node.js模块,并创建一个看起来如下所示的项目:

Project
|~apps/
| |~cms/
| | `-app.js
| |~ipad/
| | `-app.js
| |~iphone/
| | `-app.js
| `~node/
|   `-app.js
|~libs/
| |-module.js
| `-module2.js
|~specs/
| |~cms/
| | `-app.js
| |~ipad/
| | `-app.js
| |~iphone/
| | `-app.js
| `~node/
|   `-app.js
| `~libs/
|   |-module.js
|   `-module2.js
`-Makefile
.

然后我会使用像浏览器(有其他人)这样的东西来制作所需的客户端应用程序。这样,而不是拥有一个构建文件,在那里您可以说出您需要的内容实际上具有Real Apps导入模块。

其他提示

p >>我认为,在JavaScript或CoffeeScript中写入服务器侧代码的驱动器也将扩展到您的构建工具链:因此也坚持使用JavaScript / Coffeescript。这将允许您从构建工具中轻松自动执行您的服务器/客户端任务 - 我怀疑它是有意义的可能性,如make(您只需在Node.js命令调用周围编写包装器)。建议,由结构性 - ness命令:

  • node.js :只需滚动您的构建脚本在JavaScript中,并使用节点调用它们。我想,类似于shell脚本。我不推荐这条路线。
  • jake cake :我来自Java世界,所以这些有点让我想起蚂蚁并不奇怪。我更喜欢Coffeescript,因此喜欢蛋糕。
  • grunt :我之前没有听说过这个,所以我不能提供很多建议。它让我想起了Maven,当然......我可以说......构建工具往往强制强制性的结构越多。它的一些折扣。只要你这样做是“建立工具”的方式,你可以节省大约吨。但如果您有应用程序特定问题,则可以是一个皇家痛苦来解决。
当然,您可以使用一些其他语言熟悉的其他构建工具:Rake,Maven,Ant,Gradle等。

我根据需要使用节点模块在CAKEFILE中完成了这一切。

设置具有每个文件路径的阵列的一些全局变量,将这些文件连接到您指定的编译目录中的文件中,然后将该文件编译为JS。

对于风格,同样的串联没有编译,显然。

fs = require 'fs'
path = require 'path'
{spawn, exec} = require 'child_process'
parser = require('uglify-js').parser
uglify = require('uglify-js').uglify
cleanCss = require 'clean-css'

coffees = 
 [
  "/src/shared/file1.coffee"
  "/src/shared/file2.coffee"
  "/src/ipad/file1.coffee"
 ]

tests = 
  [
   "/src/ipad/tests.coffee"
  ]

styles = 
 [
  "/src/ipad/styles1.css"
  "/src/shared/styles2.css"
 ]

concatenate = (destinationFile, files, type) ->
  newContents = new Array
  remaining = files.length
  for file, index in files then do (file, index) ->
      fs.readFile file, 'utf8', (err, fileContents) ->
          throw err if err
          newContents[index] = fileContents
          if --remaining is 0
              fs.writeFile destinationFile, newContents.join '\n\n', 'utf8', (err) ->
                throw err if err
              if type is 'styles'
                 minifyCss fileName
              else
                 compileCoffee fileName


 compileCoffee = (file) ->
    exec "coffee -c #{file}", (err) ->
       throw err if err
       # delete coffee file leaving only js
       fs.unlink 'path/specifying/compiled_coffee', (err) -> 
          throw err if err
          minifyJs file

 minifyJs = (file) ->
  fs.readFile f, 'utf8', (err, contents) ->
      ast = parser.parse contents
      ast = uglify.ast_mangle ast 
      ast = uglify.ast_squeeze ast
      minified = uglify.gen_code ast

      writeMinified file, minified

writeMinified = (file, contents) ->
   fs.writeFile file, contents, 'utf8', (err) -> throw err if err  


minifyCss = (file) ->
    fs.readFile file, 'utf8', (err, contents) ->
    throw err if err
    minimized = cleanCss.process contents
    clean = minimized.replace 'app/assets', ''

    fs.writeFile file, clean, 'utf8', (err) ->
        throw err if err


task 'compile_coffees', 'concat, compile, and minify coffees', ->
  concatenate '/compiled/ipad/code.coffee', coffees, 'coffee'

task 'concat_styles', 'concat and minify styles', ->
  concatenate '/compiled/ipad/css/styles.css', styles, 'styles'

task 'compile_tests', 'concat, compile, and minify test', ->
  concatenate '/compiled/ipad/tests.coffee', tests, 'tests'
.

现在,这是我认为你要求的。

肯定可以更漂亮,特别是具有编写缩小内容的单独功能,但它有效。

对样式不完美,因为我在使用sass并且在它达到缩小功能之前有其他功能,但我认为你得到了这个想法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top