onload-transformer 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc48aa1c187e9a812f198ff52b38cc0206f45749
4
+ data.tar.gz: 289f674f9f154994bdf625c8f50de266fc541319
5
+ SHA512:
6
+ metadata.gz: 951abd2dfece8bbb097f77143d5194f25919651ff889f128d2b55a642eccd80b64030346cc57ce58807377360956f8e9335cdf6d9cda0794600f9721753cc325
7
+ data.tar.gz: 5a0086a6edd3e315e29c24d098dcede4330923f2c95a80f8516b215fdb1e60cd3c84226a98a6bd95e15fbd5efccb5303562d36327d009ec5563b859cdc0df59b
@@ -0,0 +1,3 @@
1
+ tmp/
2
+ tags
3
+ .ruby-version
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2019 Jérémie Bonal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # Onload Transformer for Sprockets
2
+
3
+ ## What?
4
+
5
+ It's a small transformer for Sprockets that allows you to make a JS file wait for the document's `DOMContentLoaded` instead of running right when read.
6
+
7
+ ## Why?
8
+
9
+ Because vendors sometimes provide minified JS that's meant to be copy-pasted at the end of your page, but you don't want to do that because of turbolinks, personal conviction or other, but if you serve it through your asset pipeline you end up running it before your DOM is ready.
10
+ Or you can use it to never write a `$(document).ready` again in your life I guess? I'm not the boss of you, use it for whatever you find it useful for.
11
+
12
+ ## How? (does it work)
13
+
14
+ It just wraps the whole file in a function and then binds it to the `DOMContentLoaded` event (with the proper check beforehand in case the DOM is already loaded).
15
+
16
+ ## How? (do i use it)
17
+
18
+ - Add the gem to your gemfile.
19
+ - Use the extension .onload.js instead of .js for your files.
20
+
21
+ ## What? (can be improved)
22
+
23
+ Probably a lot of stuff, but at least:
24
+
25
+ [ ] - The functions used to wrap the body of the file are in the global namespace. It would be better if they were defined on a transformer-only object.
26
+ [ ] - Make it so it can be used with .js.erb / .js.coffee (maybe ?)
27
+
@@ -0,0 +1 @@
1
+ require 'sprockets/onload_transformer'
@@ -0,0 +1,42 @@
1
+ require 'sprockets'
2
+
3
+ module Sprockets
4
+ class OnloadTransformer
5
+ def self.call(input)
6
+ asset_name = input[:name]
7
+ body = input[:data]
8
+ util_function_name = "onloadTransformerUtil#{to_function_key(asset_name)}"
9
+ wrapped_in_event = <<-JS
10
+ function #{util_function_name}() {
11
+ #{body}
12
+ };
13
+
14
+ if (document.readyState === "loading") {
15
+ document.addEventListener("DOMContentLoaded", #{util_function_name});
16
+ } else {
17
+ #{util_function_name}();
18
+ };
19
+ JS
20
+
21
+ { data: wrapped_in_event }
22
+ end
23
+
24
+ def self.to_function_key(key)
25
+ key.parameterize.underscore.camelize
26
+ end
27
+ end
28
+
29
+ if respond_to?(:register_transformer)
30
+ register_mime_type 'application/onload-javascript', extensions: ['.onload.js'], charset: :unicode
31
+ register_transformer 'application/onload-javascript', 'application/javascript', OnloadTransformer
32
+ register_preprocessor 'application/onload-javascript', DirectiveProcessor
33
+ end
34
+
35
+ if respond_to?(:register_engine)
36
+ args = ['.onload.js', OnloadTransformer]
37
+ if Sprockets::VERSION.start_with?("3")
38
+ args << { mime_type: 'application/javascript', silence_deprecation: true }
39
+ end
40
+ register_engine(*args)
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module Sprockets
2
+ class OnloadTransformer
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+ require 'sprockets/onload_transformer/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "onload-transformer"
6
+ s.authors = ["Jérémie Bonal"]
7
+ s.email = ["jeremie.bonal@gmail.com"]
8
+ s.version = Sprockets::OnloadTransformer::VERSION
9
+ s.summary = "A short extension of Sprockets to wrap JS libs so they wait for DOMContentLoaded"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = ["lib"]
12
+ s.license = "MIT"
13
+ s.homepage = "http://github.com/Aquaj/onload-transformer"
14
+
15
+ s.add_dependency 'sprockets', '>= 2.0', '<= 4'
16
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onload-transformer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jérémie Bonal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '4'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
33
+ description:
34
+ email:
35
+ - jeremie.bonal@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".gitignore"
41
+ - LICENSE
42
+ - README.md
43
+ - lib/onload-transformer.rb
44
+ - lib/sprockets/onload_transformer.rb
45
+ - lib/sprockets/onload_transformer/version.rb
46
+ - onload-transformer.gemspec
47
+ homepage: http://github.com/Aquaj/onload-transformer
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.5.2.2
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A short extension of Sprockets to wrap JS libs so they wait for DOMContentLoaded
71
+ test_files: []