31
loading...
This website collects cookies to deliver better user experience
src
folder and put script.js
, index.html
and style.css
in it.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document Name</title>
</head>
<body>
</body>
</html>
script.js
add:import ./style.css
style.css
add:* {
margin: 0;
padding: 0;
yarn add webpack webpack-cli
in CLI to add webpack dependencies: webpack and webpack-clibundler
folder at the same level as srcwebpack.common.js
in it.const path = require('path');
module.exports = {
entry: path.resolve(__dirname, '../src/script.js'),
output:
{
filename: 'bundle.[contenthash].js',
path: path.resolve(__dirname, '../dist')
},
devtool: 'source-map',
}
"scripts": {
"test": "webpack --config ./bundler/webpack.common.js",
},
npm run test
any time in between to see the output in the dist
foldermodule.exports = {
entry: path.resolve(__dirname, '../src/script.js'),
output:
{
filename: 'bundle.[contenthash].js',
path: path.resolve(__dirname, '../dist')
},
devtool: 'source-map',
module: {
rules: [
…
]
}
};
module.exports = {
entry: path.resolve(__dirname, '../src/script.js'),
output:
{
filename: 'bundle.[contenthash].js',
path: path.resolve(__dirname, '../dist')
},
devtool: 'source-map',
plugins:[
...
],
module: {
rules: [
…
]
}
};
yarn add html-webpack-plugin
:
This is a webpack plugin that simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.const HtmlWebpackPlugin = require('html-webpack-plugin')
plugins:[
new HtmlWebpackPlugin({
template: path.resolve(__dirname, '../src/index.html'),
minify: true
}),
],
yarn add html-loader
//HTMl:
{
test: /\.(html)$/,
use: ['html-loader']
},
yarn add @babel/core
yarn add @babel/preset-env
yarn add babel-loader
//JS
{
test: /\.js$/,
exclude: /node_modules/,
use:[
'babel-loader'
]
},
yarn add mini-css-extract-plugin
yarn add css-loader
const MiniCSSExtractPlugin = require('mini-css-extract-plugin')
plugins:
[
new MiniCSSExtractPlugin()
],
// CSS
{
test: /\.css$/,
use:
[
MiniCSSExtractPlugin.loader,
'css-loader'
]
},
yarn add file-loader
// Images
{
test: /\.(jpg|png|gif|svg)$/,
use:
[
{
loader: 'file-loader',
options: {
outputPath: 'assets/images/'
}
}
]
},
// Fonts
{
test: /\.(ttf|eot|woff|woff2)$/,
use:
[
{
loader: 'file-loader',
options: {
outputPath: 'assets/fonts/'
}
}
]
},
npm run build
to create a production ready distribution folder, webpack will browse through your code and as soon as it finds something like image or fonts, it will automatically create an asset folder inside which there will be an image folder to store that imported image and there will be an font folder in assets created to store its corresponding font.Copies individual files or entire directories, which already exist, to the build directory.
yarn add copy-webpack-plugin
const CopyWebpackPlugin = require('copy-webpack-plugin')
plugins:[
new CopyWebpackPlugin({
patterns: [
{ from: path.resolve(__dirname, '../static') }
]
}),
....
],
webpack.dev.js
file in bundler foldercommonConfiguration
from webpack.common.js
To import we will need webpack-merge
moduleyarn add webpack-merge
const { merge } = require('webpack-merge')
const commonConfiguration = require('./webpack.common.js')
module.exports = merge(
commonConfiguration,
{
mode: 'development'
}
)
package.json
"scripts": {
"test": "webpack --config ./bundler/webpack.common.js",
"dev": "webpack serve --config ./bundler/webpack.dev.js",
},
yarn add portfinder-sync
> Find an open port synchronously.yarn add D webpack-dev-server
Dev Server for web applications, ideal for buildless es module workflows. Optionally supports simple code transformations.
This is added so now thewebpack 'serve'
command is recognised
Updating wepack dev:
Importing required modules:
const ip = require('internal-ip')
const portFinderSync = require('portfinder-sync')
const infoColor = (_message) => {
return `\u001b[1m\u001b[34m${_message}\u001b[39m\u001b[22m`
}
devServer: {
host: '0.0.0.0',
port: portFinderSync.getPort(8080),
contentBase: './dist',
watchContentBase: true,
open: true,
https: false,
useLocalIp: true,
disableHostCheck: true,
overlay: true,
noInfo: true,
after: function(app, server, compiler)
{
const port = server.options.port
const https = server.options.https ? 's' : ''
const localIp = ip.v4.sync()
const domain1 = `http${https}://${localIp}:${port}`
const domain2 = `http${https}://localhost:${port}`
console.log(`Project running at:\n - ${infoColor(domain1)}\n - ${infoColor(domain2)}`)
}
}
npm run dev
and you should see a live server being sprung up! and this is now live updating with all the changes you make!webpack.prod.js
const { merge } = require('webpack-merge')
const commonConfiguration = require('./webpack.common.js')
module.exports = merge(
commonConfiguration,
{
mode: 'production',
}
)
yarn add clean-webpack-plugin
> A webpack plugin to remove/clean your build folder(s).
It makes sure that there is no dist folder.const { merge } = require('webpack-merge')
const commonConfiguration = require('./webpack.common.js')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = merge(
commonConfiguration,
{
mode: 'production',
plugins:
[
new CleanWebpackPlugin()
]
}
)
package.json
for a build command.
"scripts": {
"test": "webpack --config ./bundler/webpack.common.js",
"dev": "webpack serve --config ./bundler/webpack.dev.js",
"build": "webpack --config ./bundler/webpack.prod.js"
},
npm run build
and check the dist folder that would have been created.yarn add raw-loader
// Shaders
{
test: /\.(glsl|vs|fs|vert|frag)$/,
exclude: /node_modules/,
use: [
'raw-loader'
]
}