opal-webpack-loader 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8032476092204172bd9c607147b7cb05eaf3859f17926d1e09bd9853b4971045
4
- data.tar.gz: 252dff9803283436c074fe997e6ae2dec0f32d6ba16d1977ad5200b92edd81eb
3
+ metadata.gz: 7ff857f9dabc00c823531938b2f98e272e3116a74e7403d963c8e40d0f8c0ec6
4
+ data.tar.gz: 1661cdfe7ee58d845011790bc4f46d4d6f95a4bee2c7a93dab24c62f4099dc47
5
5
  SHA512:
6
- metadata.gz: b73cf6b3b946ffaed75e1b684bbd3dff266bc1fde599132fcb8f2992b37a44836cf5ac80a1a9efbc35dcc9332801dbc6ae8067628f585457e043aaabc86647f5
7
- data.tar.gz: b844a167b78695aaa20ae7f0e11a409c6e0f67a8de7e78aa619df4c51b0f85a25e573550ec071a43465cb2842109a076a8c263421403512b0f03b69ded94d9cf
6
+ metadata.gz: 92adebf25952466c0ffe4836567cab2a7af9e839d7e7919faeec77d2eff6c42575da98aebc396977009536202f9184041ca4d625aa57801d55091a06f1d2b416
7
+ data.tar.gz: 248f4a25ac2a60092715be545cfa7a80360c21fd624744eaab09eb7af8f7e04624159a87b08be5f6bb61c76d2b2bf43d1fe10825d081ceecc3f94772d02e25d0
@@ -0,0 +1,29 @@
1
+ # check version of opal-webpack-loader-npm
2
+ require 'opal-webpack-loader/version'
3
+ require 'opal-webpack-loader/manifest'
4
+ if defined? Rails
5
+ require 'opal-webpack-loader/rails_view_helper'
6
+ else
7
+ require 'opal-webpack-loader/view_helper'
8
+ end
9
+
10
+ owl_version = ''
11
+ npm = `which npm`.chop
12
+
13
+ if npm != ''
14
+ owl_version = `#{npm} view opal-webpack-loader version`
15
+ else
16
+ yarn = `which yarn`.chop
17
+ if yarn != ''
18
+ owl_version = `#{yarn} -s info opal-webpack-loader version`
19
+ else
20
+ raise 'opal-webpack-loader: Could not find npm or yarn! Please install npm or yarn'
21
+ end
22
+ end
23
+
24
+ unless owl_version.chop != OpalWebpackLoader::VERSION
25
+ raise "opal-webpack-loader: Incorrect version of npm package found or npm package not installed.\n" +
26
+ "Please install the npm package for opal-webpack-loader:\n" +
27
+ "\twith npm:\tnpm install opal-webpack-loader@#{OpalWebpackLoader::VERSION} --save-dev\n" +
28
+ "\twith yarn:\tyarn add opal-webpack-loader@#{OpalWebpackLoader::VERSION} --dev\n"
29
+ end
@@ -0,0 +1,156 @@
1
+ require 'oj'
2
+ require 'eventmachine'
3
+ require 'opal/paths'
4
+ require 'opal/source_map'
5
+ require 'source_map'
6
+ require 'opal/compiler'
7
+ require 'socket'
8
+
9
+ at_exit do
10
+ if OpalWebpackCompileServer::Exe.unlink_socket?
11
+ if File.exist?(OpalWebpackCompileServer::OWCS_SOCKET_PATH)
12
+ File.unlink(OpalWebpackCompileServer::OWCS_SOCKET_PATH)
13
+ end
14
+ end
15
+ end
16
+
17
+ module OpalWebpackCompileServer
18
+ OWL_CACHE_DIR = './.owl_cache/'
19
+ OWL_LP_CACHE = OWL_CACHE_DIR + 'load_paths.json'
20
+ OWCS_SOCKET_PATH = OWL_CACHE_DIR + 'owcs_socket'
21
+
22
+ class Compiler < EventMachine::Connection
23
+ def receive_data(data)
24
+ if data.start_with?('command:stop')
25
+ EventMachine.stop
26
+ exit(0)
27
+ end
28
+
29
+ filename = data.chop # remove newline
30
+
31
+ operation = proc do
32
+ begin
33
+ source = File.read(filename)
34
+ c = Opal::Compiler.new(source, file: filename, es6_modules: true)
35
+ c.compile
36
+ result = { 'javascript' => c.result }
37
+ result['source_map'] = c.source_map.as_json
38
+ result['source_map']['sourcesContent'] = [source]
39
+ result['source_map']['file'] = filename
40
+ result['source_map']['names'] = result['source_map']['names'].map(&:to_s)
41
+ result['required_trees'] = c.required_trees
42
+ Oj.dump(result)
43
+ rescue Exception => e
44
+ Oj.dump({ 'error' => { 'name' => e.class, 'message' => e.message, 'backtrace' => e.backtrace } })
45
+ end
46
+ end
47
+
48
+ callback = proc do |json|
49
+ self.send_data(json + "\n")
50
+ close_connection_after_writing
51
+ end
52
+
53
+ EM.defer(operation, callback)
54
+ end
55
+ end
56
+
57
+ class LoadPathManager
58
+ def self.get_load_path_entries(path)
59
+ path_entries = []
60
+ return [] unless Dir.exist?(path)
61
+ dir_entries = Dir.entries(path)
62
+ dir_entries.each do |entry|
63
+ next if entry == '.'
64
+ next if entry == '..'
65
+ absolute_path = File.join(path, entry)
66
+ if File.directory?(absolute_path)
67
+ more_path_entries = get_load_path_entries(absolute_path)
68
+ path_entries.push(*more_path_entries) if more_path_entries.size > 0
69
+ elsif (absolute_path.end_with?('.rb') || absolute_path.end_with?('.js')) && File.file?(absolute_path)
70
+ path_entries.push(absolute_path)
71
+ end
72
+ end
73
+ path_entries
74
+ end
75
+
76
+ def self.get_load_paths
77
+ load_paths = if File.exist?('bin/rails')
78
+ %x{
79
+ bundle exec rails runner "puts (Rails.configuration.respond_to?(:assets) ? (Rails.configuration.assets.paths + Opal.paths).uniq : Opal.paths)"
80
+ }
81
+ else
82
+ %x{
83
+ bundle exec ruby -e 'require "bundler/setup"; Bundler.require; puts Opal.paths'
84
+ }
85
+ end
86
+ if $? == 0
87
+ load_path_lines = load_paths.split("\n")
88
+ load_path_lines.pop if load_path_lines.last == ''
89
+
90
+ load_path_entries = []
91
+
92
+ cwd = Dir.pwd
93
+
94
+ load_path_lines.each do |path|
95
+ next if path.start_with?(cwd)
96
+ more_path_entries = get_load_path_entries(path)
97
+ load_path_entries.push(*more_path_entries) if more_path_entries.size > 0
98
+ end
99
+ cache_obj = { 'opal_load_paths' => load_path_lines, 'opal_load_path_entries' => load_path_entries }
100
+ Dir.mkdir(OpalWebpackCompileServer::OWL_CACHE_DIR) unless Dir.exist?(OpalWebpackCompileServer::OWL_CACHE_DIR)
101
+ File.write(OpalWebpackCompileServer::OWL_LP_CACHE, Oj.dump(cache_obj))
102
+ load_path_lines
103
+ else
104
+ raise 'Error getting load paths!'
105
+ exit(2)
106
+ end
107
+ end
108
+ end
109
+
110
+ class Exe
111
+ def self.unlink_socket?
112
+ @unlink
113
+ end
114
+
115
+ def self.unlink_on_exit
116
+ @unlink = true
117
+ end
118
+
119
+ def self.dont_unlink_on_exit
120
+ @unlink = false
121
+ end
122
+
123
+ def self.stop
124
+ if File.exist?(OWCS_SOCKET_PATH)
125
+ dont_unlink_on_exit
126
+ begin
127
+ s = UNIXSocket.new(OWCS_SOCKET_PATH)
128
+ s.send("command:stop\n", 0)
129
+ s.close
130
+ rescue
131
+ # socket cant be reached so owcs is already dead, delete socket
132
+ unlink_on_exit
133
+ end
134
+ exit(0)
135
+ end
136
+ end
137
+
138
+ def self.run
139
+ if File.exist?(OWCS_SOCKET_PATH) # OWCS already running
140
+ puts 'Another Opal Webpack Compile Server already running, exiting'
141
+ dont_unlink_on_exit
142
+ exit(1)
143
+ else
144
+ unlink_on_exit
145
+ load_paths = OpalWebpackCompileServer::LoadPathManager.get_load_paths
146
+ if load_paths
147
+ Opal.append_paths(*load_paths)
148
+ Process.daemon(true)
149
+ EventMachine.run do
150
+ EventMachine.start_unix_domain_server(OWCS_SOCKET_PATH, OpalWebpackCompileServer::Compiler)
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,11 @@
1
+ module OpalWebpackLoader
2
+ class Manifest
3
+ def self.manifest
4
+ @manifest ||= JSON.parse(File.read(File.join(Rails.root, 'public', 'packs', 'manifest.json')))
5
+ end
6
+
7
+ def self.lookup_path_for(asset)
8
+ manifest[asset]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module OpalWebpackLoader
2
+ module RailsViewHelper
3
+ def owl_include_tag(path)
4
+ case Rails.env
5
+ when 'production'
6
+ public, packs, asset = path.split('/')
7
+ asset_path = OpalWebpackLoader::Manifest.lookup_path_for(asset)
8
+ javascript_include_tag(asset_path)
9
+ when 'development'
10
+ javascript_include_tag('http://localhost:3035' + path[0..-4] + '_development' + path[-3..-1])
11
+ when 'test'
12
+ real_path = path[0..-4] + '_test' + path[-3..-1]
13
+ public, packs, asset = real_path.split('/')
14
+ asset_path = OpalWebpackLoader::Manifest.lookup_path_for(asset)
15
+ javascript_include_tag(asset_path)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module OpalWebpackLoader
2
+ VERSION="0.3.3"
3
+ end
@@ -0,0 +1,19 @@
1
+ module OpalWebpackLoader
2
+ module RailsViewHelper
3
+ def owl_include_tag(path)
4
+ case Rails.env
5
+ when 'production'
6
+ public, packs, asset = path.split('/')
7
+ asset_path = OpalWebpackLoader::Manifest.lookup_path_for(asset)
8
+ "<script type=\"application/javascript\" src=\"#{asset_path}\"></script>"
9
+ when 'development'
10
+ "<script type=\"application/javascript\" src=\"#{'http://localhost:3035' + path[0..-4] + '_development' + path[-3..-1]}\"></script>"
11
+ when 'test'
12
+ real_path = path[0..-4] + '_test' + path[-3..-1]
13
+ public, packs, asset = real_path.split('/')
14
+ asset_path = OpalWebpackLoader::Manifest.lookup_path_for(asset)
15
+ "<script type=\"application/javascript\" src=\"#{asset_path}\"></script>"
16
+ end
17
+ end
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-webpack-loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
@@ -75,6 +75,12 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - bin/opal-webpack-compile-server
78
+ - lib/opal-webpack-loader.rb
79
+ - lib/opal-webpack-loader/compile_server.rb
80
+ - lib/opal-webpack-loader/manifest.rb
81
+ - lib/opal-webpack-loader/rails_view_helper.rb
82
+ - lib/opal-webpack-loader/version.rb
83
+ - lib/opal-webpack-loader/view_helper.rb
78
84
  homepage: http://hyperstack.org
79
85
  licenses:
80
86
  - MIT