webpack_integration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8137d2b6f8d3b7eb9d0a579a568b8acd9d37b95
4
+ data.tar.gz: dd62598a0b54960ca81d18c367e871f1ccfd039c
5
+ SHA512:
6
+ metadata.gz: 0fd05c93fbb4e2da52ad6e947a9ee52766ea95f9eedbf80b1c417baf1b0f7870e76846768a77c693060a302be2b3421fa879c448b95ca8771180d62addf6e642
7
+ data.tar.gz: 5e4039d724bf2cc8c0612c0cb97bb9ed90188f5f781eda018e73720e6006fe141c677fd0e1aada626caf8ee29073b88e0ac6adf700377b993f1b9a7df1ed9df4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+
5
+ before_install:
6
+ - gem update --system
7
+ - gem --version
8
+ - gem install bundler
9
+
10
+ script: sh/test
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webpack_integration.gemspec
4
+ gemspec
5
+
6
+ gem 'codecov', :require => false, :group => :test
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Roman Heinrich
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # WebpackIntegration
2
+
3
+ [![Build Status](https://travis-ci.org/mindreframer/webpack_integration.svg?branch=master)](http://travis-ci.org/mindreframer/webpack_integration)
4
+ [![codecov.io](https://codecov.io/github/mindreframer/webpack_integration/coverage.svg?branch=master)](https://codecov.io/github/mindreframer/webpack_integration?branch=master)
5
+
6
+ A small gem for dead simple integration with any static assets compiler.
7
+ It allows you to reference any file in a output folder for a compiler (webpack / gulp / grunt) that may have a hash digest in the file name by simple non-changeable filename part.
8
+
9
+ Example:
10
+
11
+ public/webpack/reports_view-bundle-ab530a1.js
12
+ public/webpack/reports_view_styles-bundle-b94f982.js
13
+ public/webpack/common-2a55c40.js
14
+ public/webpack/invoices_view-bundle-e881f50.js
15
+ public/webpack/invoices_view_styles-bundle-0ebe2ec.js
16
+
17
+
18
+ Refer to files in Rails views by:
19
+
20
+ <%= javascript_include_tag webpack_file('reports_view_bundle') %>
21
+ <%= javascript_include_tag webpack_file('reports_view_styles-bundle') %>
22
+
23
+
24
+ Or anywhere in code by:
25
+
26
+ # fuzzy, forgiving matching
27
+ WebpackIntergration.fuzzy_file_for(filename)
28
+
29
+ # exact file name without the digest
30
+ WebpackIntergration.file_for(filename)
31
+
32
+
33
+ ## Installation
34
+
35
+ # add to your application's Gemfile:
36
+ gem 'webpack_integration'
37
+
38
+ # run
39
+ $ bundle
40
+
41
+ ## Usage
42
+
43
+ # in config/initializers/webpack_integration.rb
44
+ WebpackIntegration.configure do |config|
45
+ config.public_folder = 'public' # default 'public'
46
+ config.folder_in_public = 'webpack' # default 'webpack'
47
+ config.reset_on_each_request = Rails.env.development? # default false
48
+ end
49
+
50
+
51
+ ## Development
52
+ # run tests
53
+ $ sh/test
54
+
55
+ ## Todo
56
+ - make reloading more efficient, maybe in a background process.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/webpack_integration/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,31 @@
1
+ require "webpack_integration/version"
2
+ require "webpack_integration/configuration"
3
+ require "webpack_integration/assets"
4
+ require "webpack_integration/store"
5
+ require "webpack_integration/railtie" if defined?(Rails)
6
+
7
+
8
+ module WebpackIntegration
9
+ def self.configure
10
+ yield(configuration)
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= WebpackIntegration::Configuration.new
15
+ end
16
+
17
+ # @example
18
+ # WebpackIntergration.file_for('clients.css')
19
+ def self.file_for(filename)
20
+ WebpackIntegration::Store.file_for(filename)
21
+ end
22
+
23
+ def self.fuzzy_file_for(file_pattern)
24
+ WebpackIntegration::Store.fuzzy_file_for(file_pattern)
25
+ end
26
+
27
+ # should be called in development mode in after-request filter
28
+ def self.reset_assets_manifest!
29
+ WebpackIntegration::Store.reset
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ module WebpackIntegration
2
+ class Assets
3
+ def self.generate_assets_manifest
4
+ {}.tap do |res|
5
+ Dir[file_matcher].each do |f|
6
+ next if File.directory?(f)
7
+ key = f.gsub(webpack_folder, '')
8
+ key = key.gsub(/-[a-f\d]*(\.)/, '.') # remove md5 hash
9
+ value = f.sub(public_folder, '')
10
+ res[key] = value
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.file_matcher
16
+ File.join(configuration.full_path, '**/**/**/**')
17
+ end
18
+
19
+ def self.webpack_folder
20
+ configuration.full_path
21
+ end
22
+
23
+ def self.public_folder
24
+ configuration.public_folder
25
+ end
26
+
27
+ def self.configuration
28
+ WebpackIntegration.configuration
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module WebpackIntegration
2
+ class Configuration
3
+ attr_accessor :folder_in_public, :reset_on_each_request, :public_folder
4
+ def initialize
5
+ @folder_in_public = 'webpack'
6
+ @public_folder = 'public'
7
+ @reset_on_each_request = false
8
+ end
9
+
10
+ def full_path
11
+ File.join(public_folder, folder_in_public)
12
+ end
13
+ end
14
+ end
File without changes
@@ -0,0 +1,33 @@
1
+ module WebpackIntegration
2
+ class Store
3
+
4
+ # @example
5
+ # Store.fuzzy_file_for('client.png')
6
+ def self.fuzzy_file_for(file_pattern)
7
+ alternatives = assets_keys.grep(Regexp.new(file_pattern))
8
+ raise "TOO MANY MATCHES for #{file_pattern}" if alternatives.size > 1
9
+ file_for(alternatives.first)
10
+ end
11
+
12
+ def self.file_for(filename)
13
+ assets_manifest.fetch(filename) # will raise on missing assets!
14
+ end
15
+
16
+ def self.load
17
+ assets_keys
18
+ end
19
+
20
+ def self.assets_keys
21
+ @assets_keys ||= assets_manifest.keys.sort!
22
+ end
23
+
24
+ def self.assets_manifest
25
+ @assets_manifest ||= WebpackIntegration::Assets.generate_assets_manifest
26
+ end
27
+
28
+ def self.reset
29
+ @assets_manifest = nil
30
+ @assets_keys = nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module WebpackIntegration
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ module WebpackIntegration
2
+ module ViewHelper
3
+ def webpack_file(filename)
4
+ WebpackIntergration.fuzzy_file_for(filename)
5
+ end
6
+ end
7
+ end
data/sh/c ADDED
@@ -0,0 +1 @@
1
+ irb -r ./sh/env.rb
data/sh/env.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ require 'webpack_integration'
data/sh/test ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rake/testtask'
3
+ if ARGV[0]
4
+ testfiles = (Dir["test/**/**_test.rb"]).grep %r[#{ARGV[0]}]
5
+ end
6
+ Rake::TestTask.new("my_test") do |test|
7
+ if testfiles
8
+ test.test_files = testfiles
9
+ else
10
+ test.pattern = "test/**/*_test.rb"
11
+ end
12
+ test.verbose = true
13
+ end
14
+
15
+ Rake::Task['my_test'].invoke
File without changes
@@ -0,0 +1,36 @@
1
+ require './test/test_helper'
2
+
3
+
4
+ class PublicApiTest < Minitest::Spec
5
+ before do
6
+ WebpackIntegration.configure do |config|
7
+ config.public_folder = 'test/fixtures'
8
+ config.folder_in_public = 'webpack'
9
+ end
10
+ end
11
+
12
+ describe 'file_for' do
13
+ it "delegates to store" do
14
+ WebpackIntegration::Store.expects(:file_for).with('test')
15
+ WebpackIntegration.file_for('test')
16
+ WebpackIntegration::Store.unstub(:file_for)
17
+ end
18
+ end
19
+
20
+
21
+ describe 'fuzzy_file_for' do
22
+ it "delegates to store" do
23
+ WebpackIntegration::Store.expects(:fuzzy_file_for).with('test')
24
+ WebpackIntegration.fuzzy_file_for('test')
25
+ WebpackIntegration::Store.unstub(:fuzzy_file_for)
26
+ end
27
+ end
28
+
29
+ describe 'reset_assets_manifest!' do
30
+ it "delegates to store" do
31
+ WebpackIntegration::Store.expects(:reset)
32
+ WebpackIntegration.reset_assets_manifest!
33
+ WebpackIntegration::Store.unstub(:reset)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,71 @@
1
+ require './test/test_helper'
2
+
3
+ describe 'WebpackIntegration::Store' do
4
+ before do
5
+ WebpackIntegration.configure do |config|
6
+ config.public_folder = 'test/fixtures'
7
+ config.folder_in_public = 'webpack'
8
+ end
9
+ end
10
+
11
+ describe 'load' do
12
+ it "can be loaded" do
13
+ WebpackIntegration::Store.load
14
+ end
15
+ end
16
+
17
+ describe 'assets_keys' do
18
+ it "works" do
19
+ WebpackIntegration::Store.assets_keys
20
+ end
21
+
22
+ it "contains cleaned up file names, that you want to use in your application" do
23
+ expected = ["/brand_view-bundle.js", "/brand_view_styles-bundle.js", "/common.js", "/executive_view-bundle.js", "/executive_view_styles-bundle.js", "/img/logo.png", "/img/logo_for_runaways.png"]
24
+ WebpackIntegration::Store.assets_keys.must_equal expected
25
+ end
26
+ end
27
+
28
+
29
+ describe 'assets_manifest' do
30
+ it "is a hash-map, that maps clean names to digested names with full path" do
31
+ expected = {"/brand_view-bundle.js"=>"/webpack/brand_view-bundle-3e23e81.js", "/brand_view_styles-bundle.js"=>"/webpack/brand_view_styles-bundle-ca97811.js", "/common.js"=>"/webpack/common-2e7d2c0.js", "/executive_view-bundle.js"=>"/webpack/executive_view-bundle-3f79bb7.js", "/executive_view_styles-bundle.js"=>"/webpack/executive_view_styles-bundle-18ac3e7.js", "/img/logo.png"=>"/webpack/img/logo-0ebe2ec.png", "/img/logo_for_runaways.png"=>"/webpack/img/logo_for_runaways-907b853.png"}
32
+ WebpackIntegration::Store.assets_manifest.must_equal(expected)
33
+ end
34
+ end
35
+
36
+ describe 'file_for' do
37
+ it "returns the exact match on keys" do
38
+ expected = '/webpack/brand_view-bundle-3e23e81.js'
39
+ WebpackIntegration::Store.file_for('/brand_view-bundle.js').must_equal expected
40
+ end
41
+
42
+ it "blows up otherwise" do
43
+ exception = lambda{
44
+ WebpackIntegration::Store.file_for('non-existant')
45
+ }.must_raise KeyError
46
+ exception.message.must_equal "key not found: \"non-existant\""
47
+ end
48
+ end
49
+
50
+ describe 'fuzzy_file_for' do
51
+ it "will return the only matching file" do
52
+ expected = '/webpack/brand_view-bundle-3e23e81.js'
53
+ WebpackIntegration::Store.fuzzy_file_for('brand_view-bundle').must_equal expected
54
+ end
55
+
56
+ it "blows up on ambiguous matches" do
57
+ exception = lambda{
58
+ WebpackIntegration::Store.fuzzy_file_for('brand_view')
59
+ }.must_raise RuntimeError
60
+ exception.message.must_equal "TOO MANY MATCHES for brand_view"
61
+ end
62
+ end
63
+
64
+ describe 'reset' do
65
+ it "sets the mainifest to nil" do
66
+ WebpackIntegration::Store.reset
67
+ WebpackIntegration::Store.instance_variable_get('@assets_manifest').must_equal nil
68
+ WebpackIntegration::Store.assets_manifest.wont_equal nil
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,19 @@
1
+ require 'bundler'
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ add_filter "/test/"
6
+ add_filter "/.direnv/"
7
+ end
8
+ if ENV['CI']=='true'
9
+ require 'codecov'
10
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
11
+ end
12
+
13
+
14
+ Bundler.setup
15
+
16
+ require 'webpack_integration'
17
+ require 'maxitest'
18
+ require 'mocha/mini_test'
19
+ require 'maxitest/autorun'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webpack_integration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webpack_integration"
8
+ spec.version = WebpackIntegration::VERSION
9
+ spec.authors = ["Roman Heinrich"]
10
+ spec.email = ["roman.heinrich@gmail.com"]
11
+ spec.summary = %q{A small gem to integrated digested static files with Ruby / Rails apps}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/mindreframer/webpack_integration"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "maxitest"
25
+ spec.add_development_dependency "mocha"
26
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpack_integration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Heinrich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: maxitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A small gem to integrated digested static files with Ruby / Rails apps
84
+ email:
85
+ - roman.heinrich@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/webpack_integration.rb
97
+ - lib/webpack_integration/assets.rb
98
+ - lib/webpack_integration/configuration.rb
99
+ - lib/webpack_integration/railtie.rb
100
+ - lib/webpack_integration/store.rb
101
+ - lib/webpack_integration/version.rb
102
+ - lib/webpack_integration/view_helper.rb
103
+ - sh/c
104
+ - sh/env.rb
105
+ - sh/test
106
+ - test/fixtures/webpack/brand_view-bundle-3e23e81.js
107
+ - test/fixtures/webpack/brand_view_styles-bundle-ca97811.js
108
+ - test/fixtures/webpack/common-2e7d2c0.js
109
+ - test/fixtures/webpack/executive_view-bundle-3f79bb7.js
110
+ - test/fixtures/webpack/executive_view_styles-bundle-18ac3e7.js
111
+ - test/fixtures/webpack/img/logo-0ebe2ec.png
112
+ - test/fixtures/webpack/img/logo_for_runaways-907b853.png
113
+ - test/public_api_test.rb
114
+ - test/store_test.rb
115
+ - test/test_helper.rb
116
+ - webpack_integration.gemspec
117
+ homepage: https://github.com/mindreframer/webpack_integration
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.4
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A small gem to integrated digested static files with Ruby / Rails apps
141
+ test_files:
142
+ - test/fixtures/webpack/brand_view-bundle-3e23e81.js
143
+ - test/fixtures/webpack/brand_view_styles-bundle-ca97811.js
144
+ - test/fixtures/webpack/common-2e7d2c0.js
145
+ - test/fixtures/webpack/executive_view-bundle-3f79bb7.js
146
+ - test/fixtures/webpack/executive_view_styles-bundle-18ac3e7.js
147
+ - test/fixtures/webpack/img/logo-0ebe2ec.png
148
+ - test/fixtures/webpack/img/logo_for_runaways-907b853.png
149
+ - test/public_api_test.rb
150
+ - test/store_test.rb
151
+ - test/test_helper.rb