网络知识 科技 从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)

从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)

{"data":{"title":"从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)","abstract":"下面按照这个图来实现准备阶段检查版本号通过package.json来拿到version'use strict';module.exports = core;const pkg = require('../package.","cover":"https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/1087387c75df42f1be5e504abb6384d7","articleType":"article","itemId":"7112663898028884483","groupId":"7112663898028884483","groupSource":2,"isOriginal":true,"banComment":false,"publishTime":"2022-06-24 12:49","source":"程序员库里","tag":"technique","mediaSite":null,"pathname":"/article/7112663898028884483/","loginUserInfo":null,"favorite":false,"relation":{"isFollowing":false,"isFollowed":false},"likeData":{"userLikeStatus":0,"count":0},"isSelf":false,"content":"


从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)

下面按照这个图来实现准备阶段

检查版本号

通过package.json来拿到version

'use strict';nnmodule.exports = core;nnconst pkg = require('../package.json')nfunction core() {n checkPkgVersion()n}nnnfunction checkPkgVersion(){n console.log(pkg.version)n}n

这里有个知识点:

知识点

require可以加载所有后缀的文件,但是除了 .json和.node文件,都以js文件内容处理

.js => module.exports / exports

.json => JSON.parse

log模块

我们来实现根据环境变量打印log的级别,支持debug模式

我们使用npm第三方库 npmlog来实现

接下来看下npmlog的源码

npmlog的源码

log.addLevel('silly', -Infinity, { inverse: true }, 'sill')nlog.addLevel('verbose', 1000, { fg: 'cyan', bg: 'black' }, 'verb')nlog.addLevel('info', 2000, { fg: 'green' })nlog.addLevel('timing', 2500, { fg: 'green', bg: 'black' })nlog.addLevel('http', 3000, { fg: 'green', bg: 'black' })nlog.addLevel('notice', 3500, { fg: 'cyan', bg: 'black' })nlog.addLevel('warn', 4000, { fg: 'black', bg: 'yellow' }, 'WARN')nlog.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!')nlog.addLevel('silent', Infinity)n

从源码可以看到通过addLevel来增加不同的log,第二个参数代表log的级别,debug模式就是verbose,级别也就是1000

接下来通过npmlog来定制log

通过npmlog定制log

自定义log,来实现success日志打印

下面分别定义和使用:

// 定义nconst log = require('npmlog')nlog.addLevel('success',2000,{fg:'green',bold:true})nn// 使用nconst log = require('@rd-cli-dev/log')nlog.success('test', 'success...')n

调整npmlog level

从npmlog源码中可以看出,默认的级别是info,也就是2000

// default levelnlog.level = 'info' // 默认是infon// 默认level是2000,大于2000才会生效nlog.addLevel('info', 2000, { fg: 'green' }) n

level小于2000,不会生效,这时需要将level调整为verbose时候,变成debug模式,需要根据环境变量调整

log.addLevel('verbose', 1000, { fg: 'cyan', bg: 'black' }, 'verb')n

通过上面的讲解,我们来定制下log包

封装和使用log模块

// src/utils/log/lib/index.jsn// 封装n'use strict';nnconst log = require('npmlog')nnlog.level = process.env.LOG_LEVEL ? process.env.LOG_LEVEL: 'info';// 判断debug模式nlog.heading = 'rd-cli-dev' // 修改前缀nlog.addLevel('success',2000,{fg:'green',bold:true}) // 添加自定义命令nmodule.exports = log;nn// src/core/cli/lib/index.jsn// 使用n'use strict';nnmodule.exports = core;nnconst pkg = require('../package.json')nconst log = require('@rd-cli-dev/log')nfunction core() {n checkPkgVersion()n}nnnfunction checkPkgVersion(){n log.info('cli', pkg.version)n}n

log的效果

从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)

","imageList":["https://p6.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/a72786213c2f4c1091c7cf326d56c88e.png?from=pc","https://p6.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/6ceef59c24c745499deb76a7c8bbf0d7.png?from=pc"],"mediaInfo":{"userId":"MS4wLjABAAAAlnK9882y87tjH6LdUlr16hHTzWWhbjwqvvFANc0Fq3o","unsafeUserId":"6176765067","name":"程序员库里","avatarUrl":"https://p26.toutiaoimg.com/origin/daac001509728b72472d","description":"全栈工程师n公众号【字节爱好者】,领前端面试资料n抖音号【程序员库里】,分享前端算法和面试题nB站【程序员库里】,分享前端算法和面试题","userVerified":1,"userAuthInfo":""},"seoTDK":{"title":"从0开发一个高性能前端脚手架-第六篇-脚手架准备阶段实现(1)-今日头条","description":"下面按照这个图来实现准备阶段检查版本号通过package.json来拿到version'use strict';module.e","keywords":"","publishTimestamp":"1656046151","modifiedTimestamp":"1656047381"},"logId":"202206241419390101381722310587C4E6","sylpageConfig":{"card":{"id":""}},"identity":{"web_id":"7111199547455817229","user_is_login":false},"abtestInfo":{"rsp_type":5,"version_name":"4252807,4164637","parameters":{"feat_repost_type":{"new":true},"home_nav_conf":{"dcd_out":1},"local_filter":{"core_filter":{"filter_list":{"ms::TicaiFilter":true}}},"page_upgrade":{"new_profile":true,"video_double_column":true},"sati":{"enable_ad_prime":true,"enable_sorter_optimus":true,"prime_rule_rank_version":"toutiao_web","use_toutiao_web_feed":true,"format_max_consecutive_middle":2,"format_max_consecutive_nogroups":3,"enable_reduce_nogroup":true},"seraph":{"score_rule":{"default":{"replace":{"group_util":"_CTR - 1000*dislike"}},"new_user":{"replace":{"group_util":"_CTR - 1000*dislike"}}}},"sort":{"allowed_ticai":["forum_post","pgc_text","pgc_video"]},"video_detail_page_upgrade":{"new_page":true}},"env_flag":0},"localCityInfo":{"name":"北京","code":"110000","channelId":3202164529},"showResearch":false}}

返回顶部