url.ts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
fxy060608's avatar
fxy060608 已提交
2
import qs from 'querystring'
fxy060608's avatar
fxy060608 已提交
3
import { EXTNAME_JS_RE, EXTNAME_VUE } from '../../constants'
fxy060608's avatar
fxy060608 已提交
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

export interface VueQuery {
  vue?: boolean
  src?: boolean
  type?: 'script' | 'template' | 'style' | 'custom'
  index?: number
  lang?: string
  raw?: boolean
  mpType?: 'page'
}

export function parseVueRequest(id: string) {
  const [filename, rawQuery] = id.split(`?`, 2)
  const query = qs.parse(rawQuery) as VueQuery
  if (query.vue != null) {
    query.vue = true
  }
  if (query.src != null) {
    query.src = true
  }
  if (query.index != null) {
    query.index = Number(query.index)
  }
  if (query.raw != null) {
    query.raw = true
  }
  return {
    filename,
    query,
  }
}
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41 42 43

const importQueryRE = /(\?|&)import(?:&|$)/
export const isImportRequest = (url: string) => importQueryRE.test(url)

export const queryRE = /\?.*$/
export const hashRE = /#.*$/

export const cleanUrl = (url: string) =>
  url.replace(hashRE, '').replace(queryRE, '')
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56

export function isJsFile(id: string) {
  const isJs = EXTNAME_JS_RE.test(id)
  if (isJs) {
    return true
  }
  const { filename, query } = parseVueRequest(id)
  const isVueJs = EXTNAME_VUE.includes(path.extname(filename)) && !query.vue
  if (isVueJs) {
    return true
  }
  return false
}