Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple source_path in Rails Webpacker

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.


For reference:

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']
like image 484
Dipak Avatar asked Jun 22 '18 07:06

Dipak


People also ask

How do I add a Webpacker in Rails 6?

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.

What is Javascript_pack_tag?

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.

What is Web Packer?

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.


1 Answers

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:

like image 81
user3738936 Avatar answered Sep 22 '22 08:09

user3738936