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

Related Posts:

  • Có bao nhiêu Scopes trong JavascriptScope Levels1 - Global Scope2 - Module Scope3 - Block Scope4 - Function ScopeGlobal Scope<script src="script.js"></script><script>  // script.js  const a = 1  console.log(a)  // Prints: 1… Read More
  • Non-blocking và BlockingNon-blocking và blocking là hai khái niệm quan trọng trong lập trình hướng đồng bộ.Non-blocking là một thuật ngữ được sử dụng để chỉ việc thực hiện một tác vụ mà không cần chờ đợi kết quả từ tác vụ đó. Trong một non-blocking … Read More
  • Javascript Basic Read More
  • Session và Cookie ?Session và Cookie là hai khái niệm quan trọng trong lập trình web để lưu trữ thông tin và duy trì trạng thái của người dùng trên trang web.  Session (Phiên làm việc) là một cơ chế lưu trữ thông tin trên máy chủ. Khi… Read More
  • JS runtime cách xử lý bất đồng bộ và đồng bộ JS runtime (thời gian chạy của JavaScript) xử lý bất đồng bộ (asynchronous) và đồng bộ (synchronous) theo cách khác nhau.Khi JavaScript chạy trên trình duyệt, runtime sử dụng mô hình sự kiện (event-driven) để xử lý các … Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang