前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Thinking--javascript 多类目创建(npm源码解读)

Thinking--javascript 多类目创建(npm源码解读)

作者头像
奋飛
发布2021-12-10 08:46:41
2150
发布2021-12-10 08:46:41
举报
文章被收录于专栏:Super 前端Super 前端

Npm 获取配置的顺序(由高到低):Command Line Flags > Environment Variables > npmrc Files > Default Configs

  1. Command Line Flags:--flag1 --flag2 bar -- flag3 结果:flag1=true,flag2=bar,flag3为命令参数。
  2. Environment Variables:以 npm_config_ 开头的环境变量将被解释为配置参数。npm_config_flag=bar 可通过 process.env.npm_config_flag获取
  3. npmrc Files:根据存放位置不同,优先级不同 – https://docs.npmjs.com/cli/v8/configuring-npm/npmrc
  4. Default Configs:内置配置参数,可通过 npm config ls -l 查看

相关配置命令:

代码语言:javascript
复制
npm config set <key> <value>
npm config get [<key>]
npm config delete <key>
npm config list [--json]
npm config edit
npm set <key> <value>
npm get [<key>]

这里重点讲述的是:npm 是如何对这些配置做“统一管理”。

核心思路:统一入口

  1. 枚举了每个“配置”【必选】【可选】属性,对此做统一强校验,后续无需再做是否存在判断。
  2. Object.assign(this, def) 【扩充实例属性】这里包括 key、def
  3. 统一定义【校验方法】,关联到当前实例中(挂载到了类的 prototype 属性上面)

Class 类方法都定义在类的 prototype 属性上面。 class Person { constructor (name) {} say(){} } console.log(Person.prototype) // {constructor: f, say: f} p.hasOwnProperty('say') // false p.__proto__.hasOwnProperty('say') // true 在类的实例上面调用方法,其实就是调用原型上的方法。 const p = new Person('ligang') p.constructor === Person.prototype.constructor // true 类的所有实例共享一个原型对象。 const p1 = new Person('li') const p2 = new Person('gang') p1.__proto__ === p2.__proto__ // true

definition.js 描述配置属性

代码语言:javascript
复制
// 配置项必须具备的属性
const required = ['key', 'type', 'default']	
// 配置项可选属性
const allowed = [...required, 'description', 'name', 'flatten']

/* 统一结构定义 */
class Definition {
  constructor (key, def) {
    this.key = key
    // 扩充实例属性
    Object.assign(this, def)
    // 校验
    this.validate()
    // 统一处理
    if (!this.defaultDescription) {}
    if (!this.typeDescription) {}
    if (!this.hint) {}
  }

  validate () {
    for (let req of required) {
      // 校验必须存在的属性
      if (!Object.prototype.hasOwnProperty.call(this, req)) {
        throw new Error(`缺失必须属性 ${req}: ${this.key}`)
      }
      // 校验是否存在未知属性
      for (let field of Object.keys(this)) {
        if (!allowed.includes(field)) {
          throw new Error(`存在未知属性 ${field}: ${this.key}`)
        }
      }
    }
  }
}

definitions.js 定义相关配置

代码语言:javascript
复制
// basic flattening function, just copy it over camelCase
const flatten = (key, obj, flatOptions) => {
  const camel = key.replace(/-([a-z])/g, (_0, _1) => _1.toUpperCase())
  flatOptions[camel] = obj[key]
}

const definitions = {}
const define = (key, def) => {
  definitions[key] = new Definition(key, def)
}
define('registry', {
  type: url,
  default: 'https://registry.npmjs.org/',
  description: 'The base URL of the npm registry.',
  flatten
})
define('user-agent', {
  type: String,
  default: 'npm/{npm-version} node/{node-version} {platform} {arch} workspaces/{workspaces} {ci}',
  description: 'Sets the User-Agent request header.',
  flatten (key, obj, flatOptions) {
    // 特殊处理
  }
})

参考地址:

  • https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
  • https://github.com/npm/cli/blob/HEAD/lib/utils/config/definition.js
  • https://github.com/npm/cli/blob/HEAD/lib/utils/config/definitions.js
  • https://blog.csdn.net/jiaojsun/article/details/99831112
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-12-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档