Webpack – All about (1)

What can Webpack do?

  • Bundle multiple js files into 1 js file with correct js file order
  • Bundle multiple css, images,… files into 1 js file

How to config Webpack?

How to setup output bundle file?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const path = require('path');
module.exports = {
entry: './src/index,js', // Entry point where webpack can start to bundle js files
output: {
filename: 'bundle.js', // Bundle js file name
path: path.resolve(_dirname, './dist') // Absolute path of folder where "bundle.js" is exported to
},
}
const path = require('path'); module.exports = { entry: './src/index,js', // Entry point where webpack can start to bundle js files output: { filename: 'bundle.js', // Bundle js file name path: path.resolve(_dirname, './dist') // Absolute path of folder where "bundle.js" is exported to }, }
const path = require('path');

module.exports = {
  entry: './src/index,js',      // Entry point where webpack can start to bundle js files
  output: {
    filename: 'bundle.js',      // Bundle js file name
    path: path.resolve(_dirname, './dist')    // Absolute path of folder where "bundle.js" is exported to
  },
}

How to setup dev server?

Use “webpack-dev-server”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
mode: 'development',
devServer: {
port: 8081 // This makes "webpack serve" run on http://localhost:8081
},
...
}
module.exports = { mode: 'development', devServer: { port: 8081 // This makes "webpack serve" run on http://localhost:8081 }, ... }
module.exports = {
  mode: 'development',
  devServer: {
    port: 8081                  // This makes "webpack serve" run on http://localhost:8081
  },
  ...
}

How to insert bundle js into template HTML file?

Use html-webpack-plugin

  • Create a template HTML file
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</html>
<!DOCTYPE html> <html> <head></head> <body></body> </html>
<!DOCTYPE html>
<html>

<head></head>

<body></body>

</html>
  • Setup Webpack
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
...
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
})
]
}
const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { ... plugins: [ new HtmlWebpackPlugin({ template: './public/index.html', }) ] }
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  ...

  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
    })
  ]
}
  • Here is the result
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head></head>
<body><script src="bundle.js"></script></body>
</html>
<!DOCTYPE html> <html> <head></head> <body><script src="bundle.js"></script></body> </html>
<!DOCTYPE html>
<html>

<head></head>

<body><script src="bundle.js"></script></body>

</html>
  • Show js content into HTML body tag
    Edit HTML template
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="root"></div>
</body>
</html>
<!DOCTYPE html> <html> <head></head> <body> <div id="root"></div> </body> </html>
<!DOCTYPE html>
<html>

<head></head>

<body>
  <div id="root"></div>
</body>

</html>

Edit entry js file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const rootElement = document.getElementById('root');
rootElement.innerHTML = '<div>Testing</div>';
const rootElement = document.getElementById('root'); rootElement.innerHTML = '<div>Testing</div>';
const rootElement = document.getElementById('root');
rootElement.innerHTML = '<div>Testing</div>';

How to use React & Babel?

  • Install packages
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
yarn add react react-dom
yarn add babel-loader @babel/core @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime @babel/preset-env @babel/preset-react --dev
yarn add react react-dom yarn add babel-loader @babel/core @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime @babel/preset-env @babel/preset-react --dev
yarn add react react-dom

