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 webpack = require('webpack')


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

 最后输入命令

npm run dev

打开浏览器输入 localhost:8000

实现热加载功能:修改数据,只是重新渲染修改的组件 修改webpack.config.js文件:

const path = require('path')

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

const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
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(),
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: isDev ? '"development"' : '"production"'
        }
      }),
      new HTMLPlugin()
  ]
}

if(isDev) {
  // 帮助调试代码
  config.devtool = '#cheap-module-eval-source-map'
  config.devServer = {
    port: 8000,
    host: '0.0.0.0',
    overlay: {
      errors: true
    },
    // 热加载功能:只渲染修改的组件,不会刷新页面
    hot: true
    //open: true 改配置后就自动打开浏览器
  }
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
}

module.exports = config
View Code

 需要重新输入命令

npm run dev

刷新页面后可以看效果

posted @ 2019-05-20 22:21  study_php_java_C++  阅读(179)  评论(0编辑  收藏  举报