All files index.js

100% Statements 72/72
88.46% Branches 23/26
100% Functions 16/16
100% Lines 69/69
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198    1x 2x 2x 2x 2x   2x 2x 2x 2x         19x       27x       10x 10x         10x     10x       9x 9x         9x     9x       9x 9x 9x     9x       18x 11x 11x 11x       11x         7x 7x       9x 9x   6x 6x 6x 6x       9x 9x   5x 8x 8x 8x         9x 9x       9x             9x 9x   9x 9x   9x           9x       9x         9x       9x       9x       9x       9x     2x       19x 10x   10x 10x     10x       1x         1x     9x                                 9x   9x 9x          
'use-strict'
 
module.exports = function ({ types: t, template }) {
  const visited = Symbol('visited')
  const universalImportId = Symbol('universalImportId')
  const importCssId = Symbol('importCssId')
  const pathId = Symbol('pathId')
 
  const chunkNameTemplate = template('() => MODULE')
  const pathTemplate = template('() => PATH.join(__dirname, MODULE)')
  const resolveTemplate = template('() => require.resolveWeak(MODULE)')
  const loadTemplate = template(
    '() => Promise.all([IMPORT, IMPORT_CSS(MODULE)]).then(proms => proms[0])'
  )
 
  function getImportArgPath(p) {
    return p.parentPath.get('arguments')[0]
  }
 
  function trimChunkNameBaseDir(baseDir) {
    return baseDir.replace(/^[./]+|(\.js$)/g, '')
  }
 
  function getUniversalImport(p) {
    Eif (!p.hub.file[universalImportId]) {
      const universal = p.hub.file.addImport(
        'babel-plugin-universal-import/universalImport.js',
        'default',
        'universalImport'
      )
      p.hub.file[universalImportId] = universal
    }
 
    return p.hub.file[universalImportId]
  }
 
  function getImportCss(p) {
    Eif (!p.hub.file[importCssId]) {
      const importCss = p.hub.file.addImport(
        'babel-plugin-universal-import/importCss.js',
        'default',
        'importCss'
      )
      p.hub.file[importCssId] = importCss
    }
 
    return p.hub.file[importCssId]
  }
 
  function getPath(p) {
    Eif (!p.hub.file[pathId]) {
      const path = p.hub.file.addImport('path', 'default', 'path')
      p.hub.file[pathId] = path
    }
 
    return p.hub.file[pathId]
  }
 
  function createTrimmedChunkName(importArgNode) {
    if (importArgNode.quasis) {
      const quasis = importArgNode.quasis.slice(0)
      const baseDir = trimChunkNameBaseDir(quasis[0].value.cooked)
      quasis[0] = Object.assign({}, quasis[0], {
        value: { raw: baseDir, cooked: baseDir }
      })
 
      return Object.assign({}, importArgNode, {
        quasis
      })
    }
 
    const moduleName = trimChunkNameBaseDir(importArgNode.value)
    return t.stringLiteral(moduleName)
  }
 
  function getMagicCommentChunkName(importArgNode) {
    const { quasis, expressions } = importArgNode
    if (!quasis) return trimChunkNameBaseDir(importArgNode.value)
 
    const baseDir = quasis[0].value.cooked
    const hasExpressions = expressions.length > 0
    const chunkName = baseDir + (hasExpressions ? '[request]' : '')
    return trimChunkNameBaseDir(chunkName)
  }
 
  function getComponentId(importArgNode) {
    const { quasis, expressions } = importArgNode
    if (!quasis) return importArgNode.value
 
    return quasis.reduce((str, quasi, i) => {
      const q = quasi.value.cooked
      const id = expressions[i] && expressions[i].name
      return (str += id ? `${q}\${${id}}` : q)
    }, '')
  }
 
  function idOption(importArgNode) {
    const id = getComponentId(importArgNode)
    return t.objectProperty(t.identifier('id'), t.stringLiteral(id))
  }
 
  function fileOption(p) {
    return t.objectProperty(
      t.identifier('file'),
      t.stringLiteral(p.hub.file.opts.filename)
    )
  }
 
  function loadOption(p, importArgNode) {
    const argPath = getImportArgPath(p)
    const chunkName = getMagicCommentChunkName(importArgNode)
 
    delete argPath.node.leadingComments
    argPath.addComment('leading', ` webpackChunkName: '${chunkName}' `)
 
    const load = loadTemplate({
      IMPORT: argPath.parent,
      IMPORT_CSS: getImportCss(p),
      MODULE: createTrimmedChunkName(importArgNode)
    }).expression
 
    return t.objectProperty(t.identifier('load'), load)
  }
 
  function pathOption(p, importArgNode) {
    const path = pathTemplate({
      PATH: getPath(p),
      MODULE: importArgNode
    }).expression
 
    return t.objectProperty(t.identifier('path'), path)
  }
 
  function resolveOption(importArgNode) {
    const resolve = resolveTemplate({
      MODULE: importArgNode
    }).expression
 
    return t.objectProperty(t.identifier('resolve'), resolve)
  }
 
  function chunkNameOption(importArgNode) {
    const chunkName = chunkNameTemplate({
      MODULE: createTrimmedChunkName(importArgNode)
    }).expression
 
    return t.objectProperty(t.identifier('chunkName'), chunkName)
  }
 
  return {
    name: 'universal-import',
    visitor: {
      Import(p) {
        if (p[visited]) return
        p[visited] = true
 
        const importArgNode = getImportArgPath(p).node
        const universalImport = getUniversalImport(p)
 
        // if being used in an await statement, return load() promise
        if (
          p.parentPath.parentPath.isYieldExpression() || // await transformed already
          t.isAwaitExpression(p.parentPath.parentPath.node) // await not transformed already
        ) {
          const func = t.callExpression(universalImport, [
            loadOption(p, importArgNode).value,
            t.booleanLiteral(false)
          ])
 
          return p.parentPath.replaceWith(func)
        }
 
        const opts = this.opts.babelServer
          ? [
            idOption(importArgNode),
            fileOption(p),
            pathOption(p, importArgNode),
            resolveOption(importArgNode),
            chunkNameOption(importArgNode)
          ]
          : [
            idOption(importArgNode),
            fileOption(p),
            loadOption(p, importArgNode), // only when not on a babel-server
            pathOption(p, importArgNode),
            resolveOption(importArgNode),
            chunkNameOption(importArgNode)
          ]
 
        const options = t.objectExpression(opts)
 
        const func = t.callExpression(universalImport, [options])
        p.parentPath.replaceWith(func)
      }
    }
  }
}