vue项⽬结构及⽂件介绍(2)——build⽂件夹
build⽂件夹⾥⾯是对 webpack 开发和打包的相关设置,包括⼊⼝⽂件、输出⽂件、使⽤的模块等;build.js⽂件
构建环境下的配置:
loading动画、删除创建⽬标⽂件夹、webpack编译、输出信息
'u strict'// js的严格模式
require('./check-versions')()// node和npm的版本检查
//导进各模块
const ora = require('ora')// loading模块
const rm = require('rimraf')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const config = require('../config')
const webpackConfig = require('./f')
const spinner = ora('building ')
spinner.start()
// rm⽅法删除dist/static⽂件夹
rm(path.join(config.build.astsRoot, config.build.astsSubDirectory), err =>{
if(err) throw err //若删除中有错误则抛出异常并终⽌程序
webpack(webpackConfig,(err, stats)=>{//若没有错误则继续执⾏,构建webpack
spinner.stop()//结束loading动画
if(err) throw err //若有异常则抛出
process.stdout.String({//标准输出流,类似于console.log
colors: true,//增加控制台颜⾊开关
modules: fal,//是否增加内置模块信息
children: fal,// If you are using ts-loader, tting this to true will make TypeScript errors show up during build.
chunks: fal,//允许较少的输出
chunkModules: fal //编译过程持续打印
})+'\n\n')
//编译出错的信息老年痴呆症
if(stats.hasErrors()){
console.d(' Build failed with errors.\n'))
}
//编译成功的信息
console.an(' Build complete.\n'))
console.llow(
' Tip: built files are meant to be rved over an HTTP rver.\n'+
' Opening index.html over file:// won\'t work.\n'
))
})
})
check-versions.js⽂件node和npm的版本检查
'u strict'// js的严格模式
//导进各模块
const chalk = require('chalk')
const mver = require('mver')
const packageConfig = require('../package.json')
const shell = require('shelljs')// shell.js插件,执⾏unix系统命令
function exec(cmd){
//脚本可以通过child_process模块新建⼦进程,从⽽执⾏Unix系统命令
//将cmd参数传递的值转换成前后没有空格的字符串,也就是版本号
return require('child_process').execSync(cmd).toString().trim()
}
/
/声明常量数组,数组内容为有关node相关信息的对象
const versionRequirements =[
{
name:'node',//对象名称为node
currentVersion: mver.clean(process.version),//使⽤mver插件,把版本信息转换成规定格式
versionRequirement: de //规定package.json中engines选项的node版本信息
}
]
if(shell.which('npm')){//which为linux指令,在$path规定的路径下查找符合条件的⽂件
versionRequirements.push({
name:'npm',
currentVersion:exec('npm --version'),//调⽤npm --version命令,并且把参数返回给exec函数获取纯净版本 versionRequirement: ines.npm //规定package.json中engines选项的node版本信息
})
}
const warnings =[]
for(let i =0; i < versionRequirements.length; i++){
const mod = versionRequirements[i]
//如果版本号不符合package.json⽂件中指定的版本号,就执⾏
//当前版本号⽤红⾊标识,要求版本号⽤绿⾊标识
if(!mver.satisfies(mod.currentVersion, mod.versionRequirement)){
warnings.push(mod.name +': '+
<(mod.versionRequirement)
)
}
}
//如果为真,则打印提⽰⽤户升级新版本
if(warnings.length){
console.log('')
console.llow('To u this template, you must update following to modules:'))
console.log()
for(let i =0; i < warnings.length; i++){
const warning = warnings[i]
console.log(' '+ warning)
}
console.log()
}
}
utils.js⽂件
配置静态资源路径;
孕妇能吃杏仁吗⽣成cssLoaders⽤于加载.vue⽂件中的样式;
⽣成styleLoaders⽤于加载不在.vue⽂件中的单独存在的样式⽂件
'u strict'
const path = require('path')
人民币真假鉴别const config = require('../config')//引⼊config下的index.js⽂件
const ExtractTextPlugin = require('extract-text-webpack-plugin')//⼀个插件,抽离css样式,防⽌将样式打包在js中引起样式加载错乱const packageConfig = require('../package.json')
const packageConfig = require('../package.json')
//导出astsPath
exports.astsPath = function (_path){
const astsSubDirectory = v.NODE_ENV ==='production'
config.build.astsSubDirectory
: config.dev.astsSubDirectory
return path.posix.join(astsSubDirectory, _path)// path.join返回绝对路径(在电脑上的实际位置);path.posix.join返回相对路径}
exports.cssLoaders = function (options){
options = options ||{}
const cssLoader ={
技术环节loader:'css-loader',
options:{
sourceMap: options.sourceMap
}
}
const postcssLoader ={
loader:'postcss-loader',
options:{
sourceMap: options.sourceMap
}
国华中学}
// generate loader string to be ud with extract text plugin
function generateLoaders (loader, loaderOptions){
const loaders = options.uPostCSS ? [cssLoader, postcssLoader]:[cssLoader]
if(loader){
loaders.push({
loader: loader +'-loader',
options: Object.assign({}, loaderOptions,{
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
//(which is the ca during production build)
act){
act({
u: loaders,
云吞馅的做法publicPath:'../../',
fallback:'vue-style-loader'
})
}el{
return['vue-style-loader'].concat(loaders)
}
}
// vue-loader.vuejs/en/configurations/extract-css.html
return{
return{
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass',{ indentedSyntax: true }), scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
// Generate loaders for standalone style files (outside of .vue) exports.styleLoaders = function (options){
const output =[]
const loaders = exports.cssLoaders(options)
for(const extension in loaders){
const loader = loaders[extension]
output.push({
邯郸旅游
test: new RegExp('\\.'+ extension +'$'),
u: loader
红薯汤})
}
return output
}
const notifier = require('node-notifier')
return(verity, errors)=>{
if(verity !=='error')return
const error = errors[0]
const filename = error.file&& error.file.split('!').pop()
title: packageConfig.name,
message: verity +': '+ error.name,
subtitle: filename ||'',
icon: path.join(__dirname,'logo.png')
})
}
}