Search code examples
javascriptbundlerrolluprollupjs

Rollup replace my main class name (SDK) for default() name


I am developing a library, which had been working well until I started doing the unit tests. Until a few days ago, I could instantiate my library in the following way:

this.belpay = new belpay.SDK('test', {
            username: 'username',
            password: 'password',
            key: 'abc-def',
          });

And in my chrome console:

enter image description here

Now I present the following error:

enter image description here

To eliminate this error I have had to do the following:

this.belpay = new belpay.default('test', {
            username: 'username',
            password: 'password',
            key: 'abc-def',
          });

I don't understand why now it shows me default. I've always had this setting in rollup.config.js

import nodePolyfills from 'rollup-plugin-node-polyfills';
import babel from 'rollup-plugin-babel';
import serve from 'rollup-plugin-serve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import json from 'rollup-plugin-json';

export default {
  input: 'src/index.js',
  context: 'window',
  output: [
    {
      file: 'dist/belpay.js',
      format: 'umd',
      name: 'belpay',
      exports: 'named',
      globals: ['axios'],
    },
    {
      file: 'dist/belpay.min.js',
      format: 'umd',
      name: 'belpay',
      plugins: [terser()],
      exports: 'named',
    },
  ],
  external: ['axios'],
  plugins: [
    babel({
      exclude: ['node_modules/**'],
      runtimeHelpers: true,
    }),
    nodePolyfills(),
    serve({
      host: 'localhost',
      port: 1234,
      contentBase: 'dist',
      historyApiFallback: true,
      allowCrossOrigin: true,
    }),
    commonjs({
      include: 'node_modules/axios/**',
    }),
    livereload({
      watch: 'dist',
      verbose: true,
    }),
    json(),
  ],
};

[EDIT]

Contents of my entryfile (src/index.js):

export default class SDK {
  #myvar= '';
  #myobject = {};

  method1() {}
  method2() {}
}

and this way I am importing the library in dist/index.html

<!-- html content -->
...
<!-- at the end of the body tag. -->
<script type="module" src="./belpay.js"></script>
<script type="text/javascript">
  this.belpay = new belpay.SDK();
</script>

It is strange because on Friday (last work day of the week for us), I left it working well, but on Monday I could no longer do the tests. I did not make any changes.

Any suggestion to improve this file will be welcome.


Solution

  • I found the problem. I was doing:

    export default class SDK {}
    

    and had to do:

    export class SDK {}
    

    I didn't have to set 'default'.