新起了一个项目,对照着改了一遍,相关的包括 package.json 全都一样。不知道哪里不对,每次 build js 的时候就会说 application 中引入 controllers 不对。但奇怪的是,我单独去引用 controllers 文件夹里面的 index 和 application 这两个 JS 文件,却没有任何问题。
我在引入了 bootstrap,试了下样式的可以用没有任何问题的,因为其实 build 出来本来也不会有什么问题。
但我就是不知道,到底为什么它会说 application 中引入 controllers 那里有问题,几乎是照着文档弄的。
麻烦各位看看能不能发现可能哪里有问题呢?不知道如何排查了。相关文件,都放下面了。
17:46:36 js.1 | 1 error
17:46:36 js.1 | [watch] build finished
17:46:49 js.1 | [watch] build started (change: "app/javascript/controllers")
17:46:49 js.1 | ✘ [ERROR] Could not resolve "./controllers"
17:46:49 js.1 |
17:46:49 js.1 | app/javascript/application.js:3:7:
17:46:49 js.1 | 3 │ import "./controllers"
package.json
{
"name": "app",
"private": "true",
"dependencies": {
"@hotwired/stimulus": "^3.1.0",
"@hotwired/turbo-rails": "^7.1.3",
"@popperjs/core": "^2.11.5",
"bootstrap": "^5.1.3",
"bootstrap-icons": "^1.8.3",
"esbuild": "^0.14.51",
"sass": "^1.52.1",
"sortable": "^2.0.0"
},
"scripts": {
"build:css": "sass app/assets/stylesheets/application.scss app/assets/builds/application.css --no-source-map --load-path=node_modules",
"build": "esbuild app/javascript/application.js --bundle --outdir=app/assets/builds"
}
}
Procfile.dev
web: bin/rails server -p 3000
css: yarn build:css --watch
js: yarn build --watch
app/javascripts/application.js
// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
其中 controllers 文件夹里仅有 1 个默认的 application.js 和一个我自己加的 js 文件 app/javascripts/controllers/index_controller.js
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="hello"
export default class extends Controller {
connect() {}
greet() {
console.log(123)
}
}
app/javascript/controllers/application.js
import { Application } from "@hotwired/stimulus"
const application = Application.start()
// Configure Stimulus development experience
application.debug = false
window.Stimulus = application
export { application }
manifest.js
//= link_tree ../builds