Search code examples
javascriptrolluprollupjsumdaws-chime-sdk

Why is rollup not generating a global variable despite output name being specified?


I am trying to use singlejs which is part of the AWS Chime SDK for JavaScript. The singlejs sample generates amazon-chime-sdk.min.js and you are meant to be able to access the SDK via the global variable ChimeSDK. However in the latest version the generated file does not include this global variable. Here is the rollup.config.js that I'm using:

import commonjs from '@rollup/plugin-commonjs';
import resolve, { nodeResolve } from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';

export default {
  input: 'src/index.js',
  output: [
    {
      file: 'build/amazon-chime-sdk.min.js',
      format: 'umd',
      name: 'ChimeSDK',
      sourcemap: true,
    },
  ],
  plugins: [
      [nodeResolve({
        browser: true,
        mainFields: ['module','browser'],
      })],
      json(),
      commonjs()
      //,
      //[terser()]
    ],
  onwarn: (warning, next) => {
    if (warning.code === 'CIRCULAR_DEPENDENCY') {
      // TODO: Fix https://github.com/aws/amazon-chime-sdk-js/issues/107
      return;
    } else if (warning.code === 'EVAL') {
      return;
    } else if (warning.code === 'THIS_IS_UNDEFINED') {
      // https://stackoverflow.com/questions/43556940/rollup-js-and-this-keyword-is-equivalent-to-undefined
      return;
    }
    next(warning);
  },
};

The source index.js is simply:

export * from 'amazon-chime-sdk-js';

When rollup --config rollup.config.js runs it builds successfully. However the generated file begins like this:

(function (factory) {
    typeof define === 'function' && define.amd ? define(factory) :
    factory();
})((function () { 'use strict';

    var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
//etc

Towards the end of the file it has:

var hasRequiredBuild;

    function requireBuild () {
        if (hasRequiredBuild) return build$7;
        hasRequiredBuild = 1;
        (function (exports) {
            Object.defineProperty(exports, "__esModule", { value: true });
            exports.DefaultEventController = exports.DefaultDevicePixelRatioMonitor = exports.DefaultDeviceController = exports.DefaultContentShareController = exports.DefaultBrowserBehavior = exports.DefaultAudioVideoFacade = 
//etc

There is no sign of the ChimeSDK global variable and my code (which worked with an earlier build of amazon-chime-sdk.min.js, based on version 2.x of the SDK and generated in the same way but with an earlier and simpler configuration) no longer runs, complaining that ChimeSDK is not defined.

It looks like something which could be fixed if I had a better understanding of Rollup and umd (also tried iife format but similar result, no global variable).

Have tried the exact configuration in the repository with same result; I've bumped the versions and disabled terser temporarily in the config above.


Solution

  • Update the src/index.js file with the following code and then rebuild the code with npm run bundle. Rollup recommends a default export if we have only single export.

    export * as default from 'amazon-chime-sdk-js';