All Projects → the-teacher → GemsAssetsWebpackBridge

the-teacher / GemsAssetsWebpackBridge

Licence: MIT License
It helps to build a bridge from ruby gems' assets to Webpack

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to GemsAssetsWebpackBridge

Webpack Cdn Plugin
A webpack plugin that use externals of CDN urls for production and local node_modules for development
Stars: ✭ 306 (+1430%)
Mutual labels:  assets, webpack3
js-namespace-rails
js-namespace-rails let you choose which javascript snippet can execute in rails assets pipeline
Stars: ✭ 57 (+185%)
Mutual labels:  assets
gems
Ruby Football Week 2021, June 11th to June 17th - 7 Days of Ruby (Sports) Gems ++ Best of Ruby Gems Series
Stars: ✭ 76 (+280%)
Mutual labels:  gems
opendev
OpenDev is a non-profit project that tries to collect as many resources (assets) of free use for the development of video games and applications.
Stars: ✭ 34 (+70%)
Mutual labels:  assets
imitate-beautiful-thing
使用vue 做的一个h5 app。Using Vue to imitate a app.
Stars: ✭ 56 (+180%)
Mutual labels:  webpack3
Stamp-Craft
Plugin for adding timestamp to filenames.
Stars: ✭ 28 (+40%)
Mutual labels:  assets
react-boilerplate
Step by Step React Boilerplate
Stars: ✭ 35 (+75%)
Mutual labels:  webpack3
frontend-boilerplate
Boilerplate to create powerful frontend applications
Stars: ✭ 17 (-15%)
Mutual labels:  webpack3
react16-seed-with-apollo-graphql-scss-router4-ssr-tests-eslint-prettier-docker-webpack3-hot
Seed to create your own project using React with Apollo GraphQL client
Stars: ✭ 19 (-5%)
Mutual labels:  webpack3
fingerprint-brunch
A brunch plugin for cache busting assets
Stars: ✭ 15 (-25%)
Mutual labels:  assets
phaser-webpack-loader
Asset loader for Phaser + Webpack.
Stars: ✭ 85 (+325%)
Mutual labels:  assets
ssr-starter-pack
Moved to https://github.com/Brigad/ssr-starter-pack
Stars: ✭ 12 (-40%)
Mutual labels:  webpack3
books
List of all Ruby books
Stars: ✭ 49 (+145%)
Mutual labels:  gems
boss-lite
Boss Lite - React Redux Material Admin Template
Stars: ✭ 148 (+640%)
Mutual labels:  webpack3
Assets
WordPress assets helpers
Stars: ✭ 20 (+0%)
Mutual labels:  assets
Unity-delayed-asset
**DEPRECATED** Plugin for Unity that allows to assign assets in the inspector while preventing Unity from automatically loading the assets in memory when a scene is loaded
Stars: ✭ 19 (-5%)
Mutual labels:  assets
disclosedassets
DEPRECATED Allow your clients to find asset sub-folders by disclosing them by default
Stars: ✭ 13 (-35%)
Mutual labels:  assets
django-webpack-starter
Django Webpack starter template for using Webpack 5 with Django 3.1 & Bootstrap 4. Yes, it can hot-reload.
Stars: ✭ 52 (+160%)
Mutual labels:  assets
assetter
Assets manager for PHP. Allow manage CSS and JS files in website and its dependencies. Also allows refresh cache in browsers by adding revisions of loaded files. Builtin plugins to compile SASS and/or LESS styles to CSS just-in-time.
Stars: ✭ 14 (-30%)
Mutual labels:  assets
postcss-copy
An async postcss plugin to copy all assets referenced in CSS files to a custom destination folder and updating the URLs.
Stars: ✭ 39 (+95%)
Mutual labels:  assets

GemsAssetsWebpackBridge

This Rails gem helps to build a bridge from assets in your ruby gems to Webpack.

pexels-photo-208684

The Problem

  1. I had a classic monolith Rails application with Sprockets.
  2. I've split my Rails app into a few Rails gems and moved my assets into gems.
  3. I started migration from Sprockets to Webpack.
  4. Webpack didn't know how to find my assets in Rails gems.

The Solution

