Search code examples
iiferollupjs

Rollup external paths with iife


I found next code fragment on the Rollup api page. If I would be using iife instead of amd. How would Rollup define this in the bundle? Or would it expect a preceding <script> tag containing that external code? If the latter would be true: Is there a way to produce a bundle with JS code which dynamically loads JS files through absolute URLs?

enter image description here


Solution

  • Try it. You'll see it generates code like this:

    (function (d3) {
    'use strict';
    
    d3.selectAll('p').style('color', 'purple');
    
    }(d3));
    

    In other words yes, it expects there to be a <script> tag on the page that defines d3.

    Is there a way to produce a bundle with JS code which dynamically loads JS files through absolute URLs?

    That's exactly what the amd output is. You just need to have an AMD module loader such as require.js or curl.js on the page.