gonzofish / angular-librarian

An Angular 2+ scaffolding setup for creating libraries
https://www.npmjs.com/package/angular-librarian
MIT License
91 stars 9 forks source link

[Migration?] Lodash methods are not getting exported from the library #70

Closed tommueller closed 7 years ago

tommueller commented 7 years ago

I use a couple of lodash functions in my library, which is not working in the consuming angular-cli project. I get undefined is not a function in all the places where lodash-functions are used.

During the build of the angular-librarian project I get a lot of these warnings:

'isEqual' is not exported by 'node_modules/lodash/lodash.js' 'clone' is not exported by 'node_modules/lodash/lodash.js' 'isNumber' is not exported by 'node_modules/lodash/lodash.js'

lodash is declared as dependency in the package.json and serving the project works fine. lodash is also installed in the consuming project.

This was working before, so I think it might be an migration issue, but I am not sure ;)

tommueller commented 7 years ago

I was able to fix this by adding lodash as external and global to the rollup.config.js. But shouldn't rollup check by itself that these functions are used and bundle them into the package?

gonzofish commented 7 years ago

Rollup doesn't do that, you have to specify them yourself, so that it can understand them. That's why those sections are part of the custom configuration now.

tommueller commented 7 years ago

I thought rollup does treeshaking? At least it says so here: https://rollupjs.org/

gonzofish commented 7 years ago

@tommueller sorry I'm so tardy on responding to this--Rollup does do treeshaking, but you have to specify your external libraries. The whole reason we're using Rollup now is so that 3rd party dependencies (such as Lodash) aren't bundled into your library's code. When you specify external & globals you're letting Rollup know that it can shake this code out of the bundle and, where it's references, use the alias specified in globals.

So if you did:

import { map } from 'lodash';

And specified in your custom Rollup config:

{
   external: [ 'lodash'],
   globals: { 'lodash': '_' }
}

Rollup would know that where every an import ... from 'lodash'; happens that it can use a variable of _ in its place. Hope this makes sense and/or you got past your problem.

tommueller commented 7 years ago

Thanks Matt, I already got this working. Thanks for the clarification though!