I am migrating a Rails project from Webpacker to Webpack 5. The last hiccup is that Webpack isn't properly importing my Stimulus controllers. Per the Stimulus docs, I have this code in my entrypoint:
// app/javascript/global.js
import { Application } from "@hotwired/stimulus"
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
window.Stimulus = Application.start()
const context = require.context("./src/controllers", true, /\.js$/)
Stimulus.load(definitionsFromContext(context))
The controllers themselves live in app/javascript/src/controllers.
The assets compile as expected, but the Stimulus controllers aren't registered. Moreover, when I log the output from require.context() above, I get the following console error:
function t(e){var t=new Error("Cannot find module '"+e+"'");throw t.code="MODULE_NOT_FOUND",t}
I know the file path to my controllers is correct, because when I change it to something obviously incorrect, the build fails. In addition, when I manually register a controller:
import { Application } from "@hotwired/stimulus"
import Tabs from "./tabs_controller";
const application = Application.start()
application.register("tabs", Tabs);
The controller loads and functions as expected, so I don't believe it's a Stimulus issue, specifically.
Am I configuring the require.context() method incorrectly? It's all boilerplate straight from the documentation, so it should work, but I'm at a loss for what else to troubleshoot.