Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack CommonJS backbone and underscore

I'd like to use Backbone with webpack in the CommonJS style, but I need to understand how to:

  • Tell webpack that Backbone depends on underscore
  • Prevent them from automatically defaulting to AMD

It seems like imports-loader might be the answer. Do I want something like this in my configuration?

module: {
    loaders: [
        {
            test: require.resolve('_'),
            loader: 'imports?_=underscore,define=>false'
        },
        {
            test: require.resolve('backbone'),
            loader: 'imports?define=>false'
        }
    ]
},

Also, will this make it so I don't have to do var _ = require('underscore'); before I do var Backbone = require('backbone'); everywhere?

My question might be deemed a duplicate of this, but I hope to get a little more clarification than provided in the accepted answer there.

like image 474
Rob Johansen Avatar asked Apr 21 '26 00:04

Rob Johansen


1 Answers

If you've installed backbone via npm, it should be required via CommonJS. When installed, NPM modules also automatically retrieve and install their dependencies in their own node_modules folder, which means you don't have to worry about providing underscore to backbone.

like image 64
Alexandre Kirszenberg Avatar answered Apr 22 '26 14:04

Alexandre Kirszenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!