webpacker 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -8
  3. data/CHANGELOG.md +64 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +68 -1
  6. data/README.md +42 -25
  7. data/Rakefile +11 -0
  8. data/lib/install/angular.rb +1 -1
  9. data/lib/install/bin/webpack-dev-server.tt +2 -2
  10. data/lib/install/bin/webpack.tt +2 -9
  11. data/lib/install/bin/yarn.tt +3 -2
  12. data/lib/install/config/.babelrc +5 -0
  13. data/lib/install/config/loaders/core/assets.js +1 -1
  14. data/lib/install/config/loaders/core/babel.js +1 -6
  15. data/lib/install/config/loaders/core/erb.js +1 -1
  16. data/lib/install/config/loaders/core/sass.js +6 -1
  17. data/lib/install/config/loaders/installers/react.js +1 -7
  18. data/lib/install/config/loaders/installers/vue.js +4 -2
  19. data/lib/install/config/webpack/configuration.js +9 -4
  20. data/lib/install/config/webpack/development.js +1 -1
  21. data/lib/install/config/webpack/development.server.yml +17 -4
  22. data/lib/install/config/webpack/paths.yml +33 -19
  23. data/lib/install/config/webpack/production.js +2 -1
  24. data/lib/install/config/webpack/shared.js +13 -10
  25. data/lib/install/config/webpack/test.js +6 -0
  26. data/lib/install/examples/react/.babelrc +4 -1
  27. data/lib/install/examples/react/hello_react.jsx +2 -1
  28. data/lib/install/examples/vue/app.vue +1 -1
  29. data/lib/install/react.rb +20 -4
  30. data/lib/install/template.rb +21 -9
  31. data/lib/install/vue.rb +1 -1
  32. data/lib/tasks/installers.rake +2 -2
  33. data/lib/tasks/webpacker.rake +2 -0
  34. data/lib/tasks/webpacker/check_node.rake +19 -0
  35. data/lib/tasks/webpacker/check_yarn.rake +12 -0
  36. data/lib/tasks/webpacker/clobber.rake +17 -0
  37. data/lib/tasks/webpacker/compile.rake +15 -2
  38. data/lib/tasks/webpacker/install.rake +3 -3
  39. data/lib/tasks/webpacker/verify_install.rake +1 -1
  40. data/lib/webpacker.rb +5 -0
  41. data/lib/webpacker/configuration.rb +15 -6
  42. data/lib/webpacker/env.rb +27 -0
  43. data/lib/webpacker/manifest.rb +6 -1
  44. data/lib/webpacker/railtie.rb +6 -4
  45. data/lib/webpacker/version.rb +1 -1
  46. data/package.json +4 -1
  47. data/test/env_test.rb +16 -0
  48. data/test/webpacker_test.rb +14 -0
  49. data/webpacker.gemspec +2 -2
  50. metadata +18 -5
  51. data/lib/install/bin/webpack-watcher.tt +0 -10
@@ -1,3 +1,4 @@
1
+ require "webpacker/env"
1
2
  require "webpacker/configuration"
2
3
  REGEX_MAP = /\A.*\.map\z/
3
4
 
@@ -5,16 +6,28 @@ namespace :webpacker do
5
6
  desc "Compile javascript packs using webpack for production with digests"
6
7
  task compile: ["webpacker:verify_install", :environment] do
7
8
  puts "Compiling webpacker assets 🎉"
8
- result = `NODE_ENV=production ./bin/webpack`
9
+ asset_host = Rails.application.config.action_controller.asset_host
10
+ result = `ASSET_HOST=#{asset_host} NODE_ENV=#{Webpacker::Env.current} ./bin/webpack --json`
9
11
 
10
12
  unless $?.success?
11
13
  puts JSON.parse(result)["errors"]
12
14
  exit! $?.exitstatus
13
15
  end
14
16
 
15
- puts "Compiled digests for all packs in #{Webpacker::Configuration.output_path}: "
17
+ puts "Compiled digests for all packs in #{Webpacker::Configuration.packs_path}: "
16
18
  puts JSON.parse(File.read(Webpacker::Configuration.manifest_path))
17
19
  end
