码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-05-20 22:41:11      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:get   编译过程   输入   通过   oss   bsp   ecif   str   jpeg   

首先输入命令来安装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

标签:get   编译过程   输入   通过   oss   bsp   ecif   str   jpeg   

原文地址:https://www.cnblogs.com/init-007/p/10896947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!