webpack learn1-webpack-dev-server的配置和使用3

Posted init-007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack learn1-webpack-dev-server的配置和使用3相关的知识,希望对你有一定的参考价值。

首先输入命令来安装webpack-dev-server

npm i webpack-dev-server

在package.json文件中添加代码:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config webpack.config.js --mode development",
    "dev": "webpack-dev-server --config webpack.config.js"
  },

输入命令:

npm i cross-env

修改package.json文件其中的"scripts"

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "cross-env NODE_ENV=production webpack --config webpack.config.js",
    "dev": "cross-env NODE_ENV=development webpack-dev-server --config webpack.config.js"
  },

在webpack.config.js中添加target: ‘web‘ 和最下面添加判断条件

技术图片
const path = require(path)

const VueLoaderPlugin = require(vue-loader/lib/plugin)

const isDev = process.env.NODE_ENV === development

const config = {
  target: web,
  entry: path.join(__dirname,src/index.js),
  output: {
    filename: bundle.js,
    path: path.join(__dirname,dist)
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: vue-loader
      },{
        test:/\.css$/,
        use: [
          style-loader,
          css-loader
        ]
      },{
        test: /\.styl/,
        use: [
          style-loader,
          css-loader,
          stylus-loader
        ]
      },{
        test:/\.(jpg|svg|jpeg|png|gif)$/,
        use: [
          {
            loader: url-loader,
            options: {
              limit: 1024,
              name: [name]-aa.[ext]
            }
          }
        ]
      }
    ]
  },
  plugins:[
      new VueLoaderPlugin()
  ]
}

if(isDev) {
  config.devServer = {
    port: 8000,
    host: 0.0.0.0,
    overlay: {
      errors: true
    }
  }
}

module.exports = config
View Code

其中设置host=‘0.0.0.0‘好处是:既可以通过127.0.0.1访问,也可以通过ip进行访问,这样在别的电脑就可以访问本机.

overlay: true是为了,如果vue有错误可以显示在网页上,可以看到webpack编译过程中出现的错误

输入命令安装html-webpack-plugin

npm i html-webpack-plugin

再次修改webpack.config.js文件:

const path = require(‘path‘)

const VueLoaderPlugin = require(‘vue-loader/lib/plugin‘)

const isDev = process.env.NODE_ENV === ‘development‘

const HTMLPlugin =require(‘html-webpack-plugin‘)

const config = {
  target: ‘web‘,
  entry: path.join(__dirname,‘src/index.js‘),
  output: {
    filename: ‘bundle.js‘,
    path: path.join(__dirname,‘dist‘)
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: ‘vue-loader‘
      },{
        test:/\.css$/,
        use: [
          ‘style-loader‘,
          ‘css-loader‘
        ]
      },{
        test: /\.styl/,
        use: [
          ‘style-loader‘,
          ‘css-loader‘,
          ‘stylus-loader‘
        ]
      },{
        test:/\.(jpg|svg|jpeg|png|gif)$/,
        use: [
          {
            loader: ‘url-loader‘,
            options: {
              limit: 1024,
              name: ‘[name]-aa.[ext]‘
            }
          }
        ]
      }
    ]
  },
  plugins:[
      new VueLoaderPlugin(),
      new webpack.DefinePlugin({
        ‘process.env‘: {
          NODE_ENV: isDev ? ‘"development"‘ : ‘"production"‘
        }
      }),
      new HTMLPlugin()
  ]
}

if(isDev) {
  config.devServer = {
    port: 8000,
    host: ‘0.0.0.0‘,
    overlay: {
      errors: true
    }
  }
}

module.exports = config

 

以上是关于webpack learn1-webpack-dev-server的配置和使用3的主要内容,如果未能解决你的问题,请参考以下文章

【webpack】--config 的使用

# Webpack 学习Webpack 搭建 Vue项目

# Webpack 学习Webpack 搭建 Vue项目

Webpack教程: Webpack安装

初识webpack与webpack环境搭建

初识webpack与webpack环境搭建