28 May 2022

Webpack Init html

 
//==================== Create webpack CLI ===================//
C:\Users\BV_Computer\Downloads\beginer-webpack> npm init -y

{
  "name": "beginer-webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
npm i webpack --save-dev npm i webpack-cli --save-dev mkdir src // Tạo file type nul > index.js (Đặt console.log('Hello!')) npx webpack --mode development (Sau khi chạy lệnh này sẽ render ra folder dist) Thêm vào script của file package.json webpack --mode development webpack --mode production //================ Install html-plugin config.js ================// npm i html-webpack-plugin --save-dev // Tạo file type nul > webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  entry: {
    main: path.resolve(__dirname, './src/index.js'),
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
      new HtmlWebpackPlugin({
        title: 'Demo webpack'
      })
  ]
};
npm run dev (Mở browser dist/index.html để check console.log hiển thị chữ hello!) //================= Install server =================// npm i webpack-dev-server --save-dev https://webpack.js.org/configuration/dev-server/#root
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");

module.exports = {
  entry: {
    main: path.resolve(__dirname, "./src/index.js"),
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  devServer: {
    static: {
      directory: path.join(__dirname, "./dist"),
    },
    compress: true,
    port: 9000,
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "Demo webpack",
    }),
  ],
};

// Update lại script file package.json webpack serve --mode development // Kiểm tra http://localhost:9000/ (Xem console) //================ Install babel =============// npm i babel-loader @babel/core @babel/preset-en v --save-dev https://webpack.js.org/loaders/babel-loader/#root
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");

module.exports = {
  entry: {
    main: path.resolve(__dirname, "./src/index.js"),
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  devServer: {
    static: {
      directory: path.join(__dirname, "./dist"),
    },
    compress: true,
    port: 9000,
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "Demo webpack",
    }),
  ],
  module: {
    rules: [
      {
        test: /\.?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
    ],
  },
};

// Tạo file component.js type nul > component.js
const component = () => {
    const element = document.createElement('h2');
    element.innerHTML = "Hey this is new component";
    return element;
}
export default component;
// Update file index.js
import component from "./component";

document.body.appendChild(component());
// Start app npm run dev
//=============== Install css & syle loader =============// npm i css-loader style-loader --save-dev https://webpack.js.org/guides/asset-management/#loading-images
rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
],
// index.js
import component from "./component";

import logo from "./images/logo-on-dark-bg.png";

import "./styles/style.css";

const img = document.createElement('img');
img.src = logo;
img.width = '300';
document.body.appendChild(img);

document.body.appendChild(component());
// npm run dev

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang