WebPack2设置我的Vue开辟环境

起首已全局装置了node/vue/webpack;

新建文件夹demo4并初始化

cd demo4
npm init -y

这是页面会天生一个package.json文件。

装置webpack及相干插件

npm install webpack webpack-dev-server vue-loader vue-html-loader css-loader vue-style-loader vue-hot-reload-api babel-loader babel-core babel-plugin-transform-runtime babel-preset-es2015 babel-runtime@5 --save-dev
npm install html-webpack-plugin --save-dev
npm install vue --save

webpack-dev-server: webpack-dev-server是一款小型的Node.js Express服务器,我们运用它重要是为了完成代码的热重载,详细运用方法可拜见webpack-dev-server运用方法,看完还不会的来找我~
vue-loader/vue-html-loader/css-loader/vue-style-loader...: webpack中loader的作用不多讲,用法看文档
vue-hot-reload-api: vue-hot-reload-api望文生义,亦为完成vue热重载
此时package.json中的devDependenciesdependencies应该是如许的:

"devDependencies": {
    "babel-core": "^6.23.1",
    "babel-loader": "^6.4.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-runtime": "^5.8.38",
    "css-loader": "^0.26.4",
    "vue-hot-reload-api": "^2.0.11",
    "vue-html-loader": "^1.2.4",
    "vue-loader": "^11.1.4",
    "vue-style-loader": "^2.0.3",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.1"
  },
  "dependencies": {
    "vue": "^2.2.2"
  }

竖立文件目次

demo4
    |__dist
    |   |__js
    |__src
    |   |__index.html
    |   |__js
    |   |    |__index.js
    |   |__components
    |      |__hello.vue
    |__node_modules
    |__package.json
    |__webpack.config.js

dist: 寄存天生的文件
src: 寄存编辑的文件模板等
components: 寄存components组件

  • src/index.html

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Vue</title>
</head>
<body>
<div id="test">
    <Hello></Hello>
</div>
</body>
</html>
  • src/js/index.js

import Vue from 'vue';
import Hello from "../components/Hello.vue";

new Vue({
    el: "#test",
    template: '<Hello/>',
    components: { Hello }
})
  • src/components/Hello.vue

<template>
    <div>{{msg}}</div>
</template>

<script>
    export default {
        data () {
            return {
                msg: 'Hello World!'
            }
        }
    }
</script>

<style>
    html {
        background: green;
        color: #fff;
        font-size: 20px;
    }
</style>
  • webpack.config.js

var path = require('path');
var htmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: path.resolve(__dirname, './src/js/index.js'),
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: './js/[name].js'
    },
    module: {
        loaders: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query:{
                  presets: 'es2015'  
                },
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new htmlWebpackPlugin({
            template: './src/index.html',
            hash: true
        })
    ]
}

敕令行运转webpack敕令, 此时在dist文件目次下离别天生了js/index.js和index.html,在页面中翻开index.html发明页面有报错: [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build. 这是由于此时运用的是vue.runtime.common.js,这里能够浏览以下官方文档中的自力构建VS运转时构建简朴明白就是自力构建能够本身将字符串模板(template)编译为衬着函数(render),然后再运转时再挪用编译好的衬着函数,而运转时构建是在Vue2最先后,为了完成在服务端衬着,不依赖与浏览器端的DOM接口,而不允许运用template模板,因而运转时构建比自力构建要小,然则不能运用template模板,而官方文档中也有申明。npm包中导出的默许是运转时构建。假如愿望运用自力构建,能够增加以下代码

webpack中
resolve: {
  alias: {
    'vue$': 'vue/dist/vue.common.js'
  }
}

这句话是增加到webpack.config.js中的,固然,我们也能够翻开node_modules/vue/package.json文件,将个中的main指向”dist/vue.runtime.common.js“改成'vue/dist/vue.common.js'

此时再从新运转webpack敕令,能够还会报错: Cannot find module 'vue-template-compiler' ,此时在敕令行中运转npm install vue-template-compiler即可。
在运转webpack敕令,在浏览器中翻开dist/index.html文件就能够看到代码圆满运转了。我们只须要在src/下修正我们的Hello.vue或者是index.js以及index.html文件,然后运转webpack然后革新页面即可看到代码的修改结果。
固然,我们期待的是只修正代码,不必从新运转webpack敕令,以至不须要革新浏览器即看到代码的修改结果,这时候须要新的插件来设置完成vue的热重载。

    原文作者:前端老李
    原文地址: https://segmentfault.com/a/1190000008678236
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