Home

Awesome

<h1 align="center"> <img src="https://raw.githubusercontent.com/isomorfeus/opal-webpack-loader/master/docs/owl.png" align="center" title="Opal logo by Elia Schito combined with Webpack Logo" width="111" height="125" /> <br/> opal-webpack-loader<br/> <img src="https://img.shields.io/badge/Opal-Ruby%20💛%20JavaScript%20💛%20Webpack-yellow.svg?logo=ruby&style=social&logoColor=777"/> </h1>

Bundle assets with webpack, resolve and compile opal ruby files and import them in the bundle, without sprockets or the webpacker gem (but can be used with both of them too). Includes a loader and resolver plugin for webpack.

Community and Support

At the Isomorfeus Framework Project

Tested

TravisCI: Build Status

Features

Requirements

Installation

Using the installer

First install the gem:

gem install 'opal-webpack-loader'

Continue here:

Example applications

General Usage without Webpacker

After installing owl with the installer, three scripts are provided in package.json:

These scripts can for example be run with: yarn run debug or npm run debug

The default config provides several targets and entries:

Only the browser target is build by default. To builds the other target, just add the needed targets to the last line of the webpack config, for example to development.js: default config:

module.exports = [ browser ];

modified config with ssr and web_worker targets enabled:

module.exports = [ browser, ssr, web_worker ];

Same works for the debug.js and production.js webpack config files.

Also a Procfile has been installed, for rails its easy to startup rails and webpack with foreman: foreman start (gem install foreman if you dont have it already). It will start rails and webpack-dev-server with the development script.

For non rails installation check the Procfile and add a starter for your app.

Opal Ruby Application Files

For rails installations with the installer they all go into: app/opal, for flat installations in the opal directory. In this directory there already is a opal_loader.rb which is the entry point for your app.

Stylesheets

Stylesheets are hot reloaded too with the default config installed by the installer. Also they are imported into application.js by default. For rails like applications stylesheets are in app/assets/stylesheets/application.css, for flat applications they are in styles/application.css. SCSS is supported too by the default config.

Views

For rails like applications a watcher for app/views is installed by default. The watcher will trigger a page reload when views are changed. For flat applications nothing is configured by default, as there are to many ways to generate views, they are not even needed with frameworks like isomorfeus. Instead the section for configuring a view watcher is included in the development.js and debug.js webpack config, but it is commented out. Please see those files and adjust to your liking.

Parallel compilation for speed

Since version 0.8.0 the number of CPUs is automatically determined and a appropriate number of of compile server workers is started automatically.

Source Maps

Source Maps

Hot Module Reloading

Hot Module Reloading

Opal Load Path

The projects directory for opal ruby files must be in the opal load path. This is done in the initializer for rails apps in config/initializers/opal_webpack_loader.rb or in 'owl_init.rb' for non rails apps, for example:

Opal.append_path(File.realdirpath('app/opal'))

View Helper

In Rails or frameworks that support javscript_include_tag, add to the app/helpers/application_helper.rb

module ApplicationHelper
  include OpalWebpackLoader::RailsViewHelper

in other frameworks that dont have a javascript_include_tag:

module MyProjectsViewThings
  include OpalWebpackLoader::ViewHelper

Then you can use in your views:

owl_script_tag('application.js')

Compile Server and app_loader.rb

For non rails projects, determining Opal load paths, for the resolver and compile server to work properly, may not be obvious. For these cases a file app_loader.rb in the projects root can be created which just loads all requirements without starting anything. Usually it would just setup bundler with the appropriate options, for example:

require 'bundler/setup'
if ENV['RACK_ENV'] && ENV['RACK_ENV'] == 'test'
  Bundler.require(:default, :test)
elsif ENV['RACK_ENV'] && ENV['RACK_ENV'] == 'production'
  Bundler.require(:default, :production)
else
  Bundler.require(:default, :development)
end
Opal.append_path(File.realdirpath('opal')) # this is a good place to add the directory with opal files to the opal load path

When this file exists, the compile server will load it and generate Opal load paths accordingly for the resolver.

Project configuration options for the view helper

These setting are in the initializer in config/initializers/opal_webpack_loader.rb for rails like apps, or owl_init.rb for others.

OpalWebpackLoader.use_manifest = false

If the manifest file should be used, use_manifest should be true.

OpalWebpackLoader.manifest_path = File.join(Dir.getwd, 'public', 'assets', 'manifest.json')

Sets the path to the webpack (with the webpack-manifest-plugin) generated manifest.json to look up assets.

OpalWebpackLoader.client_asset_path = 'http://localhost:3035/assets/'

The path to prepend to the assets as configured in the webpack config 'publicPath'. In the config example below its publicPath: 'http://localhost:3025/assets' so client_asset_path should be set to the same.

For production use with readily precompiled and compressed assets which contain a fingerprint in the name (webpacks [chunkhash]), and if the path in the manifest is the full path to the asset as configured in webpack, these settings would work:

OpalWebpackLoader.use_manifest = true
OpalWebpackLoader.manifest_path = File.join(Dir.getwd, 'public', 'assets', 'manifest.json')
OpalWebpackLoader.client_asset_path = ''

For development use with webpack-dev-server, with no manifest, these settings would work:

OpalWebpackLoader.use_manifest = false
OpalWebpackLoader.manifest_path = File.join(Dir.getwd, 'public', 'assets', 'manifest.json') # doesn't matter, not used
OpalWebpackLoader.client_asset_path = 'http://localhost:3035/assets/'

Advanced Options

Advanced Options

Tests