Style Loaders
First, create a normal CSS file in a styles directory. Call in main.css and add a style rule for the heading element.
- h2 {
- background: blue;
- color: yellow;
- }
- npm install style-loader css-loader
- {
- test: /\.css$/,
- exclude: /node_modules/,
- loader: 'style!css'
- }
Because we've updated our configuration file, we'll need to restart the development server for our changes to be picked up. Use ctrl+c to stop the server and webpack-dev-server to start it again.
All we need to do now is require our stylesheet from within our main.js file. We do this in the same way as we would any other JavaScript module:
- const sayHello = require('./say-hello');
- require('./styles/main.css');
- sayHello('Guybrush', document.querySelector('h2'));
You've Got to Sass It
"But nobody uses CSS these days, Grandad! It's all about Sass". Of course it is. Luckily Webpack has a loader to do just the thing. Install it along with the node version of Sass using:
- npm install sass-loader node-sass
- {
- test: /\.scss$/,
- exclude: /node_modules/,
- loader: 'style!css!sass'
- }
- $background: blue;
- h2 {
- background: $background;
- color: yellow;
- }
- require('./styles/main.scss');
Images
"So images, loaders too I bet?" Of course! With images, we want to use the url-loader. This loader takes the relative URL of your image and updates the path so that it's correctly included in your file bundle. As per usual:
- npm install url-loader
- {
- test: /\.(jpg|png|gif)$/,
- include: /images/,
- loader: 'url'
- }
Usually you'll be using some sort of templating system to create your HTML views, but we're going to keep it basic and create an image tag in JavaScript the old-fashioned way. First create an image element, set the required image to the src attribute, and then add the element to the page.
- var imgElement = document.createElement('img');
- imgElement.src = require('./images/my-image.jpg');
- document.body.appendChild(imgElement);
Preloaders
Another task commonly carried out during development is linting. Linting is a way of looking out for potential errors in your code along with checking that you've followed certain coding conventions. Things you may want to look for are "Have I used a variable without declaring it first?" or "Have I forgotten a semicolon at the end of a line?" By enforcing these rules, we can weed out silly bugs early on.
A popular tool for linting is JSHint. This looks at our code and highlights potential errors we've made. JSHint can be run manually at the command line, but that quickly becomes a chore during development. Ideally we'd like it to run automatically every time we save a file. Our Webpack server is already watching out for changes, so yes, you guessed it—another loader.
Install the jshint-loader in the usual way:
- npm install jshint-loader
- module: {
- preLoaders: [
- {
- test: /\.js$/,
- exclude: /node_modules/,
- loader: 'jshint'
- }
- ],
- loaders: [
- ...
- ]
- }
After the module key in our config, add another entry called "jshint" and a line to specify the version of JavaScript.
- module: {
- preLoaders: [
- ...
- ],
- loaders: [
- ...
- ]
- },
- jshint: {
- esversion: 6
- }
- var imgElement = document.createElement('img')
- WARNING in ./main.js
- jshint results in errors
- Missing semicolon. @ line 7 char 47
Now that we're happy our code is in shape and it does everything we want it to, we need to get it ready for the real world. One of the most common things to do when putting your code live is to minify it, concatenating all your files into one and then compressing that into the smallest file possible. Before we continue, take a look at your current bundle.js. It's readable, has lots of whitespace, and is 32kb in size.
"Wait! Don't tell me. Another loader, right?" Nope! On this rare occasion, we don't need a loader. Webpack has minification built right in. Once you're happy with your code, simply run this command:
- webpack -p
Summary
I hope that this two-part tutorial has given you enough confidence to use Webpack in your own projects. Remember, if there's something you want to do in your build process then it's very likely Webpack has a loader for it. All loaders are installed via npm, so have a look there to see if someone's already made what you need.
Written by Stuart Memo
If you found this post interesting, follow and support us.
Suggest for you:
JavaScript Tutorials: Understanding the Weird Parts
ES6 Javascript: The Complete Developer's Guide
Upgrade your JavaScript to ES6
JavaScript Promises: Applications in ES6 and AngularJS
JavaScript For Absolute Beginners - Build Simple Projects
No comments:
Post a Comment