Brunch: how to wrap vendor code in AMD modules?

2019-08-14 19:46发布

I want all vendor code to be wrapped in AMD modules. I have defined this section in my config.coffee:

modules:
    wrapper: 'amd'
    definition: 'amd'

But it seems like brunch uses AMD optimizer only for 'app' folder files. All vendor js files where concatinated without any r.js-like preprocessing(no module names added in "define(...)" statements). As a result, Almond(it's almost the same as RequireJS) complains about anonymous module definition during vendor file executing.

Here is my full config:

exports.config =

  paths:
    public: 'public'

  files:
    javascripts:
      defaultExtension: 'js'

      joinTo:
        'js/app.js': /^app/
        'js/vendor.js': /^vendor[\\/](?!mocha|chai|sinon|sinon-chai)/
        'js/tests.js': /^test/
        'js/tests-vendor.js': /^vendor[\\/](?=mocha|chai|sinon|sinon-chai)/

      order:
        before: [
          'bower_components/almond/almond.js',
          'bower_components/jquery/jquery.js',
          'bower_components/lodash/dist/lodash.underscore.js'
        ]

    stylesheets:
      defaltExtension: 'less'

      joinTo:
        'css/styles.css': /^(vendor[\\/](?!mocha|chai|sinon|sinon-chai)|app)/
        'css/tests-vendor.css': /^(vendor[\\/](?=mocha|chai|sinon|sinon-chai))/

    templates:
      defaultExtension: 'hbs'
      joinTo: 'js/app.js'

  modules:
    wrapper: 'amd'
    definition: 'amd'

1条回答
时光不老,我们不散
2楼-- · 2019-08-14 20:21

You can change conventions.vendor in your config to something that won't match your vendor files in order to enable module wrapping.

https://github.com/brunch/brunch/blob/master/docs/config.md#conventions

查看更多
登录 后发表回答