Sampo
Sampo

Reputation: 5048

How to load external script with optimized require.js?

We use require.js to manage library dependencies. During development it loads our code from individual JS files, while the production build is optimized into a single JS file.

I'm now trying to load external JS libraries dynamically. I'm trying to add new libraries with require.config and then use them:

require.config({paths:{ "d3":"https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min" }})
require(['d3'], function(d3) { console.log("Works!") })

It works as expected in the non-optimized build. However, in the optimized build it produces an error:

Uncaught Error: undefined missing d3

It seems that the optimized require somehow different from the non-optimized library. How can I get dynamic configuration and loading working in the optimized build?

Upvotes: 0

Views: 511

Answers (1)

Sampo
Sampo

Reputation: 5048

Turns out our build scripts replaced require.js with almond in the production build. It's meant as a require.js replacement for single-file builds which does not include external file loading support.

I wish the almond error messages were a bit more verbose, it took me hours to track that one down.

Upvotes: 0

Related Questions