Skip to content

Commit 70090c1

Browse files
author
Xiaoxing Hu
committed
next: fix image path
resolve ~ in path.
1 parent 08fb501 commit 70090c1

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

.changeset/tricky-lions-look.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@orgajs/example-next': patch
3+
'@orgajs/next': patch
4+
---
5+
6+
next: resolve ~ in image path

packages/next/src/plugin/_url-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const isAbsoluteUrl = (url: string) => {
99
if (WINDOWS_PATH_REGEX.test(url)) {
1010
return false
1111
}
12+
if (url.startsWith('~/')) return true
1213

1314
return ABSOLUTE_URL_REGEX.test(url)
1415
}

packages/next/src/plugin/image.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BaseNode, walk } from 'estree-walker'
33
import { Plugin, Transformer } from 'unified'
44
import { isJSXAttribute, isJSXOpeningElement, isLiteral } from './_estree-utils'
55
import { isRelativeUrl } from './_url-utils'
6+
import { join } from 'path'
67

78
export const processImage: Plugin = () => {
89
const transformer: Transformer = async (tree: BaseNode, file) => {
@@ -11,8 +12,14 @@ export const processImage: Plugin = () => {
1112

1213
const _import = (path: string) => {
1314
let p = path
14-
if (isRelativeUrl(p) && !p.startsWith('.')) {
15-
p = `./${p}`
15+
if (isRelativeUrl(p)) {
16+
if (!p.startsWith('.')) {
17+
p = `./${p}`
18+
}
19+
} else {
20+
if (p.startsWith('~')) {
21+
p = join(process.env.HOME, p.substring(1))
22+
}
1623
}
1724
const varName = `image${imports.length}`
1825
imports.push({

0 commit comments

Comments
 (0)