【问题标题】:Loading vendor javascript as modules将供应商 javascript 作为模块加载
【发布时间】:2015-11-05 02:56:30
【问题描述】:

我正在开发一个使用 Brunch 构建的应用程序。我想将一些供应商提供的 javascript 作为模块加载,这样我就可以在我的代码中 require 它们,而不是依赖全局变量。 有什么方法可以做到这一点,而无需将所有供应商代码复制到我的app 目录中?

我尝试创建一个vendorlib 目录,但早午餐似乎除了appvendor 之外的任何地方都没有。我还尝试创建一个vendor/modules 目录,但早午餐似乎没有包含在vendor 下找到的任何内容(即使我说服它将这些文件与在app 下找到的其他模块的文件结合起来。)

*我现在正在研究的“一些”是卓别林、骨干和下划线。如果我让这些工作,我稍后会移动更多。

【问题讨论】:

    标签: brunch


    【解决方案1】:

    您可以覆盖config.modules.wrapper 并使其包装,例如,vendor/modules 目录中的所有文件。或者您可以将更多由早午餐处理的目录添加到config.paths.watched

    【讨论】:

    • 我在尝试时无法让 config.paths.watched 工作。这是一个新功能吗?我已经安装了 brunch 1.7.0。
    • 好的。更新到 1.7.5,现在它可以工作了,但是它在注册的模块名称中包含了新目录的名称。这是它应该如何工作,还是我错过了配置中的某些内容?
    • 是的。默认只清理app,由config.modules.nameCleaner函数处理。
    • 感谢您的帮助。我在这里复制了我的配置,以便下一个人可以有一个工作示例。
    【解决方案2】:

    对于那些在家里跟随的人来说,这就是我的 config.coffee 最终的样子:

      paths:
        watched: ['app','vendor','test','vendorlib']
      files:
        javascripts:
          joinTo:
            'javascripts/app.js': /^app/
            'javascripts/vendor.js': /^vendor/
            'test/javascripts/test.js': /^test[\\/](?!vendor)/
            'test/javascripts/test-vendor.js': /^test[\\/](?=vendor)/
          order:
            # Files in `vendor` directories are compiled before other files
            # even if they aren't specified in order.before.
            before: [
              'vendor/scripts/console-polyfill.js',
            ]
            after: [
              'test/vendor/scripts/test-helper.js'
            ]
    
        stylesheets:
          joinTo:
            'stylesheets/app.css': /^(app|vendor)/
            'test/stylesheets/test.css': /^test/
          order:
            after: ['vendor/styles/helpers.css']
    
        templates:
          joinTo: 'javascripts/app.js'
    
      modules:
        nameCleaner: (path) ->
          path.replace(/^(app|vendorlib)\//, '')
    

    这让我可以使用来自支持作为模块加载的供应商的模块填充vendorlib 目录。我目前有卓别林、jQuery 和 Backbone。我不得不重命名它们以不包括版本号。

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2016-02-08
      • 2018-11-27
      • 2015-06-19
      • 2020-07-01
      相关资源
      最近更新 更多