zoukankan      html  css  js  c++  java
  • webpack 项目实战

    步骤一:

    npm init
    

    步骤二:

    npm install -D clean-webpack-plugin css-loader extract-text-webpack-plugin html-webpack-plugin mini-css-extract-plugin style-loader webpack webpack-cli webpack-dev-server
    

    步骤三:

    npm install -S jquery
    

    步骤四:

    创建 webpack.config.js

    // 直接path
    const path = require('path');
    const ExtractTextPlugin = require('extract-text-webpack-plugin'); //
    const CleanWebpackPlugin = require('clean-webpack-plugin'); // 清除旧文件
    var HtmlWebpackPlugin = require('html-webpack-plugin'); // 页面热加载
    var webpack = require('webpack');
    
    module.exports = {
      mode: 'development',
      entry: './main.js', // 入口文件
      output: { // 出口
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].[hash].js' // 名称
      },
      devServer: { // webpack 本地服务
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 9000
      },
      module: {
        rules: [
          {
            test: /.css$/,
            use: ExtractTextPlugin.extract({
              fallback: "style-loader",
              use: "css-loader"
            })
          }
        ]
      },
      plugins: [ // 插件集
        new ExtractTextPlugin("[name].[hash].css"), // 输出css版本
        new CleanWebpackPlugin(['dist']), // 清除dist文件夹下的内容
        new HtmlWebpackPlugin({ // 自动生成html文件
          template: 'index.html'
        }),
        new webpack.ProvidePlugin({ // 引入第三方插件(自动加载模块)
          $: 'jquery',
          jQuery: 'jquery'
        }),
        new webpack.DefinePlugin({ // 定义全局变量
          'env': JSON.stringify('www://aaa/com')
        })
      ],
      optimization: {
        splitChunks: {
          filename: 'common.js',
          chunks: 'all'
        }
      }
    };

    步骤五:

    package.json

    {
      "name": "webpack_test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "webpack-dev-server --inline",
        "build": "webpack"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "clean-webpack-plugin": "^0.1.19",
        "css-loader": "^0.28.11",
        "extract-text-webpack-plugin": "^3.0.2",
        "html-webpack-plugin": "^3.2.0",
        "mini-css-extract-plugin": "^0.4.0",
        "style-loader": "^0.21.0",
        "webpack": "^4.12.0",
        "webpack-cli": "^3.0.8",
        "webpack-dev-server": "^3.1.4"
      },
      "dependencies": {
        "jquery": "^3.3.1"
      }
    }

    步骤六:

    项目目录

    store/age.js

    const age = 18;
    
    module.exports = age;

    store/name.js

    const name = '小明';
    
    module.exports = name;

    main.js

    // 引入 age.js
    const age = require('./store/age.js');
    // 引入 name.js
    const name = require('./store/name.js');
    
    import login from './style/common.css';
    
    console.log(`${name}的年龄是${age}`);

    index.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>React App</title>
      </head>
      <body>
        <div>React App</div>
      </body>
    </html>

    .

  • 相关阅读:
    MySQL 慢日志没有自动创建新的日志文件
    Springboot为什么加载不上application.yml的配置文件
    android studio set proxy
    c++ win32 遍历进程列表
    React Prompt组件 阻止用户离开页面
    JS 浏览器上生成 UUID API
    部署 Nestjs 最佳实践
    Nginx 部署 单页面应用 + nodejs api 应用 最佳实践
    React JS: 如何使用 RxService 管理状态
    umijs 开发优化和生产优化
  • 原文地址:https://www.cnblogs.com/crazycode2/p/9206321.html
Copyright © 2011-2022 走看看