【发布时间】:2021-10-10 03:57:32
【问题描述】:
我正在创建一个 Next.js 博客。在其中,我有一个 scripts/ 文件夹,其中包含从生成 RSS 到构建站点地图以及使用 ink CLI 创建新帖子的各种脚本。
我在 Next 应用程序中使用 scripts/new-post/ 文件夹下的 create-ink-app 构建了一个新的 CLI 项目。
我想访问一个名为authors.ts 的文件,它存在于new-post/ 文件夹之外,但在src/_data/authors.ts 下的Next.js 应用程序中。
Next.js 根目录中的树形结构如下所示:
.
├── README.md
├── next-env.d.ts
├── next.config.js
├── package-lock.json
├── package.json
├── prettier.config.js
├── public
│ ├── favicon.ico
│ └── vercel.svg
├── scripts/new-post
│ ├── package-lock.json
│ ├── package.json
│ ├── readme.md
│ ├── source
│ │ ├── cli.tsx
│ │ ├── components
│ │ │ ├── Input.tsx
│ │ │ ├── MultiSelect.tsx
│ │ │ └── index.ts
│ │ ├── state.ts
│ │ ├── test.tsx
│ │ └── ui.tsx
│ └── tsconfig.json
├── src
│ ├── _data
│ │ └── authors.ts
│ └── pages
│ ├── _app.tsx
│ └── index.tsx
└── tsconfig.json
当我在new-post/ 中使用tsc(我键入npm run dev)运行CLI 时,它会在dist/ 中创建一个子文件夹,同时在其中创建文件夹,即scripts/ 和src/。我希望在dist/ 文件夹中创建cli.js,但它实际上是在子文件夹中创建它。
dist/ 内部的树命令看起来像:
.
├── scripts/new-post/source
│ ├── cli.d.ts
│ ├── cli.js
│ ├── components
│ │ ├── Input.d.ts
│ │ ├── Input.js
│ │ ├── MultiSelect.d.ts
│ │ ├── MultiSelect.js
│ │ ├── index.d.ts
│ │ └── index.js
│ ├── state.d.ts
│ ├── state.js
│ ├── test.d.ts
│ ├── test.js
│ ├── ui.d.ts
│ └── ui.js
└── src/_data
├── authors.d.ts
└── authors.js
注意,额外创建了额外的scripts/new-post/source 命令。我不想要那个。
我尝试将tsconfig.json 中的"rootDir": "./" 用作this popular question suggests,但它不允许我使用authors.ts,因为它存在于src/_data/ 文件夹中。
如何在dist/ 文件夹中创建一个未嵌套在其中的平面输出?
我在这里搭建了一个完整的复制品 -> https://github.com/deadcoder0904/next-ink-dist-error
我尝试将package.json 中的bin 脚本指向dist/scripts/new-post/source/cli.js,但是当我运行new-post 时它不起作用。它抛出:
❯ new-post
node:internal/modules/cjs/loader:944
throw err;
^
Error: Cannot find module 'C:\Users\<username>\AppData\Roaming\npm\node_modules\new-post\dist\cli.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
我真的不想保持扁平结构。我只是想让new-post bin 命令工作,但它不起作用,所以我试图让它成为平面结构。
【问题讨论】:
标签: javascript node.js typescript tsconfig tsc