Skip to content

Commit 3f218a6

Browse files
cijiugechucsr632
andauthored
fix: find up package.json when project is located at subfolder (#78)
* feat: find up package.json * fix: pass vite project's root * improve code style * chore: fix pnpm resolve * chore: no longer need dummy package.json Co-authored-by: csr632 <[email protected]>
1 parent 796362a commit 3f218a6

File tree

5 files changed

+95
-61
lines changed

5 files changed

+95
-61
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
"sirv": "^2.0.2",
3434
"slash": "^3.0.0",
3535
"typescript": "^4.7.4",
36-
"vite-pages-theme-basic": "workspace:*",
37-
"vite-plugin-react-pages": "workspace:*",
3836
"wait-on": "^6.0.1"
3937
},
4038
"pnpm": {
4139
"overrides": {
4240
"react": "^17.0.1",
43-
"react-dom": "^17.0.1"
41+
"react-dom": "^17.0.1",
42+
"vite-plugin-react-pages": "workspace:*",
43+
"vite-pages-theme-doc": "workspace:*",
44+
"vite-pages-theme-basic": "workspace:*"
4445
},
4546
"peerDependencyRules": {
4647
"ignoreMissing": [

packages/create-project/template-lib/docs/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/react-pages/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"jotai": "^0.15.3",
7777
"mini-debounce": "^1.0.8",
7878
"minimist": "^1.2.5",
79+
"pkg-up": "^3.1.0",
7980
"remark-frontmatter": "^2.0.0",
8081
"slash": "^3.0.0",
8182
"tiny-invariant": "^1.1.0",

packages/react-pages/src/node/index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as path from 'path'
22
import fs from 'fs-extra'
3+
import pkgUp from 'pkg-up'
4+
import chalk from 'chalk'
35
import type { Plugin, IndexHtmlTransformContext } from 'vite'
46
import type { MdxPlugin } from 'vite-plugin-mdx/dist/types'
57
import {
@@ -99,7 +101,7 @@ export default function pluginFactory(opts: PluginConfig = {}): Plugin {
99101
)
100102
} else {
101103
logger.warn(
102-
'[react-pages] Please install [email protected] or higher'
104+
'[vite-plugin-react-pages] Please install [email protected] or higher'
103105
)
104106
}
105107
},
@@ -221,17 +223,26 @@ async function getRemarkPlugins(root: string) {
221223
ImageMdxPlugin,
222224
FileTextMdxPlugin,
223225
]
224-
const pkgJsonPath = path.join(root, 'package.json')
225-
// TODO: user may put the whole vite-pages project
226-
// under a sub folder (which is the root here),
227-
// so the package.json will be located at the upper folder.
228-
// checkout playground/custom-find-pages2.
229-
const hasPkgJson = fs.pathExistsSync(pkgJsonPath)
226+
227+
// pass vite project's root otherwise it will
228+
// start from process.cwd() by default
229+
const pkgJsonPath = pkgUp.sync({
230+
cwd: root,
231+
})
232+
233+
if (pkgJsonPath === null) {
234+
console.error(
235+
chalk.red(
236+
`[vite-plugin-react-pages] Could not find 'package.json', does it exist?\n'`
237+
)
238+
)
239+
process.exit(1)
240+
}
230241

231242
const pkgJson = await fs.readJSON(pkgJsonPath)
232243

233244
// Inject frontmatter parser if missing
234-
const { devDependencies = {}, dependencies = {} } = hasPkgJson ? pkgJson : {}
245+
const { devDependencies = {}, dependencies = {} } = pkgJson
235246
// By default we add remark-frontmatter automatically.
236247
// But if user install their own remark-frontmatter,
237248
// they are responsible to add the plugin manually

0 commit comments

Comments
 (0)