0
0
Ruby on Railsframework~10 mins

Import maps (Rails 7+) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Import maps (Rails 7+)
Browser requests JS module
Import map lookup
Resolve module URL
Fetch module from URL
Execute module code
Module exports available
Next module import or app runs
The browser uses the import map to find the URL for each JS module, fetches it, runs it, and makes its exports available for the app.
Execution Sample
Ruby on Rails
import { greet } from "./greet.js"
greet()

// greet.js exports a greet function
This code imports a greet function using the import map and calls it.
Execution Table
StepActionModule RequestedImport Map LookupResolved URLResult
1Import statement runs"./greet.js"Look up './greet.js' in import map"/assets/greet.js"URL found
2Browser fetches module"/assets/greet.js"N/AN/AModule code downloaded
3Execute module code"/assets/greet.js"N/AN/Agreet function exported
4Call greet()N/AN/AN/Agreet function runs, prints greeting
5No more importsN/AN/AN/AApp continues running
💡 All imports resolved and executed, app code runs fully
Variable Tracker
VariableStartAfter Step 3After Step 4Final
greetundefinedfunction greet() {...}function greet() {...}function greet() {...}
Key Moments - 2 Insights
Why does the browser need the import map to find './greet.js'?
Because browsers don't know where to find './greet.js' by default, the import map tells it the exact URL to fetch, as shown in step 1 of the execution table.
What happens if the import map does not have an entry for a module?
The browser cannot resolve the module URL, so it fails to fetch and run the module, stopping the app from loading that code. This is implied by the import map lookup step in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the resolved URL for './greet.js'?
A"/greet.js"
B"./greet.js"
C"/assets/greet.js"
D"/js/greet.js"
💡 Hint
Check the 'Resolved URL' column in step 1 of the execution table.
At which step does the greet function become available to use?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Result' column to see when the module exports are ready.
If the import map entry for './greet.js' was missing, what would happen?
AThe browser would fail to resolve and not fetch the module
BThe browser would fetch the module from the root URL
CThe greet function would still run
DThe app would ignore the import and continue
💡 Hint
Refer to the key moment about missing import map entries and step 1 lookup.
Concept Snapshot
Import maps tell the browser where to find JS modules by mapping module names to URLs.
When a module is imported, the browser looks up the URL in the import map.
It fetches and runs the module code, making exports available.
This avoids bundling and lets Rails 7+ apps load JS modules directly.
Missing import map entries cause module load failures.
Full Transcript
Import maps in Rails 7+ let the browser know where to find JavaScript modules by mapping module names to URLs. When the app imports a module like './greet.js', the browser checks the import map to find the exact URL to fetch. It downloads and runs the module code, making its exports available for use. This process repeats for all imports. If the import map lacks an entry for a module, the browser cannot resolve it and the module fails to load, stopping the app from running that code. This approach lets Rails apps load JavaScript modules without bundling, simplifying development.