Webpacker helps Rails apps manage and bundle JavaScript files so they load fast and work well together.
Webpacker and JavaScript bundling in Ruby on Rails
import { something } from './some_file' // Use something in your JavaScript code // In Rails, you include the bundled JavaScript with: <%= javascript_pack_tag 'application' %>
JavaScript files are placed in app/javascript/packs/ folder.
The javascript_pack_tag helper includes the bundled JavaScript in Rails views.
// app/javascript/packs/application.js import './hello' console.log('Hello from Webpacker!')
// app/javascript/packs/hello.js
console.log('Hello from separate file!')<%= javascript_pack_tag 'application' %>This example shows how to split JavaScript into two files and include them in a Rails app using Webpacker.
// app/javascript/packs/application.js import './greet' // app/javascript/packs/greet.js console.log('Greetings from Webpacker!') // In your Rails view (e.g., app/views/layouts/application.html.erb): <%= javascript_pack_tag 'application' %>
Webpacker automatically watches and rebuilds bundles during development.
Use bin/webpack-dev-server for faster live reloads in development.
Webpacker is replaced by jsbundling-rails in newer Rails versions, but many apps still use it.
Webpacker bundles JavaScript files for Rails apps to load efficiently.
Use javascript_pack_tag to include bundled JavaScript in views.
Organize JavaScript in app/javascript/packs/ and import modules as needed.
Practice
Solution
Step 1: Understand Webpacker's role
Webpacker is designed to bundle JavaScript files for Rails apps.Step 2: Identify its main function
Bundling means combining files to load faster and more efficiently in browsers.Final Answer:
To bundle JavaScript files so they load efficiently in the browser -> Option BQuick Check:
Webpacker bundles JS = C [OK]
- Confusing Webpacker with database tools
- Thinking Webpacker styles HTML
- Assuming Webpacker manages user login
Solution
Step 1: Identify the helper for JS packs
Webpacker usesjavascript_pack_tagto include JS bundles.Step 2: Confirm correct syntax
The syntax is<%= javascript_pack_tag 'pack_name' %>, so<%= javascript_pack_tag 'application' %>matches.Final Answer:
<%= javascript_pack_tag 'application' %> -> Option AQuick Check:
Use javascript_pack_tag for JS packs = A [OK]
- Using stylesheet_pack_tag for JavaScript
- Using javascript_include_tag instead of javascript_pack_tag
- Confusing image_pack_tag with JS inclusion
app/javascript/packs/hello.js with content:import { greet } from '../utils/greet'
console.log(greet('Rails'))and
app/javascript/utils/greet.js contains:export function greet(name) {
return `Hello, ${name}!`
}What will be printed in the browser console when the pack is loaded?
Solution
Step 1: Understand the import and export
Thegreetfunction is exported from utils/greet.js and imported correctly in hello.js.Step 2: Analyze the console.log output
Callinggreet('Rails')returns the stringHello, Rails!, which is logged.Final Answer:
Hello, Rails! -> Option DQuick Check:
Correct import/export logs greeting = D [OK]
- Forgetting to export the function
- Using wrong import path
- Expecting undefined instead of correct string
app/javascript/packs/custom.js but when you include it with <%= javascript_pack_tag 'custom' %>, the browser console shows a 404 error. What is the most likely cause?Solution
Step 1: Check file location
The file is correctly placed inapp/javascript/packs/, so location is likely correct.Step 2: Consider server restart
Webpacker requires the Rails server or webpack-dev-server to restart to detect new packs.Final Answer:
You forgot to restart the Rails server after adding the pack -> Option AQuick Check:
Restart server to load new packs = A [OK]
- Assuming helper is deprecated
- Ignoring server restart requirement
- Blaming syntax errors without checking logs
app/javascript/modules/calc.js exporting a function add(a, b). How should you import and use this function in your pack file app/javascript/packs/main.js to log the sum of 5 and 7?Solution
Step 1: Understand ES module import syntax
Ifaddis a named export, import it with curly braces:import { add } from 'path'.Step 2: Use the function correctly
Calladd(5, 7)and log the result withconsole.log.Final Answer:
import { add } from '../modules/calc' console.log(add(5, 7)) -> Option CQuick Check:
Named export import uses braces = B [OK]
- Using default import syntax for named exports
- Using require instead of import in Webpacker
- Forgetting to call function from imported module
Ready to prove it?
Pass the test and this pattern moves unknown
