r/javascript • u/tyler-mcginnis ⚛️⚛︎ • Jul 23 '20
Webpack: A Gentle Introduction
https://ui.dev/webpack7
u/franksvalli Jul 23 '20 edited Jul 23 '20
Nice fairly high-level overview (EDIT: in the first part of the article). I'm bookmarking for when I need to explain Webpack to non-tech folks!
I think the section about setting the env flag at the end may be outdated?
E.g. instead of setting NODE_ENV directly:
NODE_ENV='production' webpackNow it's recommended to pass in a flag:
webpack --mode=production3
u/tyler-mcginnis ⚛️⚛︎ Jul 23 '20
If you keep scrolling down it talks about how the
modeproperty replaces the need for setting NODE_ENV.4
Jul 23 '20
Why not use the --mode flag in the examples though? It's a platform agnostic way of setting this up in your package.json vs. the windows/not windows environment variables examples.
If you use the --mode flag "npm run build", for example, is going to work anywhere NPM is supported.
Otherwise I agree with the others, this is a nice straight forward jump into webpack.
1
u/franksvalli Jul 23 '20 edited Jul 23 '20
Thank you for the reply. I read it more carefully and see what you're doing now, but it is somewhat confusing.
If you have a single config file, I'd recommend keeping the mode flag out of
webpack.config.jsand just passing it via the mode flag CLI. It will be much more grokkable this way:"scripts": { "build": "webpack --mode production", "start": "webpack --mode development" }I think writing out the mode as a property within the config file is aimed at folks wanting to maintain a separate config file for dev, e.g.
webpack.config.dev.js webpack.config.prod.js16
u/Oalei Jul 23 '20
Non tech people?
They would not understand anything in that article.5
u/franksvalli Jul 23 '20 edited Jul 23 '20
Specifically the first parts about why Webpack exists and what problem it's solving, which is fairly high level. I agree that what follows will be less comprehensible. Questions non-tech people might ask: "Why did you spend so long this sprint configuring Webpack? What is it, and why do we even need it?"
If you know a better way to explain Webpack, please post a link.
1
2
u/Quinez Jul 24 '20
Really happy to open the link and see it's a Tyler McGinnis article. I find his stuff so clarifying. And hey, he redid his site! Looks snappy! (It needlessly scrolls horizontally a bit on my mobile though... not sure why.)
I've never dabbled with webpack, but reading this article makes me think I might try foregoing create-react-app on my next toy project.
1
u/tyler-mcginnis ⚛️⚛︎ Jul 24 '20
Thanks for the kind words! Also thanks for the heads up on the hori. scroll. I'll get that fixed. ❤️
84
u/2Punx2Furious Jul 23 '20
Is there a violent introduction?
63
u/yramagicman Jul 23 '20
The web pack documentation, while being incredibly thorough, is not what I would call beginner friendly. Most of the problems I've run into aren't easily searchable within the official docs.
15
u/peduxe |o.o| Jul 23 '20
webpack is a tricky pony, you can never trust your build config to be the right one for your needs.
even after 2 years using it I don't know how the hell my build config works at times, I just deposit partially my trust in it.
after checking Parcel i'd say webpack could do with at least providing a terminal wizard to set it up.
8
u/2Punx2Furious Jul 23 '20
Yeah, official documentations are often like that sadly.
9
u/yramagicman Jul 23 '20
Django, the python web framework, has docs which are incredibly beginner friendly while still being thorough enough to provide everything you need to get anything you need to do done. To this day I don't think I have seen better documentation than what django provides.
11
1
u/mr_axe Jul 24 '20
I've been learning a bit of data science with python and honestly all the python projects that I've come across are better written than almost all of the JS ones
1
u/yramagicman Jul 24 '20
It's not really surprising to be honest. If you run
import thisat the python repl it pulls up "the zen of python" which has become the guiding philosophy for the python community. It's all solid ideas about good design and good software. Javascript doesn't have a guiding philosophy the same way python does, and I think it shows.2
u/feketegy Jul 24 '20
Webpack is not really intended for beginners imho
4
u/CromulentEntity Jul 24 '20
I think they meant for people starting with webpack as beginners rather than complete beginners
2
u/yramagicman Jul 24 '20
Bingo! That's absolutely the way I intend it. I've been at this long enough that figuring out a build system shouldn't be difficult, but webpack found a way to make it challenging.
14
5
2
2
2
u/OhhhhhSHNAP Aug 31 '20
How bout browsing through stackoverflow posts to figure out all the rando breaking changes, package renames and deprecations of this BS. I think that's actually the 'normal' introduction.
2
8
2
1
u/sergi_dev087 Jul 24 '20
I only use webpack on preconfigured environments like create-react-app a gatsby site, etc...I compketely stopped trying to learn how to configure that thing. For everything else Parcel is a great zero config alternative.
1
u/Keevez Jul 24 '20
Does anyone know how to cache-bust webpack bundles? I still haven't quite figured out how to inject the hash into index file, aside from having webpack build the file(which is not a reasonable option for my application).
15
u/PeteCapeCod4Real Jul 23 '20
Nice this is a great read! Way to break it down so it's easy to understand. Because we all know Webpack can get outta control real quick 😆👍