pre.ts 1.2 KB
Newer Older
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
import path from 'path'
import debug from 'debug'
import { Plugin } from 'vite'
import { createFilter } from '@rollup/pluginutils'

import { UniPluginFilterOptions } from '.'

const { preJs, preHtml } = require('@dcloudio/uni-cli-shared')

const debugPreJs = debug('uni:pre-js')
const debugPreHtml = debug('uni:pre-html')
const debugPreJsTry = debug('uni:pre-js-try')

const PRE_JS_EXTNAME = ['.js', '.ts', '.json', '.css', '.vue', '.nvue']
const PRE_HTML_EXTNAME = ['.vue', '.nvue']
export function uniPrePlugin(options: UniPluginFilterOptions): Plugin {
  const filter = createFilter(options.include, options.exclude)
  return {
    name: 'vite:uni-pre',
    transform(code, id) {
      if (!filter(id)) {
        return code
      }
      const extname = path.extname(id)
      const isHtml = PRE_HTML_EXTNAME.includes(extname)
      const isJs = PRE_JS_EXTNAME.includes(extname)
      const isPre = isHtml || isJs
fxy060608's avatar
fxy060608 已提交
28 29 30
      if (isPre) {
        debugPreJsTry(id)
      }
31 32 33 34 35 36 37 38 39 40 41 42 43
      const hasEndif = isPre && code.includes('#endif')
      if (isHtml && hasEndif) {
        code = preHtml(code)
        debugPreHtml(id)
      }
      if (isJs && hasEndif) {
        code = preJs(code)
        debugPreJs(id)
      }
      return code
    },
  }
}