esbuild.ts 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import path from 'path'
import fs from 'fs-extra'
fxy060608's avatar
fxy060608 已提交
3
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
4 5
import { transformWithEsbuild } from '@dcloudio/uni-cli-shared'
import type { BuildOptions, PluginBuild } from 'esbuild'
fxy060608's avatar
fxy060608 已提交
6 7 8
import type { Plugin } from 'vite'
import { nvueOutDir } from '../../utils'

fxy060608's avatar
fxy060608 已提交
9
const debugEsbuild = debug('uni:app-nvue-esbuild')
fxy060608's avatar
fxy060608 已提交
10
export function uniEsbuildPlugin(): Plugin {
fxy060608's avatar
fxy060608 已提交
11 12
  let buildOptions: BuildOptions
  const outputDir = process.env.UNI_OUTPUT_DIR
fxy060608's avatar
fxy060608 已提交
13 14 15
  return {
    name: 'uni:app-nvue-esbuild',
    enforce: 'post',
fxy060608's avatar
fxy060608 已提交
16 17 18 19
    configResolved(config) {
      buildOptions = {
        format: 'iife',
        minify: config.build.minify ? true : false,
fxy060608's avatar
fxy060608 已提交
20 21 22
        banner: {
          js: `"use weex:vue";`,
        },
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27
        bundle: true,
        write: false,
        plugins: [esbuildGlobalPlugin({ vue: 'Vue' })],
      }
    },
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39
    async writeBundle(_, bundle) {
      const entryPoints: string[] = []
      Object.keys(bundle).forEach((name) => {
        const chunk = bundle[name]
        if (
          chunk.type === 'chunk' &&
          chunk.facadeModuleId &&
          chunk.facadeModuleId.endsWith('.nvue')
        ) {
          entryPoints.push(name)
        }
      })
fxy060608's avatar
fxy060608 已提交
40
      debugEsbuild('start', entryPoints.length, entryPoints)
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46 47
      await Promise.all(
        entryPoints.map((filename) => {
          return buildNVuePage(filename, buildOptions).then((code) => {
            return fs.outputFile(path.resolve(outputDir, filename), code)
          })
        })
      )
fxy060608's avatar
fxy060608 已提交
48
      debugEsbuild('end')
fxy060608's avatar
fxy060608 已提交
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
    },
  }
}

function buildNVuePage(filename: string, options: BuildOptions) {
  return transformWithEsbuild(
    `import NVuePageComponent from './${filename}'
Vue.createApp(NVuePageComponent).mount('#root')`,
    path.join(nvueOutDir(), 'main.js'),
    options
  ).then((res) => {
    if (res.outputFiles) {
      return res.outputFiles[0].text
    }
    return ''
  })
}

function esbuildGlobalPlugin(options: Record<string, string>) {
  const keys = Object.keys(options)
  return {
    name: 'global',
    setup(build: PluginBuild) {
      keys.forEach((key) => {
        const namespace = key + '-ns'
        build.onResolve({ filter: new RegExp('^' + key + '$') }, ({ path }) => {
          return {
            path,
            namespace,
          }
        })
        build.onLoad({ filter: /.*/, namespace }, () => ({
          contents: `module.exports = ${options[key]}`,
          loader: 'js',
        }))
fxy060608's avatar
fxy060608 已提交
84 85 86 87
      })
    },
  }
}