20
+
21
+ desc "Compile javascript packs using webpack for test with digests"
22
+ task compile_before_test: ["webpacker:compile"] do
23
+ Webpacker::Manifest.load(Webpacker::Manifest.file_path)
24
+ end
25
+ end
26
+
27
+ # Compile packs prior to system and controller tests running
28
+ if Rake::Task.task_defined?("test:system")
29
+ Rake::Task["test:system"].enhance(["webpacker:compile_before_test"])
30
+ Rake::Task["test:controllers"].enhance(["webpacker:compile_before_test"])
18
31
  end
19
32
 
20
33
  # Compile packs after we've compiled all other assets during precompilation
@@ -2,11 +2,11 @@ WEBPACKER_APP_TEMPLATE_PATH = File.expand_path("../../install/template.rb", __di
2
2
 
3
3
  namespace :webpacker do
4
4
  desc "Install webpacker in this application"
5
- task :install do
5
+ task install: [:check_node, :check_yarn] do
6
6
  if Rails::VERSION::MAJOR >= 5
7
- exec "./bin/rails app:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
7
+ exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
8
8
  else
9
- exec "./bin/rake rails:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
9
+ exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
10
10
  end
11
11
  end
12
12
  end
@@ -2,7 +2,7 @@ require "webpacker/configuration"
2
2
 
3
3
  namespace :webpacker do
4
4
  desc "Verifies if webpacker is installed"
5
- task :verify_install do
5
+ task verify_install: [:check_node, :check_yarn] do
6
6
  if File.exist?(Webpacker::Configuration.file_path)
7
7
  puts "Webpacker is installed 🎉 🍰"
8
8
  puts "Using #{Webpacker::Configuration.file_path} file for setting up webpack paths"
@@ -1,4 +1,9 @@
1
1
  module Webpacker
2
+ def self.bootstrap
3
+ Webpacker::Env.load
4
+ Webpacker::Configuration.load
5
+ Webpacker::Manifest.load
6
+ end
2
7
  end
3
8
 
4
9
  require "webpacker/railtie" if defined?(Rails)
@@ -1,5 +1,6 @@
1
1
  # Loads webpacker configuration from config/webpack/paths.yml
2
2
  require "webpacker/file_loader"
3
+ require "webpacker/env"
3
4
 
4
5
  class Webpacker::Configuration < Webpacker::FileLoader
5
6
  class << self
@@ -16,27 +17,35 @@ class Webpacker::Configuration < Webpacker::FileLoader
16
17
  end
17
18
 
18
19
  def manifest_path
19
- Rails.root.join(output_path, "manifest.json")
20
+ Rails.root.join(packs_path, paths.fetch(:manifest, "manifest.json"))
20
21
  end
21
22
 
22
- def output_path
23
- Rails.root.join(paths.fetch(:output, "public"), paths.fetch(:entry, "packs"))
23
+ def packs_path
24
+ Rails.root.join(output_path, paths.fetch(:entry, "packs"))
24
25
  end
25
26
 
26
27
  def paths
27
- load if Rails.env.development?
28
+ load if Webpacker::Env.development?
28
29
  raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
29
30
  instance.data
30
31
  end
31
32
 
33
+ def output_path
34
+ Rails.root.join(paths.fetch(:output, "public"))
35
+ end
36
+
37
+ def source
38
+ paths.fetch(:source, "app/javascript")
39
+ end
40
+
32
41
  def source_path
33
- Rails.root.join(paths.fetch(:source, "app/javascript"))
42
+ Rails.root.join(source)
34
43
  end
35
44
  end
36
45
 
37
46
  private
38
47
  def load
39
48
  return super unless File.exist?(@path)
40
- HashWithIndifferentAccess.new(YAML.load(File.read(@path)))
49
+ HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker::Env.current])
41
50
  end
42
51
  end
@@ -0,0 +1,27 @@
1
+ # Singleton registry for determining NODE_ENV from config/webpack/paths.yml
2
+ require "webpacker/file_loader"
3
+
4
+ class Webpacker::Env < Webpacker::FileLoader
5
+ class << self
6
+ def current
7
+ raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Env.load must be called first") unless instance
8
+ instance.data
9
+ end
10
+
11
+ def development?
12
+ current == "development"
13
+ end
14
+
15
+ def file_path
16
+ Rails.root.join("config", "webpack", "paths.yml")
17
+ end
18
+ end
19
+
20
+ private
21
+ def load
22
+ environments = File.exist?(@path) ? YAML.load(File.read(@path)).keys : [].freeze
23
+ return ENV["NODE_ENV"] if environments.include?(ENV["NODE_ENV"])
24
+ return Rails.env if environments.include?(Rails.env)
25
+ "production"
26
+ end
27
+ end
@@ -6,6 +6,7 @@
6
6
  # "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