yarn add babel-loader @babel/core @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime @babel/preset-env @babel/preset-react --dev
  • Create “babel.config.js”
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"presets": [
[
"@babel/preset-env",
{
"modules": "auto",
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11"
]
}
}
],
[
"@babel/preset-react",
{
"runtime": "automatic"
}
]
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime"
]
}
{ "presets": [ [ "@babel/preset-env", { "modules": "auto", "targets": { "browsers": [ "last 2 Chrome versions", "last 2 Firefox versions", "last 2 Safari versions", "last 2 iOS versions", "last 1 Android version", "last 1 ChromeAndroid version", "ie 11" ] } } ], [ "@babel/preset-react", { "runtime": "automatic" } ] ], "plugins": [ "@babel/plugin-proposal-class-properties", "@babel/plugin-transform-runtime" ] }
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": "auto",
        "targets": {
          "browsers": [
            "last 2 Chrome versions",
            "last 2 Firefox versions",
            "last 2 Safari versions",
            "last 2 iOS versions",
            "last 1 Android version",
            "last 1 ChromeAndroid version",
            "ie 11"
          ]
        }
      }
    ],
    [
      "@babel/preset-react",
      {
        "runtime": "automatic"
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-runtime"
  ]
}
  • Configure webpack
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module: {
rules: [
// JavaScript: Use Babel to transpile JavaScript files
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
}
module: { rules: [ // JavaScript: Use Babel to transpile JavaScript files { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'] } ] }
  module: {
    rules: [
      // JavaScript: Use Babel to transpile JavaScript files
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      }
    ]
  }
  • Edit entry file using React & jsx
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import ReactDOM from 'react-dom'
import App from './App'
ReactDOM.render(<App />, document.getElementById('root'))
import ReactDOM from 'react-dom' import App from './App' ReactDOM.render(<App />, document.getElementById('root'))
import ReactDOM from 'react-dom'
import App from './App'

ReactDOM.render(<App />, document.getElementById('root'))

How to use “dotenv-webpack”?

Install

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
yarn add dotenv-webpack --dev
yarn add dotenv-webpack --dev
yarn add dotenv-webpack --dev

Usage

  • Create .env.development file
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// .env.development
DB_HOST=127.0.0.1
DB_PASS=foobar
S3_API=mysecretkey
// .env.development DB_HOST=127.0.0.1 DB_PASS=foobar S3_API=mysecretkey
// .env.development
DB_HOST=127.0.0.1
DB_PASS=foobar
S3_API=mysecretkey
  • Add it to webpack
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// webpack.config.js
const Dotenv = require('dotenv-webpack');
module.exports = {
...
plugins: [
new Dotenv({
path: './.env.development',
}),
]
...
};
// webpack.config.js const Dotenv = require('dotenv-webpack'); module.exports = { ... plugins: [ new Dotenv({ path: './.env.development', }), ] ... };
// webpack.config.js
const Dotenv = require('dotenv-webpack');

module.exports = {
  ...
  plugins: [
    new Dotenv({
      path: './.env.development',
    }),
  ]
  ...
};
  • Use it in code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// file1.js
console.log(process.env.DB_HOST);
// '127.0.0.1'
// file1.js console.log(process.env.DB_HOST); // '127.0.0.1'
// file1.js
console.log(process.env.DB_HOST);
// '127.0.0.1'

How to setup proxy for localhost request?

How to make a request to /api/users proxy to http://localhost:3000/api/users?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
//...
devServer: {
proxy: {
'/api': 'http://localhost:3000',
},
},
};
module.exports = { //... devServer: { proxy: { '/api': 'http://localhost:3000', }, }, };
module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': 'http://localhost:3000',
    },
  },
};

How to omit “/api” in proxy request?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: { '^/api': '' },
},
},
},
};
module.exports = { //... devServer: { proxy: { '/api': { target: 'http://localhost:3000', pathRewrite: { '^/api': '' }, }, }, }, };
module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        pathRewrite: { '^/api': '' },
      },
    },
  },
};

How to proxy multiple paths to the same target?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
//...
devServer: {
proxy: [
{
context: ['/auth', '/api'],
target: 'http://localhost:3000',
},
],
},
};
module.exports = { //... devServer: { proxy: [ { context: ['/auth', '/api'], target: 'http://localhost:3000', }, ], }, };
module.exports = {
  //...
  devServer: {
    proxy: [
      {
        context: ['/auth', '/api'],
        target: 'http://localhost:3000',
      },
    ],
  },
};

How to override origin of host header?

The origin of the host header is kept when proxying by default, you can set changeOrigin to true to override this behaviour.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
},
},
},
};
module.exports = { //... devServer: { proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, }, }, }, };
module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
      },
    },
  },
};

How to embed static Javascript into HTML template file?

Usually, you embed your static Javascript into HTML template file as below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
window.myData = {
MY_DATA: []
}
</script>
</head>
<body>
</body>
</html>
// public/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <script> window.myData = { MY_DATA: [] } </script> </head> <body> </body> </html>
// public/index.html

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8" />
    <script>
        window.myData = {
          MY_DATA: []
        }
    </script>
  </head>

  <body>

  </body>

