Skip to content

Importing local files from app/javascript folder #55

Closed
@LukasSkywalker

Description

@LukasSkywalker

I tried to migrate from webpacker to importmap-rails and managed to port everything, including migrating from turbolinks to turbo-rails. Everything worked well in development mode, but broke as soon as I deployed the application. Specifically, a JavaScript file i loaded from application.js was not found. Here's my setup:

app/javascript/application.js

import "@hotwired/turbo-rails"
import { Filter } from './src/Filter.js';

/* some more js code skipped for brevity */

app/javascript/src/filter.js

class Filter {
  /* some more js code skipped for brevity */
}

As soon as I run the application in production mode, I get the following browser error when loading the page: Uncaught Error: 404 Not Found https://example.com/assets/src/Filter.js

I tried the following mitigations and many possible combinations:

  • Adding pin_all_from "app/javascript/src", under: "src" to config/importmap.rb
  • Moving the filter.js file to app/assets/javascripts/src, and adding //= link_tree ../../../app/assets/javascripts/src .jsto app/assets/config/manifest.js, as well as adding pin_all_from "app/assets/javascripts/src", under: "src" to config/importmap.rb
  • Adding Rails.application.config.assets.precompile += %w( src/*.js ) to config/initializers/assets.rb

I noticed that I can get the asset pipeline to compile filter.js (I do get a digested version in public/assets/src), but when the JS code is run in production, it always tries to load the non-digested version.

How can I import files that are available locally using importmaps?

I can provide a minimal example if the issue is unknown and if that helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions