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. react功能实现-数组遍历渲染

    在react中如何将一个数组遍历,并且逐个渲染在页面上? 1.在jsx渲染中,如果这个变量是一个数组,则会展开这个数组的所有成员. var arr = [ <h1>Hello world! ...

  2. Hadoop-2.2.0在Unbuntu ADM64中需要重新编译Native Lib

    通过:cat /etc/issue 查看当前系统版本: Ubuntu 12.04.3 通过:uname -ar 查看更想起信息: Linux ubuntu-236 3.8.0-29-generic # ...

  3. pytorch实战(7)-----卷积神经网络

    一.卷积: 卷积在 pytorch 中有两种方式: [实际使用中基本都使用 nn.Conv2d() 这种形式] 一种是 torch.nn.Conv2d(), 一种是 torch.nn.function ...

  4. eas之编辑表单元格

    --指定表列行单元不可编辑 // 锁定表格.行.列.单元 table.getStyleAttributes().getProtection().setLocked(true); row.getStyl ...

  5. Shell 在手分析服务器日志不愁

    转自:https://wujunze.com/server_logs_analysis.jsp 自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有骇客 ...

  6. hdu 4612 双联通缩点+树形dp

    #pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...

  7. HDU - 1243 - 反恐训练营

    先上题目: 反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. POJ 2954

    PICK定理:格子上的多边形面积=边界上格子点数/2+内部点数-1. 利用叉积求出面积.再枚举边上的点数.然后按公式求出内部点数就可以了. 关于PICK:http://blog.csdn.net/i_ ...

  9. [Angular] Upgrading to RxJS v6

    This is just a learning blog post, check out the talk. 1. Custom pipeable operators: Custom pipeable ...

  10. 使用 AFNetworking的时候,怎样管理 session ID

    问: As the title implies, I am using AFNetworking in an iOS project in which the application talks to ...