index-new.js 4.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
const path = require('path')

const {
  md5,
  parseEntry,
  normalizePath
} = require('@dcloudio/uni-cli-shared')

fxy060608's avatar
fxy060608 已提交
9 10 11 12
const {
  pagesJsonJsFileName
} = require('@dcloudio/uni-cli-shared/lib/pages')

fxy060608's avatar
fxy060608 已提交
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
const generateApp = require('./generate-app')
const generateJson = require('./generate-json')
const generateComponent = require('./generate-component')

const emitFileCaches = {}

function emitFile (filePath, source, compilation) {
  const emitFileMD5 = md5(filePath + source)
  if (emitFileCaches[filePath] !== emitFileMD5) {
    emitFileCaches[filePath] = emitFileMD5
    compilation.assets[filePath] = {
      size () {
        return Buffer.byteLength(source, 'utf8')
      },
      source () {
        return source
      }
    }
  }
}

function addSubPackagesRequire (compilation) {
  if (!process.env.UNI_OPT_SUBPACKAGES) {
    return
  }
  const assetsKeys = Object.keys(compilation.assets)
  Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
    const subPackageVendorPath = normalizePath(path.join(root, 'common/vendor.js'))
    if (assetsKeys.indexOf(subPackageVendorPath) !== -1) {
      // TODO 理论上仅需在分包第一个 js 中添加 require common vendor,但目前不同平台可能顺序不一致,
      // 故 每个分包里的 js 里均添加一次 require
      assetsKeys.forEach(name => {
        if (
          path.extname(name) === '.js' &&
47
          name.indexOf(root + '/') === 0 &&
fxy060608's avatar
fxy060608 已提交
48 49
          name !== subPackageVendorPath
        ) {
fxy060608's avatar
fxy060608 已提交
50 51
          const source =
            `require('${normalizePath(path.relative(path.dirname(name), subPackageVendorPath))}');` +
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
            compilation.assets[name].source()

          compilation.assets[name] = {
            size () {
              return Buffer.byteLength(source, 'utf8')
            },
            source () {
              return source
            }
          }
        }
      })
    }
  })
}

68 69 70 71 72 73 74 75
function addMPPluginRequire (compilation) {
  // 编译到小程序插件 特殊处理入口文件
  if (process.env.UNI_MP_PLUGIN) {
    const assetsKeys = Object.keys(compilation.assets)
    assetsKeys.forEach(name => {
      if (name === process.env.UNI_MP_PLUGIN_MAIN) {
        const modules = compilation.modules

76
        const mainFilePath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, process.env.UNI_MP_PLUGIN_MAIN))
77 78 79 80 81 82

        const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === mainFilePath).id

        const newlineIndex = compilation.assets[name].source().lastIndexOf('\n')

        const source = compilation.assets[name].source().substring(0, newlineIndex) +
fxy060608's avatar
fxy060608 已提交
83
        `\nmodule.exports = wx.__webpack_require_${process.env.UNI_MP_PLUGIN.replace(/-/g, '_')}__('${uniModuleId}');\n` +
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        compilation.assets[name].source().substring(newlineIndex + 1)

        compilation.assets[name] = {
          size () {
            return Buffer.byteLength(source, 'utf8')
          },
          source () {
            return source
          }
        }
      }
    })
  }
}

fxy060608's avatar
fxy060608 已提交
99 100
class WebpackUniMPPlugin {
  apply (compiler) {
fxy060608's avatar
fxy060608 已提交
101
    if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
102 103 104
      compiler.hooks.emit.tapPromise('webpack-uni-mp-emit', compilation => {
        return new Promise((resolve, reject) => {
          addSubPackagesRequire(compilation)
fxy060608's avatar
fxy060608 已提交
105

106 107
          addMPPluginRequire(compilation)

fxy060608's avatar
fxy060608 已提交
108
          generateJson(compilation)
fxy060608's avatar
fxy060608 已提交
109

fxy060608's avatar
fxy060608 已提交
110 111 112 113 114 115
          // app.js,app.wxss
          generateApp(compilation)
            .forEach(({
              file,
              source
            }) => emitFile(file, source, compilation))
fxy060608's avatar
fxy060608 已提交
116

fxy060608's avatar
fxy060608 已提交
117
          generateComponent(compilation, compiler.options.output.jsonpFunction)
fxy060608's avatar
fxy060608 已提交
118

fxy060608's avatar
fxy060608 已提交
119 120
          resolve()
        })
fxy060608's avatar
fxy060608 已提交
121
      })
fxy060608's avatar
fxy060608 已提交
122
    }
fxy060608's avatar
fxy060608 已提交
123
    compiler.hooks.invalid.tap('webpack-uni-mp-invalid', (fileName, changeTime) => {
fxy060608's avatar
fxy060608 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      if (
        fileName &&
        typeof fileName === 'string'
      ) { // 重新解析 entry
        const basename = path.basename(fileName)
        const deps = process.UNI_PAGES_DEPS || new Set()
        if (
          basename === 'pages.json' ||
          basename === pagesJsonJsFileName ||
          deps.has(normalizePath(fileName))
        ) {
          try {
            parseEntry()
          } catch (e) {
            console.error(e)
          }
fxy060608's avatar
fxy060608 已提交
140 141 142 143 144 145
        }
      }
    })
  }
}

146
module.exports = WebpackUniMPPlugin