Rails 7.0 + esbuild:运行应用程序出现错误:找不到命令“build”

not*_*tch 17 ruby-on-rails ruby-on-rails-7

新生成的带有 esbuild 选项的 Rails 7.0 在启动时出错。

rails new [project name] --javascript=esbuild --css=tailwind
Run Code Online (Sandbox Code Playgroud)

在创建新的 Rails 7 项目时,我尝试使用bin/dev现在使用的 foreman 来启动应用程序。但是,它会出错,并显示“未找到错误命令“build””。

bin/dev
                                                                                                      !10520
16:07:31 web.1  | started with pid 53018
16:07:31 js.1   | started with pid 53019
16:07:31 css.1  | started with pid 53020
16:07:32 js.1   | yarn run v1.22.17
16:07:32 css.1  | yarn run v1.22.17                    **************             
16:07:32 js.1   | error Command "build" not found. <== *****THIS*****
16:07:32 js.1   | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
16:07:32 css.1  | error Command "build:css" not found.
16:07:32 css.1  | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
16:07:33 css.1  | exited with code 1
16:07:33 system | sending SIGTERM to all processes
16:07:33 js.1   | exited with code 1
16:07:33 web.1  | terminated by SIGTERM
Run Code Online (Sandbox Code Playgroud)

not*_*tch 40

问题

问题是,对于 npm < 7.1,rails 生成期望您将构建命令添加到package.json脚本中。

rails new my_app --javascript=esbuild --css=tailwind
...
Add "scripts": { "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds" } to your package.json
...
Add "scripts": { "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css" } to your package.json

$ cat ol_npm/package.json
{
  "name": "app",
  "private": "true",
  "dependencies": {
    "@hotwired/stimulus": "^3.0.1",
  ...
  }
  // !! script section missing !!
  // Add the above scripts
}

Run Code Online (Sandbox Code Playgroud)

稍后 npm (>= 7.1),将其添加到 package.json 中。最好的长期解决方案是升级 npm(解决方案 1),但是,您仍然可以手动添加脚本(请参阅下面的解决方案 2)并且它会起作用。

解决方案

1.npm升级:

该修复需要升级 npm。然后再次运行安装程序。

  1. js 捆绑需要 npm 7.1+
  2. 再次运行安装程序

重新运行安装程序的示例

   ./bin/rails javascript:install:[esbuild|rollup|webpack]
   ./bin/rails css:install:[tailwind|bootstrap|bulma|postcss|sass]
Run Code Online (Sandbox Code Playgroud)

完成此操作后,Rails 会自动更新package.json所需的脚本。

2.添加脚本到package.json

如果由于某种原因,您无法升级 node/npm,那么您只需按照说明将“添加脚本”命令复制到 package.json 中即可。

  1. 添加脚本(见下文)
   ./bin/rails javascript:install:[esbuild|rollup|webpack]
   ./bin/rails css:install:[tailwind|bootstrap|bulma|postcss|sass]
Run Code Online (Sandbox Code Playgroud)
  1. 纱线结构
  2. 纱线构建:CSS