iamvince24 / Journal

This Repo is used to record some learning and notes. I will write some notes as an article to record here Zenn.
0 stars 0 forks source link

[FE201] 前端中階:那些前端會用到的工具們 筆記 #6

Open iamvince24 opened 11 months ago

iamvince24 commented 11 months ago

這邊是 [FE201] 前端中階:那些前端會用到的工具們 的筆記。

iamvince24 commented 11 months ago

打包程式碼必備 bundler:Webpack

為什麼需要 webpack?

webpack 簡介

Image.png

Basic Setup

First let's create a directory, initialize npm, install webpack locally, and install the webpack-cli (the tool used to run webpack on the command line):

mkdir webpack-demo
cd webpack-demo
npm init -y
npm install webpack webpack-cli --save-dev

Image.png

     - webpack幫我們在瀏覽器實作 `require` 這個功能。
  - Js beautify → website
     - 可以看到模組程式碼
const path = require('path');

module.exports = {
  mode: ''
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

繼續探索 webpack

Image.png

npm install --save-dev css-loader style-loader
     - 檔案內
import css from "file.css";
        - **webpack.config.js**
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
};