安装运行 taro

  1. git clone https://github.com/NervJS/taro
  2. yarn
  3. yarn run bootstrap
  4. yarn build
  5. cd packages/taro-cli
  6. npm link
  7. taro -v

通过以上几步就可以把Taro安装到本地了

开始调试

taro-cli/src/cli.ts

yarn build
npm run build:weapp — —watch

  1. parseArgs---------->>> [
  2. '/.nvm/versions/node/v12.16.2/bin/node',
  3. '/.nvm/versions/node/v12.16.2/bin/taro',
  4. 'build',
  5. '--type',
  6. 'weapp',
  7. '--watch'
  8. ]

build的执行函数是

  1. build(kernel, {
  2. platform: args.type,
  3. isWatch: !!args.watch,
  4. port: args.port,
  5. env: args.env,
  6. release: args.release,
  7. ui: args.ui,
  8. uiIndex: args.uiIndex,
  9. page: args.page,
  10. component: args.component,
  11. plugin: args.plugin,
  12. isHelp: args.h
  13. })

cli包里面调用到了 service包里的 Kernel 类

  1. constructor (options: IKernelOptions) {
  2. super()
  3. console.log('kernel---------->>>init')
  4. this.debugger = createDebug('Taro:Kernel')
  5. this.appPath = options.appPath || process.cwd()
  6. this.optsPresets = options.presets
  7. this.optsPlugins = options.plugins
  8. this.hooks = new Map()
  9. this.methods = new Map()
  10. this.commands = new Map()
  11. this.platforms = new Map()
  12. this.initHelper()
  13. }

console打印要 在 service 里执行 npm run prod 生成dist包,才能生效

kernel函数

kernel函数是从taro-services里面导入的
image.png
EventEmitter对象有点陌生

  1. # 上一个注册的异步回调执行之后的返回值会传递给下一个注册的回调。
  2. async applyPlugins (args: string | { name: string, initialVal?: any, opts?: any }) {
  3. const waterfall = new AsyncSeriesWaterfallHook(['arg'])
  4. return await waterfall.promise(initialVal)
  5. }

AsyncSeriesWaterfallHook 是从webpack的 tapable 导入的
tapable 是一个类似于 Node.js 中的 EventEmitter的库,但更专注于自定义事件的触发和处理
参考个链接 关于 tapable 你需要知道这些