详解vue-loader在项⽬中是如何配置的
什么是vue-loader
这是我⼊职第三天的故事,在写这篇⽂章之前,先来看看咱们今天要讲的主⾓——vue-loader,你对它了解多少?
这是我今天的回答,确实,vue-loader是webpack的⼀个loader,⽤于处理.vue⽂件。
.vue ⽂件是⼀个⾃定义的⽂件类型,⽤类 HTML 语法描述⼀个 Vue 组件。每个 .vue ⽂件包含三种类型的顶级语⾔块
<template>、<script>和 <style>。
vue-loader 会解析⽂件,提取每个语⾔块,如有必要会通过其它 loader 处理(⽐如<script>默认⽤babel-loader处理,<style>默认⽤style-loader处理),最后将他们组装成⼀个 CommonJS 模块,ports 出⼀个 Vue.js 组件对象。
vue-loader ⽀持使⽤⾮默认语⾔,⽐如 CSS 预处理器,预编译的 HTML 模版语⾔,通过设置语⾔块的 lang 属性。例如,你可以像下⾯这样使⽤ Sass 语法编写样式:
<style lang="sass">
/* write Sass! */
</style>
知道了什么是vue-loader之后,我们先来创建项⽬。为了快速地聊聊vue-loader,我在这⾥推荐⽤脚⼿架⼯具 @vue/cli 来创建⼀个使⽤ vue-loader 的项⽬:
npm install -g @vue/cli
vue create hello-world
cd hello-world
npm run rve
这个过程我可以等等你们,becau this might take
当你在浏览器⾥输⼊localhost:8080之后,浏览器会友善地渲染出⼀个Welcome to Your Vue.js App的欢迎页⾯。
紧接着,我们需要打开你擅长的编辑器,这⾥我选⽤的是VSCode,顺⼿将项⽬导⼊进来,你会看到最原始的⼀个项⽬⼯程⽬录,⾥⾯只有⼀些简单的项⽬构成,还没有vue-loader的配置⽂件:
⾸先,我们需要在项⽬根⽬录下⾯新建⼀个fig.js⽂件,然后开始我们今天的主题。
⼿动配置css到单独⽂件
说到提取css⽂件,我们应该先去terminal终端去安装⼀下相关的npm包:
先来说个简答的⽅法,上代码:
// fig.js
var ExtractTextPlugin = require("extract-text-webpack-plugin")
vae是什么意思//
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
extractCSS: true
}
}
]
},
plugins: [
new ExtractTextPlugin("style.css")
]
}
上述内容将⾃动处理 *.vue ⽂件内的 <style> 提取到style.css⽂件⾥⾯,并与⼤多数预处理器⼀样开箱即⽤。注意这只是提取 *.vue ⽂件 - 但在 JavaScript 中导⼊的 CSS 仍然需要单独配置。
接下来我们再看看如何⼿动配置:
ah me com// fig.js
var ExtractTextPlugin = require("extract-text-webpack-plugin")
//
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: act({
u: 'css-loader',
fallback: 'vue-style-loader' // 这是vue-loader的依赖
})
}
}
}白色英文
]
},
plugins: [
new ExtractTextPlugin("style.css")
]
}
此举便将所有 Vue 组件中的所有已处理的 CSS 提取到了单个的 CSS ⽂件。
如何构建⽣产环境
⽣产环境打包,⽬的只有两个:1.压缩应⽤代码;2.去除Vue.js中的警告。
下⾯的配置仅供参考:
// fig.js
// ... other options
plugins: [
new webpack.DefinePlugin({
'v': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin()
]
当然,如果我们不想在开发过程中使⽤这些配置,有两种⽅法可以解决这个问题:
1.使⽤环境变量动态构建;
例如:const isDev = v.NODE_ENV==='development'
或者:const isProd = v.NODE_ENV === 'production'
2.使⽤两个分开的 webpack 配置⽂件,⼀个⽤于开发环境,⼀个⽤于⽣产环境。把可能共⽤的配置放到第三个⽂件中。借鉴⼤⽜的经验
其中共⽤的配置⽂件fig.js的代码如下:
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const isProd = v.NODE_ENV === 'production'
devtool: isProd
fal
: '#cheap-module-source-map',
output: {
path: solve(__dirname, '../dist'),
publicPath: '/dist/',
filename: '[name].[chunkhash].js'
},
resolve: {civil rights
alias: {
'public': solve(__dirname, '../public')
}
},
module: {
noPar: /es6-promi\.js$/, // avoid webpack shimming process
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
compilerOptions: {
prerveWhitespace: fal
圣诞前夜}
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url-loader',
options: {
limit: 10000,
name: '[name].[ext]?[hash]'
}
},
{
test: /\.styl(us)?$/,
u: isProd
u: [
{
loader: 'css-loader',
options: { minimize: true }
},
'stylus-loader'
]
,
fallback: 'vue-style-loader'accompany用法
each是什么意思
})
: ['vue-style-loader', 'css-loader', 'stylus-loader']
]
},
performance: {
maxEntrypointSize: 300000,
hints: isProd ? 'warning' : fal
},
plugins: isProd
[
new VueLoaderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: fal }
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new ExtractTextPlugin({
filename: 'common.[chunkhash].css'
})
]
: [
new VueLoaderPlugin(),
new FriendlyErrorsPlugin()
]
}
然后看看⽤于开发环境的fig.js的配置是如何写的:const webpack = require('webpack')
const merge = require('webpack-merge')
const ba = require('./fig')
const SWPrecachePlugin = require('sw-precache-webpack-plugin')
const VueSSRClientPlugin = require('vue-rver-renderer/client-plugin')
const config = merge(ba, {
entry: {
app: './src/entry-client.js'
},
resolve: {
alias: {
'create-api': './create-api-client.js'
明白的英文}
},细节决定成败演讲稿
plugins: [
// strip dev-only code in Vue source
new webpack.DefinePlugin({
'v.NODE_ENV': JSON.v.NODE_ENV || 'development'), 'v.VUE_ENV': '"client"'
}),
// extract vendor chunks for better caching
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
// a module is extracted into the vendor
return (
摩登家庭第二季
// it's inside node_modules
/node_modules/.t) &&
// and not a CSS file (due to extract-text-webpack-plugin limitation)
!/\.css$/.quest)
)
}
}),
// extract webpack runtime & manifest to avoid vendor chunk hash changing
// on every build.
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest'
}),
new VueSSRClientPlugin()
]
})
if (v.NODE_ENV === 'production') {
config.plugins.push(
// auto generate rvice worker
new SWPrecachePlugin({
cacheId: 'vue-hn',
filename: 'rvice-worker.js',
minify: true,