webpack踩坑记录

0

webpack入门级配置(最新)

目前最的正式版本是:4.31.0,推荐使用本地安装,webpack官网-指南

1.初始化

  • 确认node 的版本号 node -v
  • 新建一个项目文件夹 mkdir demo
  • 初始化 npm init -y
  • npm 安装 webpack webpack-cli --save-dev
  • 在项目文件夹下新建一个src文件夹,src文件夹包含index.js,index.html
  • 新建webpack配置文件 webpack.config.js
const path = require('path');
module.exports = {
    entry:'./src/index.js'.
    ouyput:{
        path: path.resolve(__dirname,'dist'),
        filename: 'bundle.js',
    }
}

安装 webpack-dev-server

  • 本地安装webpack-dev-server npm i webpack-dev-server -D
  • 找到package.json文件,搜索scripts,添加 "dev": "webpack-dev-server --open --port 3000 --contentBase src --hot"代码
  • 此时在命令行运行npm run dev,即可打包文件

安装html-webpack-plugin

  • 命令行运行npm install --save-dev html-webpack-plugin
  • 在webpack配置文件添加配置信息
  • const HtmlWebpackPlugin = require('html-webpack-plugin');
  • plugins:[new HtmlWebpackPlugin({template:'./dist/index.html'})]

加载css

  • 命令行运行npm install --save-dev style-loader css-loader
  • 配置文件配置module:{reules:[{test:/\.css$/,use:['style-loader','css-loader']}]}

安装babel-loader

  • 命令行运行npm install -D babel-loader @babel/core @babel/preset-env webpack
  • 配置文件配置
module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env']
        }
      }
    }
  ]
}
  • 运行npm install -D @babel/plugin-transform-runtime
  • 配置文件
use: {
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env'],
        plugins: ['@babel/plugin-transform-runtime']
      }
    }

使用vue

  • 命令行运行 npm i vue -D
  • 配置文件配置
# const VueLoaderPlugin = require('vue-loader/lib/plugin');


# resolve:{
        alias:{
            'vue$':"vue/dist/vue.esm.js"
        }
    }
    

安装vue-loader

  • 命令行npm install -D vue-loader vue-template-compiler
  • 配置文件
+ const VueLoaderPlugin = require('vue-loader/lib/plugin');

-  rules: [
      // ... 其它规则
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
    
- plugins: [
    // 引入这个插件!
    new VueLoaderPlugin()
  ]

附上源代码

webpack.config.js


const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname,'dist'),
        filename: 'bundle.js',
    },
    plugins:[
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            template:'./src/index.html'
        }),
        
    ],
    module:{
        rules:[
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test:/\.(png|svg|ipg|gif)$/,
                use:['file-loader']
            },
            {
                test:/\.vue$/,
                loader:'vue-loader'
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['@babel/preset-env'],
                    plugins: ['@babel/plugin-transform-runtime']
                  }
                }
            },
            
        ]
    },
    resolve:{
        alias:{
            'vue$':"vue/dist/vue.esm.js"
        }
    }
}

package.json

{
  "name": "mint-demo",
  "version": "1.0.0",
  "description": "\"",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --open --port 3000 --contentBase src --hot"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.4",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/preset-env": "^7.4.4",
    "babel-loader": "^8.0.6",
    "css-loader": "^2.1.1",
    "file-loader": "^3.0.1",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "vue-loader": "^15.7.0",
    "vue-template-compiler": "^2.6.10",
    "webpack": "^4.31.0",
    "webpack-cli": "^3.3.2",
    "webpack-dev-server": "^3.3.1"
  },
  "dependencies": {
    "vue": "^2.6.10"
  }
}

index.js

import Vue from 'vue';
import App from './App.vue';
console.log('444');
var app = new Vue({
    el:'#app',
    data:{
        msg:'ccc'
    },
    render: c => c(App)
    
})

console.log('2323');

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue</title>
</head>
<body>
    <div id="app">
        {{msg}}
    </div>
</body>
</html>

App.vue

<template>
    <div>
        这是app.vue
    </div>
</template>
<script>
export default {
}
</script>

<style scoped>

</style>

欢迎指正哦!
参照:
webpack官网
vue官网

你可能感兴趣的

载入中...