krasimir / webpack-library-starter

Webpack based boilerplate for producing libraries (Input: ES6, Output: universal library)
MIT License
1.36k stars 290 forks source link

external dependencies #9

Closed riskers closed 7 years ago

riskers commented 7 years ago

Now, I have a library xx depending jQuery:

import $ from 'jquery'

...

I find #3 , so I write this externals:

externals: {
    'jquery': 'jQuery'
}

It's great! The bundle file is small!

But, when I use my library:

var xx = require('xx')

then use webpack, the library put xx and jQuery together. I don't know how to separate them?

krasimir commented 7 years ago

Sorry for the late reply. Is there require('jquery') inside your library. If yes, then this result is correct.

adamkleingit commented 7 years ago

So the library will be bundled together with all its dependencies? For example if I have import 'angular' then angular will be bundled with my library? Is there a way to bypass this?

Thanks

krasimir commented 7 years ago

@adamkleingit the proper way for handling this case is webpack's externals.

@riskers Now, when I'm reading your question again I think I understood what's going on. Correct me if I'm wrong but you build your library using webpack and then you have a build step (again using webpack) that uses your library. If that's the case then you'll have to use externals option one more time in your build process.

gen4sp commented 7 years ago

But how to include dependencies into your library? If I use something like import _ from "lodash" it generate an error while building Module not found: Error: Can't resolve 'lodash' in... Once I include lodash in externals option it builds perfectly, but library needs lodash as dependency. Thanks!

gen4sp commented 7 years ago

Oh, solution found modules: [path.resolve('./src'), path.join(__dirname, 'node_modules')],

freeman29 commented 7 years ago

@gen4sp I've tried your solution above on including dependencies but it's not working correctly because it's now looking for all its dependencies.

I'm trying to include request library.

freeman29 commented 7 years ago

I resolved my issue by adding the following into the config object:

target: 'node'

a good read from jlongster blog

krasimir commented 7 years ago

@riskers do we have to keep the issue open?