main.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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
const path = require('path')

const babel = require('@babel/core')

const loaderUtils = require('loader-utils')

const {
  hashify,
  hasModule,
  removeExt,
  normalizePath
} = require('@dcloudio/uni-cli-shared')

const {
  resolve,
  getPlatformExts,
  cacheGlobalComponents,
  normalizeNodeModules
} = require('./shared')

const babelPluginGlobalComponent = require('./babel-plugin-global-component')

const templateExt = getPlatformExts().template

function getNormalMainJsCode (params) {
  return `import App from './${normalizePath(params.page)}.vue'
import Vue from 'vue'
App.mpType='page'
const app = new Vue(App)
app.$mount()`
}

function getMPVuePageFactoryMainJsCode (params) {
  return `import pageFactory from 'mpvue-page-factory'
    import App from './${normalizePath(params.page)}.vue'
    Page(pageFactory(App))`
}

module.exports = function (content) {
  if (process.env.UNI_USING_COMPONENTS) {
    return require('./main-new').call(this, content)
  }
  this.cacheable && this.cacheable()
  if (this.resourceQuery) {
    const params = loaderUtils.parseQuery(this.resourceQuery)
    if (params && params.page) {
      params.page = decodeURIComponent(params.page)
      return (process.env.UNI_PLATFORM === 'mp-weixin' || process.env.UNI_PLATFORM === 'app-plus')
        ? getMPVuePageFactoryMainJsCode(params) : getNormalMainJsCode(params)
    }
  } else {
    // 解析全局组件
    const plugins = []
    if (hasModule('@babel/plugin-syntax-typescript')) {
      plugins.push('@babel/plugin-syntax-typescript')
      plugins.push([
        '@babel/plugin-proposal-decorators',
        {
fxy060608's avatar
fxy060608 已提交
59
          legacy: true
fxy060608's avatar
fxy060608 已提交
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
        }
      ])
    }
    plugins.push(babelPluginGlobalComponent)
    const ast = babel.transform(content, {
      root: process.env.UNI_CLI_CONTEXT,
      plugins
    })

    const globalComponents = {}

    const callback = this.async()

    if (!ast.metadata.globalComponents) {
      ast.metadata.globalComponents = {}
    }

    Promise.all(Object.keys(ast.metadata.globalComponents).map(name => {
      return resolve.call(this, ast.metadata.globalComponents[name]).then(resolved => {
        resolved = path.relative(process.env.UNI_INPUT_DIR, resolved)
        const hashed = hashify(resolved)
        globalComponents[name] = {
          name: hashed,
          src: '/' + normalizeNodeModules(removeExt(resolved)) + '.vue' +
                        templateExt
        }
      })
    })).then(() => {
      cacheGlobalComponents(globalComponents)
      callback(null, content)
    }, err => {
      callback(err, content)
    })
  }
}