DimiMikadze / create-react-library

React NPM library starter kit based on Facebook's create react app
MIT License
602 stars 60 forks source link

Libraries built using this library get way too big due to bundling. #15

Closed markusenglund closed 6 years ago

markusenglund commented 6 years ago

The contents of the src/lib-folder + dependencies are bundled and minified with webpack into the build folder. The build folder is then served as the final package. This is bad for the following reasons:

What this library should be doing: Use babel to transpile the es6+jsx files in the src/lib-folder to plain old es5 and that's it. It shouldn't do any bundling or minifying (except for maybe a UMD build, I have no idea how that works). The react devs who will use the final package already bundle and minify their app so it doesn't have to be done by this library.

DimiMikadze commented 6 years ago

Hi @yogaboll, thanks for reporting.

i see your point, but i don't think it happens because we minify/bundle the code for production.

create-react-app is loading polyfills, Promise whatwg-fetch object-assign and raf in to production bundle, which i totally agree we shouldn't load, so i'll remove them now.

regarding dependencies like prop-types i guess while working on library, we should add them as a peerDependencies so if it's already provided by application, we won't install it.

what you think?

DimiMikadze commented 6 years ago

https://github.com/UdiliaInc/create-react-library/commit/cd4422078766e1cb32378f117ae01ea423091c7b

markusenglund commented 6 years ago

Removing the polyfills seems to have fixed my first complaint. I agree that minifying and bundling the code in src/lib isn’t an issue. However, bundling together the dependencies definitely is.

Prop-types is a bit of a special case as it could reasonably be either a dependency or a peerDependency. However, Dan Abramov recommends having it as a regular dependency.

For other dependencies, it’s pretty clear that you should not import them as peerDependencies. Let’s say my library uses the package react-draggable as a peerDependency. People who use my library would then have to install that separately since peerDependencies are not automatically installed. Now let’s say my library uses react-draggable version 2.0.0, which is an old version. Then the user would have to specifically install version 2, which would confuse some. They would have to do that for every dependency that my library uses, which would be non-standard and a bit confusing.

EdgardosReis commented 6 years ago

what about a new script called "export" for example, that creates a folder with the files package.json, README.md, LICENCE, index.js and all the other lib contents and transpile to ES5 the js files. It can minify but not important in this context as stated by @yogaboll.

markusenglund commented 6 years ago

That would work. So do you mean duplicating the readme, license etc? I'm guessing that would not be necessary.

EdgardosReis commented 6 years ago

yeah, I didn't explain my use case and is probably a bit offtopic. I can't publish my lib to npm and don't have a private package repository so what I would want is, in the app (a non ejected CRA), add a dependency in package.json to my library like: "dependencies": { "my-lib": "file:./src/libs/my-lib", } And then just execute npm install in my app, that's why I would need the package.json of the library and no dependencies bundled.

markusenglund commented 6 years ago

Ok, I don't know how that would work exactly. The npm docs have some info on different ways to depend on packages: https://docs.npmjs.com/files/package.json#urls-as-dependencies

DimiMikadze commented 6 years ago

Guys sorry for late response.

@EdgardosReis i think, to modify or add new functionality for use case like yours will add extra layer of complexity/uncertainty to this starter kit, because in the most cases we want our library to be published on public or private NPM.

If there's demand for it and someone will modify create-react-library to support this kind of functionality, we can create separate branch for it.