發表於 程式分享

WebPack基礎

1.Download nodejs

2.Init WebPack Project

mkdir webpackproj
cd webpackproj
code . # 進入VS Code
npm init # 建立專案
npm i webpack -D # -D = --save-dev

3.webpack說明

1.Entry: webpack.config.js 專案入口
2.Output: webpack.config.js 專案產出檔
3.Loaders: 加module,根據規則去建立好檔案
4.Plugins: 除了讀檔外
5.Mode: Develop / Production , Ex. file size (compress)

3-1.webpack.config.js

const path = require('path')

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

Command

npm run build # will install webpack-cli

4.啟動server執行

package.json

...
"scripts": {
    "build": "webpack",
    "dev": "webpack serve"
  },
...

Command

npm run dev # will install webpack-dev-server

localhost:8080/webpack-dev-server

5.建立index.html

# 先Key !,會產出預設的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</title>
</head>

<body>
    http://./main.bundle.js
</body>

</html>

6.dev server config

DevServer | webpack

7.use css file

webpack預設只看得懂html, 要認識css要裝loader, 裝css loader & style loader

npm install --save-dev css-loader
npm install --save-dev style-loader

webpack.config.js

...
module: {
        rules: [
            { 
                test: /\\.css$/i, 
                use: ['style-loader', 'css-loader'], 
            }
        ]
    }
...

index.css

div {
    width: 100px;
    height: 100px;
    background-color: yellow;
}

index.js

import './index.css'

console.log("hello");
console.log("hello");
console.log("hello");

index.html

<body>
    http://./main.bundle.js
    <div></div>
</body>

Command

npm run dev

8.bund hash for plugin

webpack.config.js

output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'main.**[hash]**.bundle.js'
    },

install html-webpack-plugin

npm install --save-dev html-webpack-plugin
npm install --save-dev mini-css-extract-plugin

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module: {
        rules: [
            { 
                test: /\\.css$/i, 
                use: [MiniCssExtractPlugin.loader, 'css-loader'], 
            }
        ]
    },

plugins: [new HtmlWebpackPlugin({
            template: './base.html'
        }),
        new MiniCssExtractPlugin({
            filename: 'main.[hash].css'
        })
    ],

base.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</title>
</head>

<body>
    http://./main.bundle.js
    <div id="num">0</div>
    <button id="btn">加1</button>
</body>

</html>

index.js

import './index.css'

const btn = document.getElementById('btn');
const num = document.getElementById('num');

btn.addEventListener('click', function() {
    const numVal = parseInt(num.innerHTML, 10);
    console.log('numVal: ' + numVal);
    num.innerHTML = numVal + 1;
})

9.babel: 讓所有瀏覽器都看得懂

Babel · The compiler for next generation JavaScript

將Babel加入webpack

# <https://babeljs.io/setup#installation>
npm install --save-dev babel-loader @babel/core
npm install @babel/preset-env --save-dev

webpack.config.js

{
  module: {
    rules: [
      {
        test: /\\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        }
      }
    ]
  }
}

babel.config.json

{
  "presets": ["@babel/preset-env"]
}

index.js

...
class Test {
    #a = 1
    b = 2
}

const tt = new Test()
// TODO del
console.log('tt.a', tt.a);
console.log('tt.b', tt.b);

10.sourcemap

webpack.config.js

...
devtool: 'source-map',
...

11.raw-loader: Ex.image, file…

index.css

div {
    width: 1000px;
    height: 1000px;
    background-color: yellow;
    background-image: url('./cat.png');
}

webpack.config.js

module: {
        rules: [
            { 
...
            {
                test: /\\.(png|svg|jpg|jpeg|gif)$/i,
                type: 'asset/resource',
            }
        ]
    },
發表於 程式分享

Python專案管理 in VSCode

1.Install

  1. VS Colde
  2. Anaconda 3
#設定環境變數Path
C:\\ProgramData\\Anaconda3\\
C:\\ProgramData\\Anaconda3\\Scripts
C:\\ProgramData\\Anaconda3\\Library\\bin
  1. Plugin: Python, vscode-icons

2.Virtual Environment

flowchart LR

G[(Cmd + Shift + P)]:::blue ---> |select| P[(Create Terminal)]:::blue
P ---> |input| B[(conda init)]---> |input| A[(Python Interpreter,conda activate myenv)]
A ---> |install| V1[(pip3 install virtualenv)]
V1 ---> V2[(virtualenv .venv)]

G[(Cmd + Shift + P)]:::blue ---> V3[(Python Select Interpreter)]
V3 ---> |select| P2[(Create Terminal)]:::blue
P2 --> I1[(pip3 install pandas plotly)]
I1 ---> I2[(pip3 freeze)]

G[(Cmd + Shift + P)]:::blue ---> |remove env| R[(rm -rf .venv)]

3.建立main.py

print('Hello, world!')

執行

python main.py

4.建立script1.py

def writeMessage(message):
    print('Message received:', message)
    return
    
writeMessage('This is called from script1.py')
if __name__ == '__main__':
    writeMessage('This is called from script1.py')

執行

python script1.py

5.調整main.py

import script1
script1.writeMessage('This is called from main.py')

執行

python main.py

6.Ref

VSCode 1/5: macOS 最佳 Python 編程工具?豐富的插件商店?Visual Studio Code 安裝攻略 – Python 編程.圖表