非交互式创建-next-app为什么不会使用默认的导入别名?

huangapple go评论110阅读模式
英文:

Why non-interactive create-next-app will not use default import alias?

问题

非交互式的 create-next-app 具有一个参数 --import-alias 用于配置导入别名。

--import-alias <alias-to-configure>

  指定要使用的导入别名(默认为 "@/*")。

我不想自定义导入别名,而是使用默认的 @/*。在这种情况下,当执行 create-next-app 时,我不应该指定 --import-alias,就像下面这样:

$ npx create-next-app@latest hello --javascript --eslint --no-tailwind --no-src-dir --app
? Would you like to customize the default import alias? » No / Yes

为什么它不会自动采用默认值并询问我是否要自定义默认的导入别名呢?

create-next-app 的版本是 13.4.4。

英文:

The non-interactive create-next-app has an argument --import-alias to configure the import alias.

--import-alias <alias-to-configure>
 
  Specify import alias to use (default "@/*").

I do not want to customize the import alias, but to use the default @/*. In this case, I should not specify --import-alias when executing create-next-app like the following:

$ npx create-next-app@latest hello --javascript --eslint --no-tailwind --no-src-dir --app
? Would you like to customize the default import alias? » No / Yes

Why it does not take the default value automatically and asked me if I want to customize the default import alias?

The version of create-next-app is 13.4.4.

答案1

得分: 1

首次在此回答,如果有任何格式或语言错误,请见谅。

简而言之:

npx create-next-app hello --javascript --eslint --no-tailwind --no-src-dir --app --import-alias '@/*'

只需指定默认别名即可消除提示。

如何以及为什么:

我进行了一些调查,因为我遇到了与您类似的问题。我查看了Next.js的源代码,我无法评论他们应该如何处理这个问题,但如果我们参考这一行:https://github.com/vercel/next.js/blob/e0ca2ba544b562bf718afadbc75db1885cb41504/packages/create-next-app/index.ts#L376

您会看到一个开始的条件子句,看起来像这样:

if (
  typeof program.importAlias !== 'string' ||
  !program.importAlias.length
) {
...

这似乎是您问题的根源,该命令正在查看importAlias的类型以及是否输入了任何内容。这意味着即使解析 --no-import-alias 也不起作用,因为这个条件不会考虑到它。相比之下,这是源目录选项的条件:

if (
  !process.argv.includes('--src-dir') &&
  !process.argv.includes('--no-src-dir')
) {
...

这里只关心您是否设置了该选项,如果您设置了选项,它将不会提示您。

尽管如此,我仍然尝试在Python中使用子进程以编程方式运行此命令,但仍然无法为我工作,所以如果您的用例与我的类似,我们都卡住了!希望您的情况只是在终端/PowerShell中,这会帮助您!

英文:

First answer on here so bare with me if there is any formatting/language mistakes.

TLDR;

npx create-next-app hello --javascript --eslint --no-tailwind --no-src-dir --app --import-alias '@/*'

Just specifying the default alias should get rid of the prompt

How and why;

I did some digging as I ran into a similar issue to you. I had a look in the nextjs source code - I can't comment on how they should be doing this but if we refer to this line: https://github.com/vercel/next.js/blob/e0ca2ba544b562bf718afadbc75db1885cb41504/packages/create-next-app/index.ts#L376

You will see a clause start that looks like this:

if (
  typeof program.importAlias !== 'string' ||
  !program.importAlias.length
) {
...

This seems to be the source of your problem, the command is looking at importAlias's type and if there has been anything input. This means even parsing --no-import-alias will not work because this conditional doesn't look at that. By comparison this is the conditional for the source directory option:

if (
  !process.argv.includes('--src-dir') &&
  !process.argv.includes('--no-src-dir')
) {
...

Here all it cares about is wether you have set the option or not and if you have set an option it won't propmt you.

All that being said I am trying to run this command programmatically using subprocess in Python and it still isn't working for me so if your use case is similar to mine. We are both stuck! Hopefully yours is just in a terminal/powershell and this helps you out!

huangapple
  • 本文由 发表于 2023年5月28日 06:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定