All files index.js

82.76% Statements 48/58
72.55% Branches 37/51
100% Functions 8/8
85.45% Lines 47/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154    1x   1x         5x 5x 5x 5x   5x 3x 2x 2x 2x 2x 1x 1x 1x                   40x       29x 29x               4x           2x       2x 2x             2x 2x 2x               4x 4x             7x       1x 1x 1x           5x 5x 5x             1x 1x 1x 1x                       9x                         9x       40x 40x   40x   40x 7x   7x         7x            
'use-strict'
 
const visited = Symbol('visited')
 
module.exports = function dvaCoreHmrPlugin({
  types: t,
  template
}) {
  function getImportRequirePath(identifierName, scope) {
    Eif (scope.hasBinding(identifierName)) {
      const binding = scope.bindings[identifierName]
      Eif (binding) {
        const parent = binding.path.parent
 
        if (t.isImportDeclaration(parent)) { // import xxx from 'xxx' or import { xxx } from 'xxx'
          return parent.source.value
        } else Eif (t.isVariableDeclaration(parent)) {
          const declarator = findDeclarator(parent.declarations, identifierName)
          Eif (declarator) {
            if (isRequire(declarator.init)) { // const xxx = require('xxx')
              return getArguments0(declarator.init)
            } else Eif (isRequireMember(declarator.init)) { // const xxx = require('xxx').xxx
              return getArguments0(declarator.init.object)
            }
          }
        }
      }
    }
    return null
  }
 
  function isModelCall(node, appNames = ['app']) {
    if (!t.isMemberExpression(node)) return false
    const {
      object,
      property
    } = node
    return (
      (t.isIdentifier(property) && property.name === 'model') &&
      (t.isIdentifier(object) && appNames.indexOf(object.name) >= 0)
    )
  }
 
 
  function isRequire(node) {
    return t.isCallExpression(node) &&
      t.isIdentifier(node.callee) &&
      node.callee.name === 'require'
  }
 
  function isRequireMember(node) {
    Iif (!t.isMemberExpression(node)) return false
    const {
      object,
      property
    } = node
    return isRequire(object) &&
      t.isIdentifier(property) &&
      property.name
  }
 
  function findDeclarator(declarations, identifier) {
    // eslint-disable-next-line
    for (const d of declarations) {
      Eif (t.isIdentifier(d.id) && d.id.name === identifier) {
        return d
      }
    }
 
    return null
  }
 
  function getArguments0(node) {
    Eif (t.isLiteral(node.arguments[0])) {
      return node.arguments[0].value
    }
 
    return null
  }
 
  function getRequirePath(node, scope) {
    switch (node.type) {
      // app.model(require('xxxx))
      case 'CallExpression':
        {
          const path = getArguments0(node)
          Eif (path) {
            return path
          }
          break
        }
      case 'Identifier':
        {
          const path = getImportRequirePath(node.name, scope)
          Eif (path) {
            return path
          }
          break
        }
      // app.model(require('xxxx).xxxxx)
      case 'MemberExpression':
        {
          Eif (isRequireMember(node)) {
            const path = getArguments0(node.object)
            Eif (path) {
              return path
            }
          }
          break
        }
      default:
        break
    }
 
    return null
  }
 
  const hmrTemplate = template(`
  if (module.hot) {
    module.hot.accept('MODELPATH', () => {
      try {
        let newModel = require('MODELPATH');
        if (newModel.default) newModel = newModel.default;
        APPNAME.replaceModel(newModel);
      } catch(e) {
        console.error(e);
      }
    });
  }`)
 
  return {
    name: 'dva-core-hmr',
    visitor: {
      CallExpression(path) {
        Iif (path[visited]) return
        path[visited] = true
 
        const { callee, arguments: args } = path.node
 
        if (isModelCall(callee, this.opts.appNames)) {
          const modelPath = getRequirePath(args[0], path.scope)
 
          const hmrExpression = hmrTemplate({
            MODELPATH: t.stringLiteral(modelPath),
            APPNAME: callee.object
          })
 
          path.parentPath.insertAfter(hmrExpression)
        }
      }
    }
  }
}