Coder Social home page Coder Social logo

webpack's Introduction

Webpack

tags: vscode

安裝流程

//server 是存檔可以執行 npx webpack命令
npm install webpack webpack-cli webpack-dev-server --save-dev
npm i html-webpack-plugin -D
//完整複製過去
npm install copy-webpack-plugin --save-dev
//清除舊資料 只呈現最新資料
npm install --save-dev clean-webpack-plugin
//合併wepback.config檔
npm i webpack-merge -D
//babel
npm install -D babel-loader @babel/core @babel/preset-env
npm install core-js@3 --save

五種模式

  1. 入口 (entry)
  2. 出口 (output)
  3. loader
  4. 插件 (plugin)
  5. 模式 (mode)
    • development 開發模式
    • production 輸出產品
    • none

build

在 buid 資料夾下建立四種 webpack.config

  1. webpack.base.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "index.js",
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      inject:'body',
    }),
    new CopyPlugin({
      patterns: [
        { from: "static", to: "static" },
      ],
    }),
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', {
                targets: "defaults",
                "corejs": "3",
                "useBuiltIns": "entry",
              }]
            ]
          }
        }
      }
    ]
  }
};

  1. webpack.dev.config.js
module.exports={
    devtool: 'eval-cheap-module-source-map'
}
  1. webpack.pro.config.js
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports ={
    plugins: [
        new CleanWebpackPlugin(),
    ],
}
  1. webpack.config.js
const { merge } = require('webpack-merge')
const baseConfig = require("./webpack.base.config");
const devConfig = require("./webpack.dev.config");
const proConfig = require("./webpack.pro.config");


module.exports = (env, argv) => {
  const config = argv.mode === "development" ? devConfig : proConfig;
  return merge(baseConfig, config);
};

webpack's People

Contributors

q1113111 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.