乐趣区

学习webpack创建vue项目3-htmlwebpackplugin-webpackdevserver

安装html-webpack-plugin

npm install --save-dev html-webpack-plugin
webpack.config.js 中配置html-webpack-plugin


const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            // ... 其它规则
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            // 它会应用到普通的 `.css` 文件
            // 以及 `.vue` 文件中的 `<style>` 块
            {
                test: /\.css$/,
                use: [
                    'vue-style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.js$/,
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [
        // 请确保引入这个插件!new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'index.html',
            inject: true
        })
    ],
    resolve:{extensions:['.vue','.js'],
        alias:{'vue$': 'vue/dist/vue.esm.js'}
    }
}

执行 npm run start,生成dist 目录

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>Document</title>
    
</head>
<body>  
    <div id = "root">
    </div>
</body>

</html>

dist下目录的 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>Document</title>
    
</head>
<body>  
    <div id = "root">
    </div>
<script type="text/javascript" src="main.js"></script></body>

</html>

html-webpack-plugin生成了 index.html 文件,并将生成的 js 插入进去

安装webpack-dev-server

npm install --save-dev webpack-dev-server

webpack.config.json 文件中添加 devServer 字段配置webpack-dev-server


const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            // ... 其它规则
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            // 它会应用到普通的 `.css` 文件
            // 以及 `.vue` 文件中的 `<style>` 块
            {
                test: /\.css$/,
                use: [
                    'vue-style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.js$/,
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [
        // 请确保引入这个插件!new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'index.html',
            inject: true
        })
    ],
    resolve:{extensions:['.vue','.js'],
        alias:{'vue$': 'vue/dist/vue.esm.js'}
    },
    devServer:{
        open: true,
        host: 'localhost',
        port: 8081,
        inline: true
    }
}

package.json 中添加 dev 命令:

{
  "name": "webpacktest",
  "version": "1.0.0",
  "description": "","main":"index.js","scripts": {"dev":"webpack-dev-server","start":"webpack --debug --progress","test":"echo \"Error: no test specified\" && exit 1"},"keywords": [],"author":"",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "babel-loader": "^8.0.6",
    "babel-plugin-transform-runtime": "^6.23.0",
    "css-loader": "^3.2.0",
    "html-webpack-plugin": "^3.2.0",
    "vue-loader": "^15.7.1",
    "vue-style-loader": "^4.1.2",
    "vue-template-compiler": "^2.6.10",
    "webpack": "^4.39.1",
    "webpack-cli": "^3.3.6",
    "webpack-dev-server": "^3.8.0"
  },
  "dependencies": {"vue": "^2.6.10"}
}

运行npm run dev

修改文件内容,会看到重新编译

退出移动版