</html>

you can make it a js file and embed it into HTML template file via webpack like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// config/dev.config.js
window.myData = {
API_END_POINT: 'https://xxxx.com/',
MY_DATA: []
}
// config/dev.config.js window.myData = { API_END_POINT: 'https://xxxx.com/', MY_DATA: [] }
// config/dev.config.js

window.myData = {
  API_END_POINT: 'https://xxxx.com/',
  MY_DATA: []
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// webpack config file
const CopyPlugin = require('copy-webpack-plugin')
...
plugins: [
new CopyPlugin({
patterns: [{ from: 'config/dev.config.js', to: 'config.js' }],
}),
]
// webpack config file const CopyPlugin = require('copy-webpack-plugin') ... plugins: [ new CopyPlugin({ patterns: [{ from: 'config/dev.config.js', to: 'config.js' }], }), ]
// webpack config file

const CopyPlugin = require('copy-webpack-plugin')

...

plugins: [
    new CopyPlugin({
      patterns: [{ from: 'config/dev.config.js', to: 'config.js' }],
    }),
]
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!-- HTML template file -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<script src="config.js?v=#{version}"></script>
</body>
</html>
<!-- HTML template file --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> </head> <body> <script src="config.js?v=#{version}"></script> </body> </html>
<!-- HTML template file -->

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8" />
  </head>

  <body>

    <script src="config.js?v=#{version}"></script>

  </body>

</html>

How to use relative path without ‘../’

  • webpack config file
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/')
}
}
resolve: { alias: { Utilities: path.resolve(__dirname, 'src/utilities/'), Templates: path.resolve(__dirname, 'src/templates/') } }
resolve: {
  alias: {
     Utilities: path.resolve(__dirname, 'src/utilities/'),
     Templates: path.resolve(__dirname, 'src/templates/')
  }
}

now you can use

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Instead of using relative paths
import config from '../../../utilities/configs'
// Now you can do
import config from 'Utilities/configs'
// Instead of using relative paths import config from '../../../utilities/configs' // Now you can do import config from 'Utilities/configs'
// Instead of using relative paths
import config from '../../../utilities/configs'

// Now you can do
import config from 'Utilities/configs'
  • In order to make VSCode understand your new alias, edit “tsconfig.json”
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
...
"baseUrl": ".",
"paths": {
"Utilities/*": ["./src/utilities/*"],
"Templates/*": ["./src/templates/*"]
},
}
{ ... "baseUrl": ".", "paths": { "Utilities/*": ["./src/utilities/*"], "Templates/*": ["./src/templates/*"] }, }
{
    ...

    "baseUrl": ".",
    "paths": {
      "Utilities/*": ["./src/utilities/*"],
      "Templates/*": ["./src/templates/*"]
    },
}
  • Add package.json into “utilities” and “templates” folders
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"name": "utilities"
}
{ "name": "utilities" }
{
  "name": "utilities"
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"name": "templates"
}
{ "name": "templates" }
{
  "name": "templates"
}

How to bundle asset modules (Webpack 5)

webpack.config.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
...
module.exports = {
...
output: {
...
publicPath: 'dist/'
}
module: [
{
test: /\.(png|jpg)$/,
type: 'assets/resource'
}
]
}
... module.exports = { ... output: { ... publicPath: 'dist/' } module: [ { test: /\.(png|jpg)$/, type: 'assets/resource' } ] }
...
module.exports = {
  ...
  output: {
    ...
    publicPath: 'dist/'
  }
  module: [
    {
       test: /\.(png|jpg)$/,
       type: 'assets/resource'
    }
  ]
}

how to set a public path for asset module

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
...
module.exports = {
...
module: [
{
test: /\.(png|jpg)$/,
type: 'assets/resource'
}
]
}
... module.exports = { ... module: [ { test: /\.(png|jpg)$/, type: 'assets/resource' } ] }
...
module.exports = {
  ...
  module: [
    {
       test: /\.(png|jpg)$/,
       type: 'assets/resource'
    }
  ]
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*