Webpack的安装和使用

Webpack的安装和使用

打开cmd,使用指令npm i webpack webpack-cli -g进行下载即可。
在这里插入图片描述
在Webpack中,万物皆模块,js、css、图片等等在webpack中,我们都可以看成是一个模块。
现在我们来简单地使用我们的webpack。
写代码前,我们先看一下最终的目录结构吧:
在这里插入图片描述
首先我们写一个要被引用的hello.js文件:

exports.sayHello = function(){
	document.write("<div>Hello Webpack!</div>");
}

然后我们写我们的main.js

// 导入hello文件
const hello = require("./hello");
//调用hello.js文件的sayHello()方法
hello.sayHello();

再写我们webpack的配置文件
webpack.config.js

module.exports = {
	// 入口文件
	entry: "./src/main.js",
	//打包
	output:{
		filename: "./js/bundle.js"
	}
}

然后我们到cmd里面使用webpack打包我们的项目。
在这里插入图片描述
打包成功后,我们的目录下会多出一个dist目录,下面js/bundle.js就是我们在配置文件里面配置的最终打包的文件的路径。
在这里插入图片描述
之后我们就可以写我们的html页面来引用刚才生成的打包文件了。
index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script src="./dist/js/bundle.js"></script>
	</body> 
</html>

结果:
在这里插入图片描述
如果我们要让他实时打包更新的话,我们在打包的时候可以这么写:webpack --watch
在这里插入图片描述
此时,最底下的光标会一直闪烁,监听我们是否有修改文件并进行实时打包。
此时我们修改我们的hello.js文件如下:

exports.sayHello = function(){
	document.write("<div>Hello Webpack!</div>");
	document.write("<div>我被改过了哦</div>");
}

然后刷新我们的页面,可以看到他自己更新了打包的文件。
在这里插入图片描述
复杂一点的我们可以看一下我们新建Vue-cli项目的时候的webpack.back.config.js:

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值