Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit a376fc7

Browse files
committed
closes #17
1 parent 502c659 commit a376fc7

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

lib/hyperloop-config.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
require 'hyperloop/string'
33
require 'hyperloop/client_stubs'
44
require 'hyperloop/context'
5+
require 'hyperloop/js_imports'
56
require 'hyperloop/on_client'
67
require 'hyperloop/active_support_string_inquirer.rb'
78
require 'hyperloop_env'
89
else
910
require 'opal'
1011
require 'opal-browser'
11-
require 'opal-rails' if defined? Rails
12+
begin
13+
require 'opal-rails' if defined? Rails
14+
rescue LoadError
15+
end
1216
require 'hyperloop/config_settings'
1317
require 'hyperloop/context'
1418
require 'hyperloop/imports'
19+
require 'hyperloop/js_imports'
1520
require 'hyperloop/client_readers'
1621
require 'hyperloop/on_client'
1722
require 'hyperloop/rail_tie' if defined? Rails

lib/hyperloop/imports.rb

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ def import_list
55
@import_list ||= []
66
end
77

8-
def import(value, gem: nil, cancelled: nil, client_only: nil, server_only: nil, tree: nil)
8+
def import(value, gem: nil, cancelled: nil, client_only: nil, server_only: nil, tree: nil, js_import: nil)
99
unless import_list.detect { |current_value, *_rest| value == current_value }
10-
import_list << [value, cancelled, !client_only, !server_only, (tree ? :tree : :gem)]
10+
import_list << [value, cancelled, !client_only, !server_only, (tree ? :tree : :gem), js_import]
1111
end
1212
end
1313

@@ -18,15 +18,32 @@ def import_tree(value, cancelled: nil, client_only: nil, server_only: nil)
1818
end
1919

2020
def cancel_import(value)
21+
return unless value
2122
current_spec = import_list.detect { |old_value, *_rest| value == old_value }
2223
if current_spec
23-
current_spec[1] = true
24+
current_spec[1] = true
2425
else
2526
import_list << [value, true, true, true, false]
2627
end
2728
end
2829

30+
def cancel_webpack_imports
31+
import_list.collect { |name, _c, _co, _so, _t, js_import, *_rest| js_import && name }
32+
.each { |name| cancel_import name }
33+
end
34+
35+
def handle_webpack
36+
return unless defined? Webpacker
37+
client_only_manifest = Webpacker.manifest.lookup("client_only.js")
38+
client_and_server_manifest = Webpacker.manifest.lookup("client_and_server.js")
39+
return unless client_only_manifest || client_and_server_manifest
40+
cancel_webpack_imports
41+
import client_only_manifest.split("/").last, client_only: true if client_only_manifest
42+
import client_and_server_manifest.split("/").last if client_and_server_manifest
43+
end
44+
2945
def generate_requires(mode, sys, file)
46+
handle_webpack
3047
import_list.collect do |value, cancelled, render_on_server, render_on_client, kind|
3148
next if cancelled
3249
next if (sys && kind == :tree) || (!sys && kind != :tree)

lib/hyperloop/js_imports.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Hyperloop
2+
class << self
3+
def js_import(value, client_only: nil, server_only: nil, defines:)
4+
defines = [*defines]
5+
if RUBY_ENGINE != 'opal'
6+
import(value, client_only: client_only, server_only: server_only, js_import: true)
7+
else
8+
on_server = `typeof Opal.global.document === 'undefined'`
9+
return if (server_only && !on_server) || (client_only && on_server)
10+
defines.each do |name|
11+
next unless `Opal.global[#{name}] === undefined`
12+
raise "The package #{name} was not found. Add it to the webpack "\
13+
"#{client_only ? 'client_only.js' : 'client_and_server.js'} manifest."
14+
end
15+
end
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)