1.5.1、安装依赖

  1. npm i vue vue-loader vue-template-compiler -S
  1. const {VueLoaderPlugin} = require('vue-loader');
  2. const config ={
  3. module:{
  4. rules:[
  5. {
  6. test:/\.vue$/,
  7. loader:'vue-loader'
  8. }
  9. ]
  10. },
  11. plugins:[
  12. new VueLoaderPlugin()
  13. ]
  14. }
  15. module.exports = config

1.5.2、配置index.js

  1. import './assets/styles/base.css'
  2. import App from './App.vue'
  3. import Vue from 'vue';
  4. const root = document.createElement("div");
  5. document.body.appendChild(root);
  6. new Vue({
  7. render:h=>h(App)
  8. }).$mount(root)

1.5.3、完整的配置

  1. const webpack = require('webpack');
  2. const path = require('path')
  3. const htmlWebpackPlugin = require('html-webpack-plugin');
  4. const {VueLoaderPlugin} = require('vue-loader');
  5. const {
  6. CleanWebpackPlugin
  7. } = require('clean-webpack-plugin');
  8. const isDev = process.env.NODE_ENV === 'development'
  9. const config = {
  10. target: "web",
  11. entry: path.join(__dirname, 'src/index.js'),
  12. output: {
  13. filename: '[hash].bundle.js',
  14. path: path.join(__dirname, 'dist')
  15. },
  16. module: {
  17. rules: [{
  18. test: /\.css$/,
  19. use: [
  20. 'style-loader',
  21. 'css-loader'
  22. ]
  23. },
  24. {
  25. test: /\.scss$/,
  26. use: [
  27. 'style-loader',
  28. 'css-loader',
  29. 'sass-loader'
  30. ]
  31. },
  32. {
  33. test: /\.(gif|jpg|jpeg|png|svg)$/,
  34. use: [{
  35. loader: 'url-loader',
  36. options: {
  37. limit: 1024,
  38. name: '[name]-[hash].[ext]'
  39. }
  40. }]
  41. },
  42. {
  43. test:/\.vue$/,
  44. loader:'vue-loader'
  45. }
  46. ]
  47. },
  48. plugins: [
  49. new VueLoaderPlugin(),
  50. new CleanWebpackPlugin(),
  51. new htmlWebpackPlugin({
  52. template: path.join(__dirname, 'public/index.html')
  53. }),
  54. new webpack.DefinePlugin({
  55. 'process.env': {
  56. NODE_ENV: isDev ? '"development"' : '"production"'
  57. }
  58. })
  59. ],
  60. mode: "development"
  61. }
  62. if (isDev) {
  63. config.devtool = "#cheap-module-eval-source-map"
  64. config.devServer = {
  65. port: 8080,
  66. host: 'localhost',
  67. overlay: {
  68. errors: true
  69. },
  70. hot: true
  71. }
  72. config.plugins.push(
  73. new webpack.HotModuleReplacementPlugin(),
  74. new webpack.NoEmitOnErrorsPlugin()
  75. )
  76. }
  77. module.exports = config
  78. if (module.hot) {
  79. // 实现热更新
  80. module.hot.accept();
  81. }