vue官方已经写好一个vue-webpack模板vue_cli,原本自己写一个,发现官方写得已经够好了,自己写显得有点多余,但为了让大家熟悉webpack,决定还是一步一步从0开始写,但源文件就直接拷贝官方的

准备工作

  1. 新建文件夹D:\03www2018\study\vue2017,下面根目录指的就是这个目录
  2. 生成package.json, 根目录>npm init
  3. 安装webpack和webpack开发服务器, 根目录>cnpm i -D webpack webpack-dev-server
  4. 安装vue、vuex、vue-router,根目录>cnpm i -S vue vuex vue-router
  5. 下载vue_cli的webpack模板中src这个源文件夹根目录\src

安装vue
D:\03www2018\study\webpack2018>cnpm i vue -S

如果有下面的报错

[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.

(found in <Root>)

解释: 运行时构建不包含模板编译器,因此不支持 template 选项,只能用 render 选项,但即使使用运行时构建,在单文件组件中也依然可以写模板,因为单文件组件的模板会在构建时预编译为 render 函数
修改 D:/03www2018/study/webpack2018/build/webpackfile.js

    resolve: {
alias: {
'vue': 'vue/dist/vue.js'
}
},

最简单的例子

D:\03www2018\study\webpack2018\today\wang\home.js
import Vue from 'vue';
const app = new Vue({
template: '<div>hello wolr</div>'
}).$mount('#main')

导入第一个vue组件

// D:\03www2018\study\webpack2018\today\wang\home.js
import App from "./app.vue"
import Vue from 'vue';
const app = new Vue({
template: '<App />',
components:{App}
}).$mount('#main')
// D:\03www2018\study\webpack2018\today\wang\App.vue
<template>
<div>上午好</div>
</template>

报错

Uncaught Error: Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.

安装并配置 vue-loader

官方文档 https://vue-loader.vuejs.org/...

D:\03www2018\study\webpack2018>cnpm i vue-loader -D

提示要安装css-loader和vue-template-compiler,现在将这两个也一起安装

D:\03www2018\study\webpack2018>cnpm i css-loader vue-template-compiler -D

现在就可以正常显示vue组件

处理css文件

// D:\03www2018\study\webpack2018\today\wang\app.css
body{
color:#09f;
}

处理单独的css文件
没有装css-loader会报错

ERROR in ./today/wang/app.css
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.

安装和配置

官方文档: https://webpack.js.org/loader...

D:\03www2018\study\webpack2018>cnpm i -D css-loader
{
test: /\.css$/,
loader: 'css-loader',
},

对上面导入的css一般有两种处理,一是使用style-loader将css嵌入到html文件的style标签中,一种是单独存在一个文件中

style-loader

官方文档: https://webpack.js.org/loader...

D:\03www2018\study\webpack2018>cnpm i style-loader -D
{
test: /\.css$/,
loader: 'style-loader!css-loader',
},

多个loader是从右到左执行,多个loader之间用!连接,上面多个loader也可以写在数组的形式

{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
}

这种写法是,从下到上执行,先执行css-loader再执行style-loader

将css文件单独打包到一个文件
这要使用到ExtractTextWebpackPlugin插件

处理less/sass等文件

这要用到less-loader或sass-loader,同时得安装less或sass,如果没安装会报错

 [Vue warn]: Error in beforeCreate hook: "Error: Cannot find module "!!vue-loader/node_modules/vue-style-loader!css-loader!../../node_modules/_vue-loader@13.6.0@vue-loader/lib/style-compiler/index?{"vue":true,"id":"data-v-381730fa","scoped":false,"hasInlineConfig":false}!less-loader!../../node_modules/_vue-loader@13.6.0@vue-loader/lib/selector?type=styles&index=0&bustCache!./app.vue""

ave组件的内容

<template>
<div class='morning'>上午好</div>
</template>
<style lang='less'>
@color:#f96;
.morning{
color:@color
}
</style>

安装less和less-loader

D:\03www2018\study\webpack2018>cnpm i -D less less-loader

配置

{
test: /\.css$/,
use: ExtractTextPlugin.extract({
// fallback: "style-loader", //备用,如果提取不成功时,会使用style-loader来处理css
use: "css-loader"
})
/*use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]*/
}, {
test: /\.less$/,
use: [
{
loader: "style-loader" // creates style nodes from JS strings
},
{
loader: "css-loader" // translates CSS into CommonJS
},
{
loader: "less-loader" // compiles Less to CSS
}
]
},

上面这个例子,只有导入的css文件单单独存在一个文件中,vue组件中的less归到了style中了,

说明:在vue组件<style>中,如果lang="less",在vue-loader中默认配置好了less,无须另外配置
说明: 上面例子是配置的是单独的less文件,不适合uve中的less
说明:如何将vue中的less也放到单独的css文件中呢? 参考https://vue-loader.vuejs.org/...

{
test: /\.vue$/,
loader: 'vue-loader',
options: {
extractCSS: true
}
}

但上面有个缺点,会覆盖之前css中的配置中生成style.css文件,如何解决呢?

const path = require('path');
const webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin"); const extractCSS = new ExtractTextPlugin('css/[name]-one.css');
const extractLESS = new ExtractTextPlugin('css/[name]-two.css'); module.exports={
plugins: [
extractCSS,
extractLESS
], module: {
rules: [ {
test: /\.css$/,
use: extractCSS.extract({
// fallback: "style-loader", //备用,如果提取不成功时,会使用style-loader来处理css
use: "css-loader"
}) }, {
test: /\.vue$/,
loader: 'vue-loader',
options: {
// extractCSS: true
extractCSS: function(){
return extractLESS
}
}
}
]
},
}

导入图片

// D:\03www2018\study\webpack2018\today\wang\App.vue中增加图片
<template>
<div class='morning'>
<img src="../images/logo.jpg" />
<img src="../images/a.jpg" />
上午好
</div>
</template>
<style lang='less'>
@color:#f96;
.morning{
color:@color
}
</style>

如果安装并配置好了url-loader,图片会生成data:image格式
发现生成<img src="data:image/jpeg;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICIzNDk0NWM0MDMyMWQyMDk3YTY5Zjg2MGZkNWQ1M2FlZC5qcGciOw==">,但是不显示出图片,url-loader不配置会显示图片,显示如下<img src="34945c40321d2097a69f860fd5d53aed.jpg">

安装
D:03www2018studywebpack2018>cnpm i url-loader -D

安装
D:03www2018studywebpack2018>cnpm i file-loader -D

(转)webpack从零开始第6课:在Vue开发中使用webpack的更多相关文章

  1. webpack从零开始第1课:安装webpack和webpack-dev-server

    原文地址:https://segmentfault.com/a/1190000012536871 webpack目录 第1课: 安装webpack和webpack-dev-server 第2课: 配置 ...

  2. vue开发中的"骚操作"

    前言 在与同事协作开发的过程中,见识到了不少"骚操作".因为之前都没用过,所以我愿称之为"高级技巧"! Vue.extend 在交互过程中,有个需求就是点击图标 ...

  3. Vue开发中的中央事件总线

    在Vue开发中会遇到大量的组件之间共享数据的情形,针对不同的情形,Vue有相对应的解决方案.比如,父组件向子组件传值可以使用props,复杂项目中不同模块之间传值可以使用Vuex.但是,对于一些简单的 ...

  4. vue 项目中 自定义 webpack 的 配置文件(webpack.config.babel.js)

    webpack.config.babel.js,这样命名是想让webpack在编译的时候自动识别es6的语法,现在貌似不需要这样命名了,之前用webpack1.x的时候貌似是需要的 let path ...

  5. 在vue项目中配置webpack

    首先我们来看一下使用Vue-cli2与Vue-cli2之后的版本(这里以Vue-cli4版本为例)创建项目目录结构的不同: Vue-cli2(左图)与Vue-cli4(右图)创建项目的目录 从上图可以 ...

  6. vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives

    在使用VScode编辑器vue开发过程中,v-for在Eslint的规则检查下出现报错:Elements in iteration expect to have 'v-bind:key' direct ...

  7. 前端vue开发中的跨域问题解决,以及nginx上线部署。(vue devServer与nginx)

    前言 最近做的一个项目中使用了vue+springboot的前后端分离模式 在前端开发的的时候,使用vue cli3的devServer来解决跨域问题 上线部署则是用的nginx反向代理至后台服务所开 ...

  8. Vue-cli+Vue.js2.0+Vuex2.0+vue-router+es6+webpack+node.js脚手架搭建和Vue开发实战

    Vue.js是一个构建数据驱动的web界面的渐进式框架.在写这边文章时Vue版本分为1.0++和2.0++,这个是基于Vue2.0的项目. Vue-cli是构建单页应用的脚手架,这个可是官方的. Vu ...

  9. Vue项目中使用webpack配置了别名,引入的时候报错

    chainWebpack(config) { config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/as ...

随机推荐

  1. 腾讯模板引擎template

    template.js是一款JavaScript模板引擎,用来渲染页面的. 原理:提前将Html代码放进编写模板  script id="tpl" type="text/ ...

  2. 【转载】java文件路径问题及getResource和getClassLoader().getResource的区别

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012572955/article/details/52880520我们经常在java的io操作中读 ...

  3. PAT_A1144#The Missing Number

    Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...

  4. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

  5. [转]理解和配置 Linux 下的 OOM Killer

    最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通常是因为某时刻应用程序大量请求内存导致系统 ...

  6. C#第十三节课

    冒泡排序 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...

  7. vue,基础整理,夯实基础,为进阶打基础

    把基础部分,再次系统的了解一遍,整理成文档.

  8. windows环境下用pip安装pyautogui遇到的几个问题

    1.不能直接使用win+r运行cmd并使用pip,必须点击开始->windows系统->命令提示符,右键->以管理员身份运行 2.运行pip install pyautogui后提示 ...

  9. POJ 3762 The Bonus Salary!

    The Bonus Salary! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Origi ...

  10. 洛谷 P2805 BZOJ 1565 植物大战僵尸

    题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻.该款游戏包含多 ...