198

Next Level Webpack Dashboard – freeCodeCamp

 6 years ago
source link: https://medium.com/@wesharehoodies/webpack-dashboard-with-create-react-app-vue-cli-and-custom-configs-49166e1a69de
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A Next Level Webpack Dashboard

Level up your webpack using webpack-dashboard

Webpack dashboard screenshot
Beautiful webpack dashboard inside the terminal

Webpack-dashboard has over 13,000 stars on GitHub, yet I almost never encounter developer teams making use of the plugin.

Why not take advantage of this great plugin?

Note: Don’t forget you can pretend you work for NASA if someone shoulder peeks!

When using webpack, especially for a dev server, you are probably used to seeing something like this — according to webpack-dashboard GitHub page.

Webpack screenshot

We’re all familiar with the webpack log above. Although sometimes I wonder — who exactly is this screen for? A vastly superior alien race?

This is why the webpack log desperately needs a new friendlier UI, and webpack-dashboard does that.

Webpack-Dashboard to the Rescue!

This plugin improves your visuals for the webpack logs. Now when you run your dev server, you basically work at NASA.

Webpack dashboard screenshot
1*Lf5kgUjKCag-rb-dtNz-6A.png
Much better

Aren’t the compilation errors and logs much more human-readable?

Let’s take a look at how two of the most popular scaffolding tools use webpack logging.

From the examples above — Webpack lets us know whether our application compiled successfully. It also lets us know which port the webpack server is currently using.

We surely can improve this by having more useful information on the screen.

Error Handling With Create-React-App and Vue-CLI

Here’s how compilation errors currently look when using webpack.

1*H2NhxeIXSX-jnPrd6MnYlA.png
1*G9qBFhEXLdfM1p6jdqOVpg.png
Left — React and Vue — Right

Pretty good — immediately after making a syntax error, we get notified.

What’s more important; we not only get notified — but we’re also given the error message, and where the error occured.

Create-React-App and Vue-CLI Production Output

Here’s what we get when we decide to bundle our application for production.

1*88gsd6GmFXg4oz5otb5PGg.png
1*KVEIxzAlXIN_5LTOXUnLTA.png

No complaints — but there are areas where we could improve. Let’s give the webpack dashboard spin and see how it might help us.

Using Webpack-Dashboard with Custom Config

I’ve set up a basic project using Webpack. Clone the project, install the dependencies, and start the project with the following command.

git clone [email protected]:indreklasn/webpack-2.0-from-scratch.git && cd webpack-2.0-from-scratch && npm i && npm run start

If you’re curious — Here’s a step by step, a five-chapter guide on how this webpack config was set up — strongly recommend reading it through!

After pasting in the command and some patience, you should see the following screen.

1*vBiGshXUnB6p9nSoSfBLVQ.png

Great! Working as intended. Let’s get started with the webpack-dashboard.

Installing webpack-dashboard

Installing via NPM

$ npm install --save-dev webpack-dashboard

Installing via Yarn

$ yarn add --dev webpack-dashboard

Next up— Require the webpack-dashboard plugin in webpack.config.js and call the plugin with the new keyword, like so:

1*pUbaaYJKreweOj_WMPp77A.png
1*o3GiNXsiIUUD4T1PfChxxg.png

Then, modify your dev server start script previously looked like:

"scripts": {
"dev": "node index.js", # OR
"dev": "webpack-dev-server", # OR
"dev": "webpack",
}

Fire up our webpack with npm run start and we get the following

1*ccfctNdhUEhxi9u7M-LmHA.png

Booyah! Awesome!

By the way — we don’t have to have this giant dashboard. Here’s a practical example of resizing the windows.

Screenshot of code
I’m using iTerm2.

Create-React-App With Webpack-Dashboard

React logo
npm i -g create-react-app && create-react-app react-webpack-dashboard && cd react-webpack-dashboard && npm i webpack-dashboard --save-dev && npm run eject
Ejected successfully screenshot

Now I’m not a big fan of ejecting but if you find a work-around, please let me know!

We set it up the same way as with the custom config.

Open config/webpack.config.dev.js and require the dashboard plugin. Also, don’t forget to include the plugin in the plugins: [] section. You can search for the plugin section with CTRL + F. Should be at line ~ 214.

Screenshot of code
Screenshot of code

Open package.json and update the start script to the following:

"start": "webpack-dashboard -- node scripts/start.js",

Testing time! Run the following commandnpm run start

1*yAXH-uizz2VI46j3537sEQ.png
1*eBQ4RjIaeWLcJBL_vZiDaA.png
Works.

Vue-CLI With Webpack-Dashboard

Vue logo

Vue doesn’t have the eject concept. This is a no brainer since we don’t have to make sacrifices.

Open build/webpack.dev.conf.js — require the plugin (line 9) and insert the plugin (line 23)

Screenshot of code

Finally, go to package.json and change the dev script to the following:

“dev”: “webpack-dashboard — node build/dev-server.js”,

Test it:

npm run start

1*OHkPGirvgNB4GSsUqYTAnA.png
1*RkfL7FpIz6WiUnPdA-7kcA.png
It works!

Conclusion

Special thanks to the creators of the webpack-dashboard. Please show appreciation for their hard work — go and star the repository on GitHub!

Thanks for reading, happy coding!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK