download the forkify starter files.
Install Node
download node via command line or an executable/dmg file from Node’s website.
check if node is installed correctly:
node -v
Go into the forkify folder and you’ll see folders:
– dist
– src
This is where our source files are.
Go ahead and make this into a project: npm init
Go ahead and fill it in. The output should look something like this:
bogon:starter$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See npm help json
for definitive documentation on these fields
and exactly what they do.
Use npm install
afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (starter) forkify
version: (1.0.0)
description: forkify project
entry point: (index.js)
test command:
git repository:
keywords:
author: ricky t
license: (ISC)
About to write to /Users/*****/Desktop/complete-javascript-course/9-forkify/starter/package.json:
{
"name": "forkify",
"version": "1.0.0",
"description": "forkify project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ricky t",
"license": "ISC"
}
Is this OK? (yes) yes
Once you’re done, go back to the directory, and you’ll see a package.json.
Now let’s install webpack: npm install webpack –save-dev
It will save webpack as a development dependency. Now go back and look at your packpage.json, you’ll see that webpack has been added.
Your packpage.json should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "name": "forkify", "version": "1.0.0", "description": "forkify project", "main": "index.js", "scripts": "...test", "author": "ricky t", "license": "ISC", "devDependencies": { "webpack": "^4.41.2", } } |
Now let’s install jQuery: npm install jquery –save
It will save jQuery not as a dev dependency, but as dependency.
You can also uninstall jQuery like so: npm uninstall jQuery –save
Let’s install local web server: npm install live-server –global
If you get an error, make sure you use ‘sudo’ to give permission:
sudo npm install live-server –global
when you install globally, you can run it anywhere by typing:
live-server
In main directory, create file webpack.config.js:
1 2 3 4 5 6 7 8 9 10 |
const path = require('path'); module.exports = { entry: './src/js/index.js', output: { path: path.resolve(__dirname, 'dist/js'), filename: 'bundle.js' }, mode: 'development', } |
The entry point is where we want to start running the project. Create a file: /src/js/index.js
1 2 |
import x from './test' console.log(`i imported ${x} from ./test`) |
Basically, we have a value/component in test.js, and we export it to index.js.
Create file: /src/js/test.js
1 2 |
console.log('Imported module'); export default 23; |
The output means that after we package all of our js files together, we want to export the resulting file into ‘dist/js’ with the name ‘bundle.js’.
This bundle.js will have all of our code, which can be easily run in the browser. It literally packs everything for the web to run. If you were to look inside the bundle.js file, you’ll see our js code of test and index.
Install webpack client: npm install webpack-cli –save-dev
Another thing you should look at is change your package.json when you run the scripts. Make sure we’re running the webpack conversion using development or production.
Your package.json should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ "name": "forkify", "version": "1.0.0", "description": "forkify project", "main": "index.js", "scripts": { "dev": "webpack --mode development", "build": "webpack --mode production" }, "author": "ricky t", "license": "ISC", "devDependencies": { "webpack": "^4.41.2", "webpack-cli": "^3.3.10" }, "dependencies": { "jquery": "^3.4.1" } } |
Now, when you go: npm dev run, webpack should convert our web files into a bundle.js.
Built at: 12/15/2019 6:23:26 PM
Asset Size Chunks Chunk Names
bundle.js 1020 bytes 0 [emitted] main
Entrypoint main = bundle.js
[0] ./src/js/index.js + 1 modules 143 bytes {0} [built]
| ./src/js/index.js 92 bytes [built]
| ./src/js/test.js 51 bytes [built]
However, we need to put this bundle into an html file so we can see it:
create index.html in distr/
Then in index.html, type ‘!’, then press ‘tab’.
you’ll get the typical html template. Then in body tag, put:
your index.html should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>hohoho</title> </head> <body> <script src="js/bundle.js"></script> </body> </html> |
Open the index html file dist/index.html and when you see the html load, it will also load the bundle.js file. You’ll see the test.js and index.js print out the results.Make sure your web server is running by typing live-server.
Development Server
Let’s install this so we don’t have to run webpack manually everytime.
1 |
npm install webpack-dev-server --save-dev |
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "name": "forkify", "version": "1.0.0", "description": "forkify project", "main": "index.js", "scripts": { "dev": "webpack --mode development", "build": "webpack --mode production", "start": "webpack-dev-server --mode development --open" }, "author": "ricky t", "license": "ISC", "devDependencies": { "webpack": "^4.41.2", "webpack-cli": "^3.3.10", "webpack-dev-server": "^3.10.1" }, "dependencies": { "jquery": "^3.4.1", "lodash": "^4.17.15" } } |
webpack.config.js
1 2 3 4 5 6 7 8 9 10 11 12 |
const path = require('path'); module.exports = { entry: './src/js/index.js', output: { path: path.resolve(__dirname, 'dist/js'), filename: 'bundle.js' }, devServer: { contentBase: './dist' } } |
now run:
npm run start
you’ll get a browser up and running with the code. Now, when you make changes to your code, you will get live compiling of the files. You don’t need to go to your terminal and run “npm run dev” every single time. Every save you make will automatically compile and rerun the server.
That you can go straight to the webpage to see the updates.
Now we try to run it with the given css from the css folder
remove index.html in distr/js
make sure to install html-webpack-plugin:
1 |
npm install html-webpack-plugin --save-dev |
webpack.config.js
we alter our webpack.config.js like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/js/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'js/bundle.js' }, devServer: { contentBase: './dist' }, plugins: [ new HtmlWebpackPlugin({ filename: "index.html", template: "./src/index.html" }) ] } |
however, u won’t see it because it’s not saving it on disk.
It simply injects the script.
If you want to save it to disk, run:
npm run dev
At this point, your files should look something like this:
Installing Babel
Babel is a JS compiler, in order to use next generation JS.
npm install babel-core babel-preset-env babel-loader –save-dev
check out package.json file to make sure you’ve installed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{ "name": "forkify", "version": "1.0.0", "description": "forkify project", "main": "index.js", "scripts": { "dev": "webpack --mode development", "build": "webpack --mode production", "start": "webpack-dev-server --mode development --open" }, "author": "ricky t", "license": "ISC", "devDependencies": { "babel-core": "^6.26.3", "babel-loader": "^8.0.6", "babel-preset-env": "^1.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.41.2", "webpack-cli": "^3.3.10", "webpack-dev-server": "^3.10.1" }, "dependencies": { "jquery": "^3.4.1", "lodash": "^4.17.15" } } |
Loaders allow us to import all different kind of files and process them. Convert es6 to es5. Or convert less to sass.
We need to babel loader to do the conversion.
There are certain rules to do these loading. So in our webpack.config.js, we need to define rules. It is an array.