20.2 First app, build, test
- When the app is completed and built, the file structure is as below:
- folder for the application
- folder for node_modules
- package.json
- folder node_modules
- webpack.config.js
- app.js
- index.html
- bundle.js
- The following are the steps.
- create a folder for the application.
- In the terminal window, enter npm init to create the package.json to define the app.
- modify the package.json to define scripts for use.
- In the terminal window, enter npm install webpack --save-dev to install it.
- folder node-modules is created.
- A lot of related LIBRAIES are downloaded.
- for both test and production.
- create and edit webpack.config.js to define files locations
- create the main js file
- create the main html file
- In the terminal window, enter npm run build
- The js files and the needed libraries are bundled together.
- to reduce traffic for efficiency
-------------- build -----------------------
code as below:
// package.json, modify
"scripts" {
"build": "webpack"
},
// create webpack.config.js under the app folder, add code as below:
module.exports = {
entry: "./app.js", // under the app folder
output: {
filename: "bundle.js" // default is app folder
}
}
// add app.js under the app folder
console.log("app loaded")
// under the root, add index.html
home
- build in the terminal window.
- npm run build
- You can see bundle.js is created.
- in the finder window, right-click index.html
- test with FTP protocal
- open with browser
- You'll see the page
- click the developer tool, you can see the message "app loaded".
- in classic html, index.html is the default page.
- The web request uses ftp protocal for a simple test.
20.3 webpack dev server
intro
- webpack dev server is a more better way to test.
- It is a nodejs Express server as node app's webpack middleware.
- Once you start the server, it will listen to any events from code changes...
- When the test is done, you have to stop the server by ctrl-c
- When the server is started, or any js code change, there is a bundle build process.
// code
// continue from the previous lab
// step 1:
npm install webpack-dev-server --save-dev
// step 2: in package.json in key scripts, adding
"dev": "webpack-dev-derver"
// step 3: in webpack.config.js adding a new key
devServer: {
contentBase: __dirname // using the bundle file under the root,
port: 8080
}
- in the terminal window, enter npm run dev
- open your browser localhost:8080
- open the developer tool to examine server side messages.
- ctrl-c to stop the server, when the test completed.
notes
- The bundle file is created in memory. You can verify this by examining the file creation timestamp.
- If you want to see the content of the bundle, a separate build run is needed.
- Both app.js and index.html is under the app root folder.
20.4 partition of js files
// create the folder structure as below:
| src
index.js
another.js
| dist
index.html
--- /src/another.js ---
var greeting = "happy new year 2018"
document.write(greeting)
--- /src/index.js ----
require("./another.js")
// under the same folder, src
// require is a nodejs builtin function
var arr = ['peter', 'tairo', 'emi']
document.write("from " + arr)
--- webpack.config.js ----------
var path = require("path")
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.join(__dirname, "dist")
},
devServer:{
contentBase: path.join(__dirname, "dist"),
port: 8080
}
}
---- package.json ------
"scripts": {
"dev": "wepack-dev-server",
"build": "webpack"
},
- Two js files are under folder src.
- index.html and bundle.js are under folder dist.
- enter npm run dev to start the server.
- launch browser, enter localhost:8080 to see the page.
- open the developer tool to examine any issue.
- ctrl-c to stop the server.
- enter npm run build to examine the bundle file.
20.5 managing folders and files
// create the folder structure as below:
| js
app.js
| public
index.html
--- /js/app.js ---
document.write("hello world")
console.log("app loaded")
--- webpack.config.js ----
var path = require("path")
module.exports = {
// --------------------------------
// for app.js, set the folder to js
// array is used for multiple bundle files
context: path.resolve("js"),
entry: ["./app"],
// --------------------------------
// Key path is the physical folder address.
// if the folder is not there, it will be created.
// Key publicPath is for access.
output: {
path: path.resolve('build/js/'),
publicPath: '/public/assets/js/',
filename: "bundle.js"
},
// --------------------------------
// index.html is under folder public.
devServer:{
contentBase: "public",
port: 8080
},
}
--- /public/index.html ---
terst at 3:10 pm
"
---- package.json ------
"scripts": {
"dev": "wepack-dev-server",
"build": "webpack"
},
- When the coding is ready.
- enter npm run build
You'll see folder build, and bundle.js are created.
- enter npm run dev
in the terminal window, you can see the bundle is created.
launch your brower, enter localhost:8080
you see the page.
Open the developer tool, to make sure there is no issue.
When the test is completed, ctrl-c to stop the server.
20.6 Multiple bundles
- create an app, cloned the 20.5 app.
- under folder public, replace 3 html files
- index.html, service.html, contact.html
- under folder js, replace 3 js files.
- home.js, service.js, contact.js
---- index.html ------------
---- replace tag h1 content for other two -----
Home page
----- index.js
---- replace the content for other two -----
console.log('App loaded')
------- webpack.config.js -----------------------
var path = require("path")
module.exports = {
context: path.resolve("js"),
entry: { // the contents is an object
service: './service.js', // it is not an array
home: './home.js', // item key name like home is used for output
contact: './contact.js'
},
output: {
path: path.resolve('build/js/'),
publicPath: '/public/assets/js/',
filename: "[name].js" // it is an template
},
devServer:{
contentBase: "public",
port: 8080
}
}
- enter npm run dev
- in the terminal window, you can see
three bundles is created.
- launch your brower, enter localhost:8080 to see pages
20.7 UglifyJS, webpack plug in - 1
During the development streamline with webpack3, this plugin will be plugged in at later stage.
- cloned from the app for 20.2,First app
- in the terminal window, enter
npm install uglifyjs-webpack-plugin --save-dev
- This package is generic, not webpack's libray. Using npm to install is required,
- in webpack.config.js, adding two lines
var UglifyJsPlugin = require("uglifyjs-webpack-plugin")
plugins: [
new UglifyJsPlugin()
]
- The first line is at the top.
- Then, add key plugins for module.exports
- For test purpose, add many lines like document.write("hello")
- npm run build
- open the bundle file, all the white spaces are removed.
- use ftp to examine the page result, open the developer tool to make sure no issue.
20.8 ProvidePlugin, webpack plug in - 2
During the development streamline with webpack3, this plugin will be plugged in at an earlier stage.
- The objective of using this plugin is to share libraries, like jquery globally.
- Create an app by cloning from the app for 20.4, partition of js files
- enter npm install jquery --save
This dependent library is for runtime, not just for development time.
- in webpack.config.js, adding two lines
var webpack = require("webpack")
plugins: [
new webpack.ProvidePlugin({
$: "jquery"
})
]
note 1: $ is a global variable.
note 2: jquery is the library name.
- The first line is at the top.
- ProvidePlugin is a library in webpack.
- Then, add key plugins for module.exports
- enter npm run dev to see no break.
index.html, add code as below:
default content 1
default content 1
- index.js, add code as below:
$("#div1").text("hello from index.js")
- another.js, add code as below:
$("#div2").text("hello from index.js")
- in the terminal window, enter npm run dev
- open your browser, enter localhost:8080 to see the page as below:
hello from index.js
hello from another.js
- Because variable $ is global. Both js files can use it.
20.9 javascript version 6, loader-1
- --- setup steps -----
- Create an app by cloning from the app for 20.32,webpack dev server
- package.json, add one item for key "scriptd"
"build": "webpack"
later for examining bundle file
- app.js, add the following code
const myDogs = ["tairo", "emi"]
document.write(myDogs)
- in the terminal window, enternpm run dev
- open browser, localhost:8080, to see the result.
- enternpm run build
- examine the bundle code, keyword const is there.
- --- comments ------
- My development environment is OK with javascript version 6.
- Not ALL are OK with javascript version 6 NOW(Jan. 2018).
- Babel loader is used to tanspile...
- Javascipt version 6 is for better code readability, which is important for later code enhancements.
// step 1: install related packages
npm install babel-loader babel-core babel-preset-env --save-dev
// step 2: add code in webpack.config.js
module: {
rules: [
{
test: /.js$/, // filtering
exclude: /(node_modules)/,
use: {
loader: "babel-loader", // loader - transform and load them into the js bundle file to reduce http requests
options: {
presets: ["env"] // for langauge transformation AND react
}
}
}
]
}
note-1: key test is for filtering process.
note-2: allowing file with js extension.
note-3: two // is the syntax for javascript regular expression.
note-4: sometimes, \ is added as an escape character.
// step 3: under app root folder, create a new file .babelrc
{
"preset": "env"
}
// step 4: npm run build
open and examine the bundle file
- keyword const(for javascript version 6) is changed to var.
- One line of code is added for the top. "use strict"
- Two changes are required by plain javascript.
- Keyword const means that the object is not mutable. No listening is needed.
- String literal, "use strict" means to declaring a variable is needed, before it is used..
// step 5: test
- npm run dev
- open the browser, localhost:8080, see the same result page
20.10 react, loader-2
- Using Babel to transform jsx to javascript.
- Create an app by cloning from the app for 20.9,javascript version 6
// step 1: install related packages
npm install babel-preset-react --save-dev
npm install react react-dom --save
verify in package.json
// step 2: index.html add one html tag
// step 3: app.js replace the code as below:
import React from "react" // import is a javascript satement
import ReactDom from "react-dom"
const MyComponent = () => hello react
ReactDom.render(, document.getElementById("react-container"))
// step 4: add "react" to key presets for both webpack.config.js and .babelrc
["env", "react"]
// step 5: start the server, npm run dev, then start the browser, localhost:8080
you see the page, with the message, "hello react"
20.11 css, loader-3
// step 1: create an app
Create an app by cloning from the app for 20.10
// step 2: under the root, add style.css
h1 {
color: #FF0000;
}
// step 3: app.js add one line of code as below:
import "./style.css"
// step 4: install loaders
npm install style-loader css-loader --save-dev
// step 5: webpack.config.js add one item as below
rules: [
{...
},
{
test: /\.css$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"}
]
}
]
// step 6: test
start the server: npm run dev
launch the browser: localhost:8080 to see red color for h1 tags
-
The css file is bundled into the bundle file.
// the same for images
{
test: /\.jpg$/,
use: [
{loader: "url-loader"}
]
}
20.12 commons chunk bundle
- The purpose is to have a bundle file for shared js code.
- node_modules/webpack/lib/optimize/CommonsChunkPlugins.js serves the purpose.
- Its size is more sginificantly large, taking care of caching task...
- This bundle file is used before the pages' bundle files.
- create an app from 20.6, multiple bundles.
- Follow 20.10, to create reactjs environment.
- Adding code for all three js file as below:
// home.js
// index.html needed
// code the same for service.js, contact.js, change the contents a bit.
import React from "react"
import ReactDom from "react-dom"
const MyComponent = () => hello from home.js
ReactDom.render(, document.getElementById("react-container"))
// npm run dev, localhost:8080 to see the page from reactjs.
// in webpack.config.js add item under module.exports
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "commons",
filename: "commons.bundle.js"
})
]
---- adding the shared bundle before a page bundle like below ---
Home page
- in the terminal window, enter npm run dev
- You can see the commons.bundle.js is relatively large.
- in the browser, enterlocalhost:8080 to see three pages.