Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Named Exports in Rollup Not Working

I am using Rollup for the first time (following the example at angular.io) and I'm getting this error:

'AuthHttp' is not exported by 'node_modules/angular2-jwt/angular2-jwt.js'

from this line in app.module.js:

13: import { AuthHttp, AuthConfig } from 'angular2-jwt/angular2-jwt';

The docs say you can correct this by specifying a custom named export in the rollup-config.js file like this:

commonjs({
  namedExports: {
    // left-hand side can be an absolute path, a path
    // relative to the current directory, or the name
    // of a module in node_modules
    'node_modules/my-lib/index.js': [ 'named' ]
  }
})

here is the relevant section of my rollup-config.js file:

  plugins: [
      nodeResolve({jsnext: true, module: true}),
      commonjs({
        include: 'node_modules/rxjs/**',
         namedExports: {
          'node_modules/angular2-jwt/angular2-jwt.js': [ 'AuthHttp' ]
        }
      }),

However this does not have any effect and the error remains. Any suggestions on how to correct this?

like image 338
ckapilla Avatar asked Jul 31 '17 02:07

ckapilla


People also ask

Is rollup deprecated?

This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.

What is Rollupjs?

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the new standardized format for code modules included in the ES6 revision of JavaScript, instead of previous idiosyncratic solutions such as CommonJS and AMD.

What are rollup Globals?

Rollup bundles multiple files that use import and export statements into a single script that no longer contains any import statements, but rather either contains all the JS it needs to function (more-or-less copied from the other files) or "loads" the JS from global variables. All reactions.


1 Answers

Try this and let me know how you get on:

rollup-config.js

commonjs({ include: ['node_modules/rxjs/**', 
                     'node_modules/angular2-jwt/angular2-jwt.js'],
 ..
         }) 

Did you do npm i -D rollup-plugin-node-resolve yet too?

jsnext is shown in the rollup-plugin-node-resolve documentation here.

There is a cryptic comment about removing it in next release too in the issues.

The rollup wiki docs however seem odd with respect to jsnext too. They just say it's superceeded by pkg.module which alone doesn't really clarify things for me. So maybe remove flag or switch to false?

There is a rollup starter-project config file. It references pkg.module in the targets array.

There is also a rollup-starter-lib config example too.

And here is the rollup guide

Update:

Named-exports seems to be part of rollup-plugin-commonjs npm i -D rollup-plugin-commonjs

Typically, you would use this plugin alongside rollup-plugin-node-resolve, so that you could bundle your CommonJS dependencies in node_modules.

`// explicitly specify unresolvable named exports
 // (see below for more details)
 namedExports: { './module.js': ['foo', 'bar' ] },  // Default: undefined`

Did you setup your tsconfig-aot.json properly too, per here?

like image 85
JGFMK Avatar answered Oct 18 '22 20:10

JGFMK