We are using Webpacker for loading JavaScripts
and CSS
files into the webpage.
Currently, in webpacker.yml
we have set the source_path
to app/javascript
. Which is working fine to load the JavaScript files form this directory.
But in our application, we have an engines
directory, and all the JavaScript
files are located inside different engines
in engines
directory, to load these JavaScript files we created a link
in app/javascript/packs
for each pack in engines directory.
Is there a better way to do this, without providing links OR by providing multiple source_path
in the webpacker.yml
file.
This is the folder structure currently we have:
-root
|
|-app
|-javascript
|-packs
|-[link to engine1.js pack files]
|-[link to engine2.js pack files]
|-engines
|- engine1
|-app
|-javascript
|-packs
|-engine1.js
|- engine2
|-app
|-javascript
|-packs
|-engine1.js
And this is how the
webpacker.yml
configuration
default: &default
source_path: app/javascript
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: ['app/assets']
To include Webpacker in a new project, add --webpack to the rails new command. To add Webpacker to an existing project, add the webpacker gem to the project's Gemfile , run bundle install , and then run bin/rails webpacker:install . The installation also calls the yarn package manager, creates a package.
Method: Webpacker::Helper#javascript_pack_tagCreates a script tag that references the named pack file, as compiled by webpack per the entries list in package/environments/base. js. By default, this list is auto-generated to match everything in app/javascript/packs/*. js.
Webpacker is a gem which allows easy integration of JavaScript pre-processor and bundler with Rails. It provides various helpers and configuration options to use webpack easily with Rails.
I think you'll want to do something like this:
additional_paths: ['engines']
Source: https://github.com/rails/webpacker#resolved
If you are adding Webpacker to an existing app that has most of the assets inside app/assets or inside an engine, and you want to share that with webpack modules, you can use the additional_paths option available in config/webpacker.yml. This lets you add additional paths that webpack should lookup when resolving modules:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With