• 玩转webpack(01):初识webpack


    一、webpack配置组成

    webpack默认配置文件:bebpack.config.js

    可以通过webpack --config指定配置文件


    moudle.exports = {

            entry: './src/index.js',        ————打包的入口文件

            output: './dist/main.js',        ————打包的输出

            mode: 'prouction',        ————环境

            moudle:{

                    rules:[        ————loader配置

                            {test:/\.txt$/,use:'raw-loader'}

                    ]

            },

            plugins:[

                    new HtmlwebpackPlugin({

                            template: './src/index.html'

                    })

            ]

    }

    零配置的webpack包含哪些配置

    • 指定默认的entry为:./src/index.js
    • 指定默认的output为:./dist/main.js

    二、安装Node.js和NPM

    1、安装nvm(nodejs的包管理器)

    • 通过 curl 安装:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
    • 通过 wget 安装:wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
    • 安装完成需要配置环境变量:source ~/.bash_profile 

    2、安装Node.js 和 npm

    nvm install v10.15.3

    nvm list   (查看计算机上安装的所有的nodejs的版本)

    三、环境搭建——安装webpack和webpack-cli

    1、新建空目录

    在新建的目录中打开终端

    执行:npm init -y

    2、安装webpack和webpack-cli

    npm i webpack webpack-cli --save-dev

    四、一个简单的例子 

    webpack.config.js

    1. 'use strict'
    2. const path = require('path');
    3. module.exports = {
    4. entry: './src/index.js',
    5. output: {
    6. path: path.join(__dirname, 'dist'),
    7. filename: 'bundle.js'
    8. },
    9. mode: 'production'
    10. }

    src/index.js

    1. import { helloWorld } from "./helloWorld";
    2. document.write(helloWorld());

    src/helloWorld.js

    1. export function helloWorld(){
    2. return 'hello webpack!'
    3. }

    代码编写完成后,在当前目录下运行:

    ./node_modules/.bin/webpack

    在dist目录下新建index.html ,并在浏览器打开此文件

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script src="./bundle.js">script>
    11. body>
    12. html>

    配置快捷打包方式:修改package.json

    1. "scripts": {
    2. "test": "echo \"Error: no test specified\" && exit 1",
    3. "build": "webpack"
    4. },

  • 相关阅读:
    iptables的50条常用命令
    【C++那些事儿】函数重载与C++中的“指针“——引用
    Liquibase使用SQL语句执行数据库变更
    缓存和数据库数据一致性解决方案
    Java中JavaBean对象和Map的互相转换
    巨控GRM230模块在分散式污水行业专用解决方案
    C++DAY44
    【FNN回归预测】基于matlab粒子群优化前馈神经网络婚姻和离婚数据回归预测【含Matlab源码 2069期】
    package.json文件的一些知识点,实践总结
    spring 中refresh()方法中prepareRefresh()方法的功能
  • 原文地址:https://blog.csdn.net/m0_47135993/article/details/128075800