Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 vue-loader V14 中 style 的提取(json化) #3

Open
RonkTsang opened this issue Jun 15, 2018 · 0 comments
Open

在 vue-loader V14 中 style 的提取(json化) #3

RonkTsang opened this issue Jun 15, 2018 · 0 comments
Labels
小点记录 记录小的改变点、改进点 源码学习记录 源码学习中的思路记录,需后续整理

Comments

@RonkTsang
Copy link
Owner

RonkTsang commented Jun 15, 2018

vue-loader v14 中对 style 的载入是一个动态的载入

一个 .vue 文件经过 vue-loader 后,style 会是一个注入函数

var injectStyle = require("!!vue-style-loader!css-loader!../node_modules/vue-loader/lib/style-compiler/index?{\"optionsId\":\"0\",\"vue\":true,\"id\":\"data-v-dc790388\",\"scoped\":true,\"sourceMap\":false}!sass-loader!../node_modules/vue-loader/lib/selector?type=styles&index=0!./hello.vue")

injectStyle 是经过 selector, style-compiler, css-loader, vue-style-loader 后生成的一个注入函数,随后被作为参数传入了 normalizeComponent()

normalizeComponent

而关于 normalizeComponent, 文件里的注释如下,说的是这个函数是属于 runtime 的,打包后才运行的

This module is a runtime utility for cleaner component module output and will
be included in the final webpack user bundle.

normalizeComponent 对 style 的处理:

  • 第一步,生成一个样式加载的函数
/* 样式加载函数 */
  var hook
  if (moduleIdentifier) { // server build
    hook = function (context) {
      // 2.3 injection
      context =
        context || // cached call
        (this.$vnode && this.$vnode.ssrContext) || // stateful
        (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
      // 2.2 with runInNewContext: true
      if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
        context = __VUE_SSR_CONTEXT__
      }
      // inject component styles
      if (injectStyles) {
        injectStyles.call(this, context)
      }
      // register component module identifier for async chunk inferrence
      if (context && context._registeredComponents) {
        context._registeredComponents.add(moduleIdentifier)
      }
    }
    // used by ssr in case component is cached and beforeCreate
    // never gets called
    options._ssrRegister = hook
  } else if (injectStyles) {
    hook = shadowMode
      ? function () { injectStyles.call(this, this.$root.$options.shadowRoot) }
      : injectStyles
  }
  • 第二步,样式加载函数作为钩子函数执行
if (hook) {
    console.log('\n\n options: ', options)
    if (options.functional) {
      // for template-only hot-reload because in that case the render fn doesn't
      // go through the normalizer
      options._injectStyles = hook
      // register for functioal component in vue file
      var originalRender = options.render
      options.render = function renderWithStyleInjection (h, context) {
         hook.call(context)
         return originalRender(h, context)
      }
    } else {
      // inject component registration as beforeCreate hook
      var existing = options.beforeCreate
      options.beforeCreate = existing
        ? [].concat(existing, hook)
        : [hook]
    }
  }

生成 loaderString

生成一个 requirestring,用来加载style
// todo

selector,

selectorloaderString 中的第一个 loader,用来选择 .vue 文件的对应代码块
// todo

style-compiler

style-compiler
// todo

vue-style-loader

vue-style-loader 生成动态插入样式的代码
// todo

@RonkTsang RonkTsang added 小点记录 记录小的改变点、改进点 源码学习记录 源码学习中的思路记录,需后续整理 labels Jun 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
小点记录 记录小的改变点、改进点 源码学习记录 源码学习中的思路记录,需后续整理
Projects
None yet
Development

No branches or pull requests

1 participant