我正在使用 BrunchJS 来处理咖啡脚本和资产编译。该项目使用了几个Brunch插件,例如需要“commonjs”包装器才能运行的brunch-handlebar。
从我的config.coffee中提取
modules:
# We cant avoid require js wrapping since brunch modules use commonjs
# Otherwise Marionnette JS offers its own modules loading strategy
# loading mechanism
wrapper: "commonjs"
definition: "commonjs"
在 Marionette 方面,我可以很好地加载一个简单的应用程序。
索引.html
<script type="text/javascript">
var app = require('application');
app.initialize()
</script>
应用程序.coffee
# 加载把手块助手需要'lib/view_helper'
class Application extends Backbone.Marionette.Application
initialize: =>
@addInitializer((options) =>
console.log "HELLO WORLD"
AppLayout = require 'views/app_layout'
@layout = new AppLayout()
@layout.render()
)
@start()
# module.exports is the object that's actually returned as the result of
# a require call.
module.exports = new Application()
从那里开始我如何使用 Marionette JS 模块?我在这里阅读了有关使用 AMD 模块的信息https://github.com/marionettejs/backbone.marionette/wiki/AMD-Modules-vs-Marionette 's-Modules 但我不能在我的木偶模块定义中使用define关键字,因为“定义”和“要求”没有公开。Brunch 只使用它来加载它的插件和我的应用程序源文件。
一个常见的 Marionette 模块如下所示:
MyApp = new Backbone.Marionette.Application();
MyApp.module("Foo", function(){
// module code goes here
});
MyApp.start();
在一个单独的文件moduleA.coffee我试图做:
MyApp = require 'application'
define ["MyApp", "Marionette"], (MyApp, Marionette) ->
MyModule = MyApp.module("MyModule")
MyModule.addInitializer ->
console.log "HELLO FROM MODULE"
MyModule
但是定义 没有定义。
我也尝试过:
MyApp = require 'application'
MyApp.module "ModuleA", (MyApp, ModuleA, Backbone, Marionette, $, _) ->
ModuleA.addInitializer ->
console.log "HELLO FROM MODULE"
但后来我需要在 application.coffee 中要求我所有的木偶模块(“moduleA”)并遇到一些循环依赖问题。
我正在考虑的解决方案之一是禁用BrunchJS commonjs 包装并从供应商文件夹加载把手,而不是作为早午餐插件。