#前端# Vue+webpack手动搭建开发环境

需要安装的依赖

- vue-loader
- vue-template-compiler
npm install vue-loader vue-template-compiler --save-dev

webpack.config.js 配置

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

module.exports = {
    // 两种模式 development 和 production
    mode: 'development',
    // 入口文件
    entry: './app/main.js',
    // 输出文件名称和路径
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    // 监听文件变化
    watch: true,
    // 加载器: 用于识别更多的文件类型
    module: {
        // 规则
        rules: [
            {
                // 文件后缀, 一定是正在表达式
                test: /\.vue$/,
                // 加载器名称
                loader: 'vue-loader',
                // 包含的文件
                include: [path.resolve(__dirname, 'app')]
            }
        ]
    },
    // 插件
    plugins: [
        // 确保安装这个插件
        new VueLoaderPlugin()
    ],
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.esm.js'
        }
    }
}

app/main.js

import Vue from 'vue'
import App from './App.vue'
import bus from './utils/bus'

Vue.use(bus)

new Vue({
    el: '#app',
    render(h) {
        return h(App)
    }
})

app/App.vue

<template>
    <div>
        hello world!!!
    </div>
</template>

<script>
export default {
   
}
</script>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>
<script src="./dist/bundle.js"></script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章