Comments (25)
And in other cases? Like cocoon gem
(https://github.com/nathanvda/cocoon) ?
from webpacker.
I made it work by
-
Unpacking gem
cocoon-1.2.10
intovendor/gems/cocoon-1.2.10
(according to https://makandracards.com/makandra/538-freeze-vendor-unpack-a-single-ruby-gem-with-and-without-bundler). And modifiing Gemfile. -
Adding vendor path
resolved_paths: ['vendor']
toconfig/webpacker.yml
. -
Created file
vendor/gems/index.js
withimport {} from './cocoon-1.2.10/app/assets/javascripts/cocoon.js';
-
Import this file in
app/javascript/application.js
withimport {} from 'gems/index.js'; //in /vendor/gems
Of course, no npm package is then needed.
from webpacker.
I think we can add an helper to wrap that logic making easier to load assets from gems.
from webpacker.
I solved it by enabling erb like this comment describes: nathanvda/cocoon#452 (comment)
Once you have access to the JS via ERB you can make a shim, this is what I did for the best_in_place gem:
import jQuery from 'jquery'
import 'jquery-autosize'
<%= File.read(File.join(Gem.loaded_specs['best_in_place'].full_gem_path, 'lib', 'assets', 'javascripts', 'best_in_place.js')) %>
from webpacker.
I was also struggling with this and I got a solution that worked for me that I wanted to share. If you follow the instructions in link that @qwertme shared, you can write a ruby class helper in your lib folder and include it with rails-erb-loader-dependencies
to make it more presentable in your webpack application.
This is how I'm currently using this:
Rails 5.1.6.1
Webpacker 4.0.2
bundle exec rails webpacker:install:erb
# lib/webpack_helper.rb
class WebpackHelper
class << self
def my_gem_js(path)
File.join(Gem.loaded_specs['my_gem'].full_gem_path, 'app/assets/javascripts/my_gem/', path)
end
def my_gem_css(path)
File.join(Gem.loaded_specs['my_gem'].full_gem_path, 'app/assets/stylesheets/my_gem/', path)
end
end
end
// app/javascript/packs/application.js.erb
/* rails-erb-loader-dependencies ../lib/webpack_helper */
import "<%= WebpackHelper.my_gem_js('path-to-my-file.js') %>";
import "<%= WebpackHelper.my_gem_js('path-to-another-file.coffee') %>";
May not be much, but it helps me DRY things up and I can add more class methods for other gems I may need. Hope this helps!
from webpacker.
Hi, how to import assets from a gem for example such as https://github.com/estum/growlyflash? This gem adds both assets and some Ruby code. What do I set in resolved_paths without unpacking the gem into the vendor directory? Thanks.
from webpacker.
Is there are an easier way to import javascript rather than unpacking a gem?
from webpacker.
Hi, I maintain the cocoon gem. I am a bit confused, so maybe a silly question, but since webpacker coexists with the normal asset pipeline, why should a gem containing javascript "sprinkles" move those to npm where they make no sense?
from webpacker.
@guilleiguaran Is there anything available yet for this use case?
from webpacker.
Yes, that's another way so you could require cocoon gem JS into your sprockets application.js but I guess it might not make sense for people who don't want to use asset pipeline at all since that's totally possible with Webpacker.
@nathanvda Webpacker co-exists with asset pipeline but can also be used alone, since Rails also supports yarn for installing npm packages it would make sense to publish the JS counterpart to npm for future, since that's where everything is moving. I understand it's a bit more work though :) i.e. publishing two packages.
from webpacker.
Looks like the best would be convert gems to npm packages: #488
from webpacker.
I could not make any of these work, but (for Rails 6) the asset pipeline still works, so I did this
# config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( best_in_place.js )
and in my erb file
<%= javascript_include_tag 'best_in_place' %>
from webpacker.
I think others have shared this opinion here but just to extend this concept, Webpacker is designed to be a wrapper around Webpack, a tool widely used in the JavaScript community. The JavaScript community uses NPM to keep and distribute packages and has done so for several years now. In my opinion, people who've made Ruby gems which wrap JavaScript libraries should be encouraged to publish those libraries to NPM. Webpacker should embrace the future, and any behavior supporting Ruby wrappers should stay in Sprockets and not get ported over into Webpacker.
from webpacker.
In your case you can require action_cable
from npm: https://www.npmjs.com/package/actioncable
from webpacker.
I solved it using the idea of a separate pack file for gems.
In packs/gems.js.erb:
import {} from '<%= File.join(File.join(Gem.loaded_specs['best_in_place'].full_gem_path, 'lib', 'assets', 'javascripts', 'best_in_place.js')) %>'
import {} from '<%= File.join(File.join(Gem.loaded_specs['best_in_place'].full_gem_path, 'vendor', 'assets', 'javascripts', 'jquery.autosize.js')) %>'
The key one for me was the second import - without that I was getting autosize not found.
In packs/application.js:
import './gems.js.erb'
from webpacker.
yep, now always sit at npmjs.com.
Just remember, don't do ./bin/yarn clean
. It will make ignore file for all packages, which is fighting with actioncable.js.
from webpacker.
@foton You can send a PR to Cocoon to include a package.json, you can use this as example: ifad/data-confirm-modal#53
from webpacker.
I agree with @dmitry, My problem is because I'm work in a old project with old front end gems, and the version from theses gems dont exist in npm because is so old like 2014. In the last opition I will up this gems in npm packages.
from webpacker.
For anyone looking for context, the idea is to move all assets over to Webpacker, rather than having to maintain say, jQuery in both the asset pipeline and the package manager.
https://medium.com/@coorasse/goodbye-sprockets-welcome-webpacker-3-0-ff877fb8fa79
from webpacker.
Seems be a good solution @qwertme
from webpacker.
This setup worked for me. I made a few tweaks to get it doing what I needed:
https://github.com/projectblacklight/blacklight/wiki/Using-Webpacker-to-compile-javascript-assets
Feel free to hit me up if you're running into issues and want more details.
from webpacker.
From what I understand, the pre-Rails 6 behavior (with JavaScript assets being handled by Sprockets) was such that the assets pipeline could access any JavaScript files in a Gem's app/assets/javascript
directory (such as in this example).
Naively, it would seem that the Webpacker Gem could just solve this by making files under a Gem's app/javascript
directory available through the path that's used to search JavaScript assets. From the above discussion, which focused mainly on workarounds, it is not clear to me what (if anything) would speak against implementing such a solution.
from webpacker.
Thank you for the explanation @mcmire. As I am new to all this, I am definitely still missing a lot of context and grateful for all the bits of information I get.
I think any solution that forces a package to be maintained in two separate places (say as a Gem and as an NPM module) is something to be avoided. I think in the example case of libraries such as Bootstrap or Semantic UI, which combine CSS and JS, the problem is that Rails 6 uses two quite different solutions to serve JS and CSS assets. I hope this will be cleaned up at some point.
from webpacker.
@infogrind You can (and probably should, if you depend on easy to install & modern/up-to-date packages) use Webpacker for both JS and CSS assets.
from webpacker.
Thanks @cseelus , I wasn't aware this was possible but now I am 😀
I found this article, which does a nice job of explaining how to get it done in Rails 6, in case anyone else is interested.
from webpacker.
Related Issues (20)
- Webpacker 6 Disable Babel HOT 1
- Webpacker compilation takes more than 1.5 hours HOT 11
- How do I allow a package in node_modules to be processed by babel? HOT 1
- Remove addition of node_modules to Rails.application.config.assets.paths HOT 1
- V6 Status HOT 3
- Raise an error when javascript_pack_tag or stylesheet_pack_tag are called more than once HOT 1
- Regular Expression Denial of Service in postcss HOT 2
- Intermittent Webpacker.dev_server.running? behavior HOT 2
- Host for assets from a webpacker configuration using custom domains not being resolved HOT 2
- Cherry-pick "Nothing to do" logging update onto `5.x` HOT 1
- Rails 6 WebPacker is not calling JQuery inside views HOT 1
- Security Vulnerability in [email protected] HOT 2
- currently no loaders are configured to process this file HOT 1
- Incompatibility with Ruby 3.2.0 HOT 2
- Confusing gem description on RubyGems HOT 1
- Npm package for 5.4.4 was not released HOT 2
- class variable @@local_levels of ActiveSupport::Logger is overtaken by Logger HOT 2
- Trying to deploy but assets assets:precompile fail
- Security Vulnerability for postcss
- Postcss Security Vulnerability
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpacker.