Académique Documents
Professionnel Documents
Culture Documents
BeginnersguidetoWebpackMedium
Nader Dabit
Follow
Getting Started
Webpack Conventions
1. Webpack works best with NPM, not Bower
2. Uses a module system (AMD, CommonJS, ES6)
Installing Webpack globally:
npminstallwebpackg
document.write('welcometomyapp')
console.log('apploaded')
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
1/16
25/01/2017
BeginnersguidetoWebpackMedium
In index.html:
<html>
<body>
<scriptsrc="bundle.js"></script>
</body>
</html>
webpack./app.jsbundle.js
Dening a congle
A conguration le in Webpack is basically a common.js module. The
cong le is a place to put all of your conguration, loaders
(explained later), and other specic information relating to your
build.
In your root directory, create a le called webpack.cong.js, and add
the following code:
module.exports={
entry:"./app.js",
output:{
filename:"bundle.js"
}
}
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
2/16
25/01/2017
BeginnersguidetoWebpackMedium
webpack
webpackwatch
2. Go into the cong le, and add a key called watch, and set it to
true
module.exports={
entry:"./app.js",
output:{
filename:"bundle.js"
},
watch:true
}
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
3/16
25/01/2017
BeginnersguidetoWebpackMedium
After watch is set to true, when you run the webpack command,
webpack will rebuild your bundle when any of your les change.
Webpack Dev Server
Webpack has a web server called webpack-dev-server.
From the command line, install webpack-dev-server globally:
npminstallwebpackdevserverg
webpackdevserver
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
4/16
25/01/2017
BeginnersguidetoWebpackMedium
(hot-loading).
The default is to have hot-loading enabled. To disable hot-loading and
remove the App Ready status bar at the top, remove the /webpackdev-server/ from the url, and go to http://localhost:8080/.
To enable hot-loading and remove the App Ready status bar at the
top, terminate the webpack-dev-server and rerun webpack-dev-server
with the inline ag:
webpackdevserverinline
Navigate to http://localhost:8080/
Now you will see that hot-loading is still enabled, but the status bar is
now removed.
Building multipleles
1. Requiring les
Add a le called logger.js to your project.
In logger.js, add the following code:
console.log('logger.jsisnowloaded...')
In app.js, add the following to require the logger.js le at the very top
before any other code:
require('./logger')
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
5/16
25/01/2017
BeginnersguidetoWebpackMedium
console.log('global.jsisnowloaded...')
module.exports={
entry:["./global.js","./app.js"],
output:{
filename:"bundle.js"
}
}
Now, close and rerun webpack-dev-server, you should now also see
global.js is now loaded being logged to the console when you reload
http://localhost:8080/webpack-dev-server/.
npminstallbabelcorebabelloaderjshintjshintloader
nodelibsbrowserbabelpresetes2015babelpresetreact
webpacksavedev
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
6/16
25/01/2017
BeginnersguidetoWebpackMedium
letcheckName=(firstName,lastName)=>{
if(firstName!=='nader'||lastName!=='dabit'){
console.log('YouarenotNaderDabit')
}else{
console.log('YouareNaderDabit')
}
}
checkName('nader','jackson')
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
7/16
25/01/2017
BeginnersguidetoWebpackMedium
module.exports={
entry:["./global.js","./app.js"],
output:{
filename:"bundle.js"
},
module:{
loaders:[
{
test:/\.es6$/,
exclude:/node_modules/,
loader:'babelloader',
query:{
presets:['react','es2015']
}
}
]
},
resolve:{
extensions:['','.js','.es6']
},
}
As you can see above, we have added three keys to our rst loader.
1. test a regular expression that tests what kind of les to run
through this loader. As you can see, we have added a regex to
test all les with an es6 extension.
2. exclude which les the loader should exclude /ignore. We
have added the node_modules folder.
3. loader the name of the loader we are going to use (babelloader).
4. query You can pass options to the loader by writing them as a
query string or by using the query property as we have done
above.
5. cacheDirectory Default false. When set, the given directory
will be used to cache the results of the loader. Future webpack
builds will attempt to read from the cache to avoid needing to
run the potentially expensive Babel recompilation process on
each run
6. presets lets us use the react and es2015 presets that were
installed earlier
We have also added a key named resolve to our module.exports
object. resolve is a section which lets us specify what kind of le types
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
8/16
25/01/2017
BeginnersguidetoWebpackMedium
require('./logger')
instead of:
require('./logger.es6')
module.exports={
entry:["./global.js","./app.js"],
output:{
filename:"bundle.js"
},
module:{
preLoaders:[
{
test:/\.js$/,
exclude:/node_modules/,
loader:'jshintloader'
}
],
loaders:[
{
test:/\.es6$/,
exclude:/node_modules/,
loader:'babelloader',
query:{
cacheDirectory:true,
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
9/16
25/01/2017
BeginnersguidetoWebpackMedium
presets:['react','es2015']
}
}
]
},
resolve:{
extensions:['','.js','.es6']
}
}
Creating a StartScript
This is actually a feature of npm and the functionality you can use in
your package.json le, but it is something worth doing to make your
process easier.
In package.json, there is a key called scripts. Delete the contents of
the scripts key, and replace it with the following:
"scripts":{
"start":"webpackdevserver"
},
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
10/16
25/01/2017
BeginnersguidetoWebpackMedium
npmstart
This will make more sense in the future as we add more options into
our webpack-dev-server command, and make it easier to rerun often,
but is still nice to have even now.
webpackp
npminstallstriploadersavedev
varWebpackStripLoader=require('striploader')
vardevConfig=require('./webpack.config.js')
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
11/16
25/01/2017
BeginnersguidetoWebpackMedium
varstripLoader={
test:[/\.js$/,/\.es6$/],
exclude:/node_modules/,
loader:WebpackStripLoader.loader('console.log')
}
devConfig.module.loaders.push(stripLoader)
module.exports=devConfig
loader:WebpackStripLoader.loader('console.log')
webpackconfigwebpackproduction.config.jsp
This runs webpack with the cong ag, letting us specify a custom
cong le. -p minies the code for production.
Open bundle.js. If you do a nd and replace you will notice there are
no console.log statements in our bundle.
12/16
25/01/2017
BeginnersguidetoWebpackMedium
npminstallreactreactdomsave
importReactfrom"react"
exportdefaultReact.createClass({
render:function(){
return(
<div>
Hello,{this.props.name}!
</div>
)
},
})
Open app.js. Remove the contents of app.js, and replace with the
following:
importReactfrom"react"
importReactDOMfrom"reactdom"
importHellofrom"./hello"
ReactDOM.render(
<Helloname="World"/>,
document.body
)
loaders:[
{
test:[/\.js$/,/\.es6$/],
exclude:'node_modules',
loader:'babelloader'
}
]
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
13/16
25/01/2017
BeginnersguidetoWebpackMedium
. . .
My Name is Nader Dabit @dabit3. I am a developer at
@schoolstatusapp. If you enjoyed this article, please recommend and
share it! Thanks for your time.
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
14/16
25/01/2017
BeginnersguidetoWebpackMedium
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
15/16
25/01/2017
BeginnersguidetoWebpackMedium
https://medium.com/@dabit3/beginnersguidetowebpackb1f1a3638460#.yc484m9o1
16/16