1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { TransformAssetUrlsOptions } from './assetUrl'
- import { UrlWithStringQuery, parse as uriParse } from 'url'
- import path from 'path'
- export interface Attr {
- name: string
- value: string
- }
- export interface ASTNode {
- tag: string
- attrs: Attr[]
- }
- export function urlToRequire(
- url: string,
- transformAssetUrlsOption: TransformAssetUrlsOptions = {}
- ): string {
- const returnValue = `"${url}"`
-
- const firstChar = url.charAt(0)
- if (firstChar === '~') {
- const secondChar = url.charAt(1)
- url = url.slice(secondChar === '/' ? 2 : 1)
- }
- const uriParts = parseUriParts(url)
- if (transformAssetUrlsOption.base) {
-
-
-
- if (firstChar === '.' || firstChar === '~') {
-
-
- return `"${(path.posix || path).join(
- transformAssetUrlsOption.base,
- uriParts.path + (uriParts.hash || '')
- )}"`
- }
- return returnValue
- }
- if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
- if (!uriParts.hash) {
- return `require("${url}")`
- } else {
-
-
-
-
- return `require("${uriParts.path}") + "${uriParts.hash}"`
- }
- }
- return returnValue
- }
- function parseUriParts(urlString: string): UrlWithStringQuery {
-
- const returnValue: UrlWithStringQuery = uriParse('')
- if (urlString) {
-
-
- if ('string' === typeof urlString) {
-
- return uriParse(urlString)
- }
- }
- return returnValue
- }
|