webpackrails 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: e569323b6234dc24370f3b4dc766f74727fd5078
4
+ data.tar.gz: b1744dd6544f0a0fdb4019a353bc4f54f6fafc92
5
+ SHA512:
6
+ metadata.gz: 820dcdd02d7d11b75498a155ca11a392294517cdc8a258b5c0fafb6d1c2eb073af14454bb4beb54b91bdfd5040491074a291b3b5e84d1c33e4121e4d28f9374a
7
+ data.tar.gz: 56ffbbd95569dc345987f14a3116029f8cc66ba76600cbbc014c83828073d54f7a145e8d1a1a250b10631d5412a780f54cc245769a0a1f05d7ad33fb835a03a2
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ */.DS_Store
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://ruby.taobao.org/'
2
+
3
+ # Specify your gem's dependencies in webpack-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 towry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # WebpackRails
2
+
3
+ This gem make [Webpack](http://webpack.github.io) works with [Rails](http://github.com/rails/rails).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'webpack-rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install webpack-rails
20
+
21
+ Create `package.json` in your Rails root:
22
+
23
+ ```json
24
+ {
25
+ "name": "app",
26
+ "dependencies": {
27
+ "webpack": "^1.11.0"
28
+ }
29
+ }
30
+ ```
31
+
32
+ Run `npm i` to install the dependencies.
33
+
34
+ ## Usage
35
+
36
+ Make sure you have a `webpack.config.js` file in your Rails root dir, or specific your
37
+ webpack config file within `application.rb`
38
+
39
+ `config.webpack_rails.config_file = "path_to_the_config_file"`
40
+
41
+ ### Config
42
+
43
+ See [source](https://raw.githubusercontent.com/webpack-rails/webpack-rails/master/lib/webpack-rails/railtie.rb)
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/webpack-rails/webpack-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "webpack-rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ require 'sprockets'
4
+
5
+ require 'webpack-rails/webpack_processor'
6
+ require 'webpack-rails/webpack_logger'
7
+ require 'webpack-rails/webpack_error'
8
+ require 'webpack-rails/railtie'
9
+ require 'webpack-rails/version'
10
+
11
+ module WebpackRails
12
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ module WebpackRails
4
+ class Railtie < Rails::Engine
5
+ config.webpack_rails = ActiveSupport::OrderedOptions.new
6
+
7
+ # Webpack config file location
8
+ config.webpack_rails.config_file = ''
9
+
10
+ # Process every file?
11
+ config.webpack_rails.force = false
12
+
13
+ # paths to be parse
14
+ config.webpack_rails.paths = [lambda { |p| p.start_with?(Rails.root.join("app").to_s) },
15
+ lambda { |p| p.start_with?(Rails.root.join('node_modules').to_s) }]
16
+
17
+ config.webpack_rails.node_bin = "node_modules/.bin/"
18
+
19
+ initializer :setup_webpack do |app|
20
+ app.assets.register_postprocessor "application/javascript", WebapckProcessor
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module WebpackRails
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ module WebpackRails
4
+ class WebpackError < RuntimeError
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module WebpackRails
4
+ module Logger
5
+ def self.log(message)
6
+ Rails.logger.debug message
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tilt'
4
+ require 'open3'
5
+ require 'tempfile'
6
+ require 'fileutils'
7
+ require 'shellwords'
8
+
9
+ module WebpackRails
10
+ class WebapckProcessor < Tilt::Template
11
+ attr_accessor :config
12
+
13
+ def initialize(template)
14
+ self.config = Rails.application.config.webpack_rails
15
+ super(template)
16
+ end
17
+
18
+ def prepare
19
+ ensure_tmp_dir_exists!
20
+ ensure_commands_exists!
21
+ ensure_webpack_config_exists!
22
+ end
23
+
24
+ def evaluate(context, locals, &block)
25
+ # return if there is nothing to do
26
+ return data unless should_webpack?
27
+
28
+ run_webpack(context.logical_path)
29
+ end
30
+
31
+ private
32
+
33
+ # Set the temp path
34
+ def tmp_path
35
+ @tmp_path ||= Rails.root.join('tmp', 'cache', 'webpack-rails').freeze
36
+ end
37
+
38
+ # return the webpack command path
39
+ def webpack_cmd
40
+ @webpack_cmd ||= rails_path(config.node_bin, "webpack").freeze
41
+ end
42
+
43
+ # make sure the temp dir exists
44
+ def ensure_tmp_dir_exists!
45
+ FileUtils.mkdir_p(rails_path(tmp_path))
46
+ end
47
+
48
+ # make sure the webpack config file exists
49
+ def ensure_webpack_config_exists!
50
+ @config_file ||= config.config_file
51
+
52
+ if @config_file.blank?
53
+ @config_file = Rails.root.join('webpack.config.js').freeze
54
+ end
55
+
56
+ if !File.exists?(@config_file)
57
+ raise WebpackRails::WebpackError.new("Webpack config file not found.")
58
+ end
59
+ end
60
+
61
+ # make sure the `webpack` command exists
62
+ def ensure_commands_exists!
63
+ error = ->(cmd) { "Unable to run #{cmd}. Ensure you have installed it with npm." }
64
+
65
+ if !File.exists?(rails_path(webpack_cmd))
66
+ raise WebpackRails::WebpackError.new(error.call(webpack_cmd))
67
+ end
68
+ end
69
+
70
+ # should we use webpack to parse that file?
71
+ def should_webpack?
72
+ config.force || (in_path? && !webpacked? && commonjs_module?)
73
+ end
74
+
75
+ def in_path?
76
+ config.paths.any? { |p| p === file }
77
+ end
78
+
79
+ # Need check.
80
+ def webpacked?
81
+ data.to_s.include?("__webpack_require__")
82
+ end
83
+
84
+ def commonjs_module?
85
+ data.to_s.include?("module.exports") || data.present? && data.to_s.include?("require")
86
+ end
87
+
88
+ def asset_paths
89
+ @asset_paths ||= Rails.application.config.assets.paths.collect { |p| p.to_s }.join(":") || ""
90
+ end
91
+
92
+ def env
93
+ env_hash = {}
94
+ env_hash["NODE_PATH"] = asset_paths unless uses_exorcist
95
+ env_hash["NODE_ENV"] = config.node_env || Rails.env
96
+ env_hash
97
+ end
98
+
99
+ def uses_exorcist
100
+ false
101
+ end
102
+
103
+ def run_webpack(logical_path=nil)
104
+ command_options = "--config #{@config_file} #{logical_path} --output-filename"
105
+ output_file = Tempfile.new("output", rails_path(tmp_path))
106
+ command_options << " #{output_file.path.inspect}"
107
+
108
+ command = "#{Shellwords.escape(webpack_cmd)} #{command_options}"
109
+
110
+ base_directory = File.dirname(file)
111
+
112
+ Logger::log "Webpack: #{command}"
113
+ stdout, stderr, status = Open3.capture3(env, command, stdin_data: data, chdir: base_directory)
114
+
115
+ if !status.success?
116
+ raise WebpackRails::WebpackError.new("Error while running `#{command}`:\n\n#{stderr}")
117
+ end
118
+
119
+ output_file.read
120
+ end
121
+
122
+ def rails_path(*paths)
123
+ Rails.root.join(*paths).to_s
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webpack-rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webpackrails"
8
+ spec.version = WebpackRails::VERSION
9
+ spec.authors = ["towry"]
10
+ spec.email = ["tovvry@gmail.com"]
11
+
12
+ spec.summary = %q{Make Webpack work with Rails for you}
13
+ spec.description = %q{Webpack + Rails ≠ CommonJS Heaven}
14
+ spec.homepage = "https://github.com/webpack-rails/webpack-rails"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "railties", "~> 3.2", "<= 5.0.0"
31
+ spec.add_runtime_dependency "sprockets", "~> 2.0"
32
+
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "bundler", ">= 1.3"
35
+ spec.add_development_dependency "rails"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "tilt"
38
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpackrails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - towry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - <=
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - <=
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: sprockets
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '1.3'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '1.3'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: tilt
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Webpack + Rails ≠ CommonJS Heaven
118
+ email:
119
+ - tovvry@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - .gitignore
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/webpack-rails.rb
133
+ - lib/webpack-rails/railtie.rb
134
+ - lib/webpack-rails/version.rb
135
+ - lib/webpack-rails/webpack_error.rb
136
+ - lib/webpack-rails/webpack_logger.rb
137
+ - lib/webpack-rails/webpack_processor.rb
138
+ - webpack-rails.gemspec
139
+ homepage: https://github.com/webpack-rails/webpack-rails
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ allowed_push_host: https://rubygems.org
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.4.6
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Make Webpack work with Rails for you
164
+ test_files: []