7
7
 
8
8
  require "webpacker/file_loader"
9
+ require "webpacker/env"
9
10
  require "webpacker/configuration"
10
11
 
11
12
  class Webpacker::Manifest < Webpacker::FileLoader
@@ -15,10 +16,14 @@ class Webpacker::Manifest < Webpacker::FileLoader
15
16
  end
16
17
 
17
18
  def lookup(name)
18
- load if Rails.env.development?
19
+ load if Webpacker::Env.development?
19
20
  raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
20
21
  instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
21
22
  end
23
+
24
+ def lookup_path(name)
25
+ Rails.root.join(File.join(Webpacker::Configuration.output_path, lookup(name)))
26
+ end
22
27
  end
23
28
 
24
29
  private
@@ -8,9 +8,11 @@ class Webpacker::Engine < ::Rails::Engine
8
8
  ActionController::Base.helper Webpacker::Helper
9
9
  end
10
10
 
11
- # Loads webpacker config data from config/webpack/paths.yml
12
- Webpacker::Configuration.load
13
- # Loads manifest data from public/packs/manifest.json
14
- Webpacker::Manifest.load
11
+ ActiveSupport.on_load :action_view do
12
+ include Webpacker::Helper
13
+ end
14
+
15
+ Webpacker.bootstrap
16
+ Spring.after_fork { Webpacker.bootstrap } if defined?(Spring)
15
17
  end
16
18
  end
@@ -1,3 +1,3 @@
1
1
  module Webpacker
2
- VERSION = "1.1".freeze
2
+ VERSION = "1.2".freeze
3
3
  end
@@ -3,6 +3,9 @@
3
3
  "version": "1.0.0",
4
4
  "description": "Webpacker makes it easy to use the JavaScript preprocessor and bundler [Webpack](https://webpack.github.io) to manage application-like JavaScript in Rails. It coexists with the asset pipeline, as the purpose is only to use Webpack for app-like JavaScript, not images, css, or even JavaScript Sprinkles (that all continues to live in app/assets).",
5
5
  "main": "index.js",
6
+ "engines": {
7
+ "node": ">= 6.4.0"
8
+ },
6
9
  "dependencies": {},
7
10
  "devDependencies": {
8
11
  "eslint": "^3.16.1",
@@ -24,5 +27,5 @@
24
27
  "bugs": {
25
28
  "url": "https://github.com/rails/webpacker/issues"
26
29
  },
27
- "homepage": "https://github.com/rails/webpacker#readme"
30
+ "homepage": "https://github.com/rails/webpacker"
28
31
  }
@@ -0,0 +1,16 @@
1
+ require "webpacker_test"
2
+
3
+ class EnvTest < Minitest::Test
4
+ def test_current_env
5
+ assert_equal Webpacker::Env.current, "production"
6
+ end
7
+
8
+ def test_env_is_development?
9
+ refute_predicate Webpacker::Env, :development?
10
+ end
11
+
12
+ def test_file_path
13
+ correct_path = File.join(File.dirname(__FILE__), "config", "webpack", "paths.yml").to_s
14
+ assert_equal Webpacker::Env.file_path.to_s, correct_path
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/autorun"
4
+ require "rails"
5
+ require "rails/test_help"
6
+ require "webpacker"
7
+
8
+ module TestApp
9
+ class Application < ::Rails::Application
10
+ config.root = File.dirname(__FILE__)
11
+ end
12
+ end
13
+
14
+ TestApp::Application.initialize!
@@ -4,8 +4,8 @@ require "webpacker/version"
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "webpacker"
6
6
  s.version = Webpacker::VERSION
7
- s.authors = "David Heinemeier Hansson"
8
- s.email = "david@basecamp.com"
7
+ s.authors = [ "David Heinemeier Hansson", "Gaurav Tiwari" ]
8
+ s.email = [ "david@basecamp.com", "gaurav@gauravtiwari.co.uk" ]
9
9
  s.summary = "Use Webpack to manage app-like JavaScript modules in Rails"