This gem helps to collect paths to assets' folders in gems and adds some aliases for Webpack to help it to find assets in classic Rails gems.

Installation

Gemfile

gem 'gems_assets_webpack_bridge'
$ bundle

Usage

Build a bridge between Rails gems and Webpack:

rake gems_asstes_webpack_bridge:build

What is The Bridge?

  • The Bridge is a JSON file with paths to assets in your gems.
  • The Bridge provides additional aliases for Webpack to help it to find assets' folders in ruby gems.
  • Every alias has the following naming: @[UNDERSCORED_GEM_NAME]-[ASSET_TYPE].
  • Default asset types are: images, scripts, styles.
  • By default the file has name gems-assets-webpack-bridge.json and looks like that:
{
  "@crop_tool-scripts": "/Users/@username/.rvm/gems/ruby-2.4.2/gems/crop_tool/app/assets/javascripts",
  "@crop_tool-styles": "/Users/@username/.rvm/gems/ruby-2.4.2/gems/crop_tool/app/assets/stylesheets",
  "@jquery_ui_rails-images": "/Users/@username/.rvm/gems/[email protected]/gems/jquery-ui-rails-5.0.0/app/assets/images",
  "@jquery_ui_rails-scripts": "/Users/@username/.rvm/gems/[email protected]/gems/jquery-ui-rails-5.0.0/app/assets/javascripts",
  "@jquery_ui_rails-styles": "/Users/@username/.rvm/gems/[email protected]/gems/jquery-ui-rails-5.0.0/app/assets/stylesheets",
  "@log_js-scripts": "/Users/@username/.rvm/gems/ruby-2.4.2/gems/log_js/app/assets/javascripts",
  "@notifications-scripts": "/Users/@username/.rvm/gems/ruby-2.4.2/gems/notifications/app/assets/javascripts",
  "@notifications-styles": "/Users/@username/.rvm/gems/ruby-2.4.2/gems/notifications/app/assets/stylesheets",
}

How to use The Bridge?

Now, when you have the bridge file, you can use it in Webpack like this:

webpack.config.js

// This helper-function reads the bridge file and
// adds some additional aliases in `WebPackConfig.resolve.alias`
function addGemsAssetsWebpackBridge (WebPackConfig) {
  const alias = WebPackConfig.resolve.alias
  const bridgeFile = `${rootPath}/gems-assets-webpack-bridge.json`

  var bridgeAlias = JSON.parse(fs.readFileSync(bridgeFile, 'utf8'))
  return Object.assign(alias, bridgeAlias)
}

let WebpackConfig = {
  entry: {
    ...
  },

  output: {
    ...
  },

  resolve : {
    alias: {
      '@vendors': `${rootPath}/assets/vendors`
    }
  }
}

WebPackConfig.resolve.alias = addGemsAssetsWebpackBridge(WebPackConfig)
module.exports = WebPackConfig

After you added the aliases you can use them in your Webpack entries like this:

require('@log_js-scripts/log_js')
require('@notifications-scripts/notifications')
require('@the_comments-scripts/the_comments')

require('@notifications-styles/notifications')
require('@the_comments-styles/the_comments')

How can I configure this gem?

You can create a new initializer in your Rails app change options:

config/initializers/gems_assets_webpack_bridge.rb

GemsAssetsWebpackBridge.configure do |config|
  # From a root of your Rails app
  config.bridge_path = '/configs/gems-webpack-bridge.json'

  #  From a root of every Rails gem
  config.assets_types = {
    # Default values
    images: 'app/assets/images',
    scripts: 'app/assets/javascripts',
    styles: 'app/assets/stylesheets',

    # Your custom values
    vendor_select2_images: 'vendors/select2/images',
  }
end

Should I add the bridge file in my VCS?

No. Paths will be different for different users. Just add gems-assets-webpack-bridge.json in your .gitignore and run rake gems_asstes_webpack_bridge:build when you need it.

Should I change my deployment process?

You have to run rake gems_asstes_webpack_bridge:build before every Webpack compilation during deployment. Otherwise Webpack will know nothing about aliases and a compilation won't work.

License

The gem is available as open source under the terms of the MIT License.

Bridge image @ pexels.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].