zoukankan      html  css  js  c++  java
  • webpack使用

    一、安装

      webpack安装需要nodejs环境支持,推荐本地开发环境安装

    npm i webpack webpack-cli -D

    二、初始化项目

    npm init

      生成package.json文件并配置

    "scripts": {
        "build": "webpack"
    }

    三、创建webpack.config.js文件并配置

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

      运行npm run build命令即可使用webpack打包

     四、概念

      webpack.config.js文件的主要配置项

      1、入口(entry)

    module.exports = {
        entry: './src/index.js'
    };

      2、出口(output)

    const path = require('path');
    module.exports
    = { output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' } };

      3、loader

    module.exports = {
      module: {
        rules: [{
          test:
    /\.txt$/, // 正则表达式用以匹配文件
          use: 'raw-loader'
        }]
      }
    };

      4、插件(plugins)

    const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
    module.exports = {   plugins: [     new HtmlWebpackPlugin({template: './index.html'})   ] };

      5、模式

    module.exports = {
      mode: 'production' // production:生产环境下使用,代码量少;development:开发环境下使用,代码量多
    };

    五、自动化导入模块

    //参数1:目录路径
    //参数2:是否递归目录
    //参数3:文件匹配正则表达式
    let reqAll = require.context('./module', true, /\.js$/)
    reqAll.keys().map(li => {
        console.log(reqAll(li))
    })
  • 相关阅读:
    IO以及file的一些基本方法
    异常处理和Throwable中的几个方法
    Map的嵌套
    Collections
    Map接口
    Set接口
    React生命周期执行顺序详解
    当面试官问你GET和POST区别的时候,请这么回答.......
    webpack.config.js配置遇到Error: Cannot find module '@babel/core'&&Cannot find module '@babel/plugin-transform-react-jsx' 问题
    前端简单实现校招笔试'作弊监听'功能
  • 原文地址:https://www.cnblogs.com/linding/p/12368987.html
Copyright © 2011-2022 走看看