10
10
  s.homepage = "https://github.com/rails/webpacker"
11
11
  s.license = "MIT"
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
+ - Gaurav Tiwari
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2017-03-25 00:00:00.000000000 Z
12
+ date: 2017-04-27 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: activesupport
@@ -67,7 +68,9 @@ dependencies:
67
68
  - !ruby/object:Gem::Version
68
69
  version: '1.12'
69
70
  description:
70
- email: david@basecamp.com
71
+ email:
72
+ - david@basecamp.com
73
+ - gaurav@gauravtiwari.co.uk
71
74
  executables: []
72
75
  extensions: []
73
76
  extra_rdoc_files: []
@@ -76,6 +79,7 @@ files:
76
79
  - ".gitignore"
77
80
  - ".rubocop.yml"
78
81
  - ".travis.yml"
82
+ - CHANGELOG.md
79
83
  - Gemfile
80
84
  - Gemfile.lock
81
85
  - MIT-LICENSE
@@ -83,9 +87,9 @@ files:
83
87
  - Rakefile
84
88
  - lib/install/angular.rb
85
89
  - lib/install/bin/webpack-dev-server.tt
86
- - lib/install/bin/webpack-watcher.tt
87
90
  - lib/install/bin/webpack.tt
88
91
  - lib/install/bin/yarn.tt
92
+ - lib/install/config/.babelrc
89
93
  - lib/install/config/.postcssrc.yml
90
94
  - lib/install/config/loaders/core/assets.js
91
95
  - lib/install/config/loaders/core/babel.js
@@ -102,6 +106,7 @@ files:
102
106
  - lib/install/config/webpack/paths.yml
103
107
  - lib/install/config/webpack/production.js
104
108
  - lib/install/config/webpack/shared.js
109
+ - lib/install/config/webpack/test.js
105
110
  - lib/install/examples/angular/hello_angular.js
106
111
  - lib/install/examples/angular/hello_angular/app/app.component.ts
107
112
  - lib/install/examples/angular/hello_angular/app/app.module.ts
@@ -118,18 +123,24 @@ files:
118
123
  - lib/install/vue.rb
119
124
  - lib/tasks/installers.rake
120
125
  - lib/tasks/webpacker.rake
126
+ - lib/tasks/webpacker/check_node.rake
127
+ - lib/tasks/webpacker/check_yarn.rake
128
+ - lib/tasks/webpacker/clobber.rake
121
129
  - lib/tasks/webpacker/compile.rake
122
130
  - lib/tasks/webpacker/install.rake
123
131
  - lib/tasks/webpacker/verify_install.rake
124
132
  - lib/tasks/webpacker/yarn_install.rake
125
133
  - lib/webpacker.rb
126
134
  - lib/webpacker/configuration.rb
135
+ - lib/webpacker/env.rb
127
136
  - lib/webpacker/file_loader.rb
128
137
  - lib/webpacker/helper.rb
129
138
  - lib/webpacker/manifest.rb
130
139
  - lib/webpacker/railtie.rb
131
140
  - lib/webpacker/version.rb
132
141
  - package.json
142
+ - test/env_test.rb
143
+ - test/webpacker_test.rb
133
144
  - webpacker.gemspec
134
145
  - yarn.lock
135
146
  homepage: https://github.com/rails/webpacker
@@ -156,4 +167,6 @@ rubygems_version: 2.6.8
156
167
  signing_key:
157
168
  specification_version: 4
158
169
  summary: Use Webpack to manage app-like JavaScript modules in Rails
159
- test_files: []
170
+ test_files:
171
+ - test/env_test.rb
172
+ - test/webpacker_test.rb
@@ -1,10 +0,0 @@
1
- <%= shebang %>
2
-
3
- ENV['RAILS_ENV'] ||= 'development'
4
- ENV['NODE_ENV'] ||= ENV['RAILS_ENV']
5
-
6
- BIN_PATH = File.expand_path('.', __dir__)
7
-
8
- Dir.chdir(BIN_PATH) do
9
- exec "./webpack --watch --progress --color #{ARGV.join(" ")}"
10
- end