shakapacker 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.eslintignore +4 -0
  3. data/.eslintrc.js +14 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +18 -0
  6. data/.github/workflows/jest.yml +30 -0
  7. data/.github/workflows/js-lint.yml +31 -0
  8. data/.github/workflows/rubocop.yml +39 -0
  9. data/.github/workflows/ruby.yml +48 -0
  10. data/.gitignore +13 -0
  11. data/.node-version +1 -0
  12. data/.rubocop.yml +229 -0
  13. data/CHANGELOG.md +32 -0
  14. data/CONTRIBUTING.md +62 -0
  15. data/Gemfile +13 -0
  16. data/Gemfile.lock +183 -0
  17. data/MIT-LICENSE +20 -0
  18. data/README.md +666 -0
  19. data/Rakefile +11 -0
  20. data/config/README.md +3 -0
  21. data/config/webpacker.yml +1 -0
  22. data/docs/customizing_babel_config.md +59 -0
  23. data/docs/deployment.md +116 -0
  24. data/docs/developing_webpacker.md +29 -0
  25. data/docs/troubleshooting.md +212 -0
  26. data/docs/v6_upgrade.md +158 -0
  27. data/gemfiles/Gemfile-rails-edge +12 -0
  28. data/gemfiles/Gemfile-rails.5.2.x +9 -0
  29. data/gemfiles/Gemfile-rails.6.0.x +9 -0
  30. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  31. data/lib/install/application.js +15 -0
  32. data/lib/install/bin/webpacker +15 -0
  33. data/lib/install/bin/webpacker-dev-server +18 -0
  34. data/lib/install/bin/yarn +18 -0
  35. data/lib/install/binstubs.rb +4 -0
  36. data/lib/install/config/webpack/webpack.config.js +5 -0
  37. data/lib/install/config/webpacker.yml +64 -0
  38. data/lib/install/package.json +15 -0
  39. data/lib/install/template.rb +100 -0
  40. data/lib/shakapacker/utils/git_utils.rb +23 -0
  41. data/lib/shakapacker/utils/version_syntax_converter.rb +24 -0
  42. data/lib/tasks/webpacker/binstubs.rake +15 -0
  43. data/lib/tasks/webpacker/check_binstubs.rake +12 -0
  44. data/lib/tasks/webpacker/check_node.rake +31 -0
  45. data/lib/tasks/webpacker/check_yarn.rake +33 -0
  46. data/lib/tasks/webpacker/clean.rake +25 -0
  47. data/lib/tasks/webpacker/clobber.rake +20 -0
  48. data/lib/tasks/webpacker/compile.rake +45 -0
  49. data/lib/tasks/webpacker/info.rake +21 -0
  50. data/lib/tasks/webpacker/install.rake +17 -0
  51. data/lib/tasks/webpacker/verify_config.rake +14 -0
  52. data/lib/tasks/webpacker/verify_install.rake +4 -0
  53. data/lib/tasks/webpacker/yarn_install.rake +18 -0
  54. data/lib/tasks/webpacker.rake +19 -0
  55. data/lib/tasks/yarn.rake +38 -0
  56. data/lib/webpacker/commands.rb +79 -0
  57. data/lib/webpacker/compiler.rb +130 -0
  58. data/lib/webpacker/configuration.rb +111 -0
  59. data/lib/webpacker/dev_server.rb +72 -0
  60. data/lib/webpacker/dev_server_proxy.rb +33 -0
  61. data/lib/webpacker/dev_server_runner.rb +96 -0
  62. data/lib/webpacker/env.rb +43 -0
  63. data/lib/webpacker/helper.rb +161 -0
  64. data/lib/webpacker/instance.rb +41 -0
  65. data/lib/webpacker/manifest.rb +120 -0
  66. data/lib/webpacker/railtie.rb +63 -0
  67. data/lib/webpacker/runner.rb +23 -0
  68. data/lib/webpacker/version.rb +4 -0
  69. data/lib/webpacker/webpack_runner.rb +58 -0
  70. data/lib/webpacker.rb +46 -0
  71. data/package/__tests__/config.js +34 -0
  72. data/package/__tests__/dev_server.js +45 -0
  73. data/package/__tests__/development.js +35 -0
  74. data/package/__tests__/env.js +58 -0
  75. data/package/__tests__/index.js +9 -0
  76. data/package/__tests__/production.js +29 -0
  77. data/package/__tests__/staging.js +30 -0
  78. data/package/__tests__/test.js +25 -0
  79. data/package/babel/preset.js +41 -0
  80. data/package/config.js +32 -0
  81. data/package/configPath.js +3 -0
  82. data/package/dev_server.js +20 -0
  83. data/package/env.js +27 -0
  84. data/package/environments/__tests__/base.js +69 -0
  85. data/package/environments/base.js +116 -0
  86. data/package/environments/development.js +55 -0
  87. data/package/environments/production.js +79 -0
  88. data/package/environments/test.js +3 -0
  89. data/package/index.js +33 -0
  90. data/package/inliningCss.js +7 -0
  91. data/package/rules/babel.js +30 -0
  92. data/package/rules/coffee.js +6 -0
  93. data/package/rules/css.js +3 -0
  94. data/package/rules/erb.js +15 -0
  95. data/package/rules/file.js +23 -0
  96. data/package/rules/index.js +18 -0
  97. data/package/rules/less.js +22 -0
  98. data/package/rules/raw.js +5 -0
  99. data/package/rules/sass.js +16 -0
  100. data/package/rules/stylus.js +26 -0
  101. data/package/utils/get_style_rule.js +37 -0
  102. data/package/utils/helpers.js +51 -0
  103. data/package.json +71 -0
  104. data/rakelib/release.rake +57 -0
  105. data/test/command_test.rb +109 -0
  106. data/test/compiler_test.rb +68 -0
  107. data/test/configuration_test.rb +78 -0
  108. data/test/dev_server_runner_test.rb +81 -0
  109. data/test/dev_server_test.rb +47 -0
  110. data/test/engine_rake_tasks_test.rb +39 -0
  111. data/test/env_test.rb +23 -0
  112. data/test/helper_test.rb +159 -0
  113. data/test/manifest_test.rb +89 -0
  114. data/test/mounted_app/Rakefile +4 -0
  115. data/test/mounted_app/test/dummy/Rakefile +3 -0
  116. data/test/mounted_app/test/dummy/bin/rails +3 -0
  117. data/test/mounted_app/test/dummy/bin/rake +3 -0
  118. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  119. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  120. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  121. data/test/mounted_app/test/dummy/config.ru +5 -0
  122. data/test/mounted_app/test/dummy/package.json +7 -0
  123. data/test/rake_tasks_test.rb +71 -0
  124. data/test/test_app/Rakefile +3 -0
  125. data/test/test_app/app/packs/entrypoints/application.js +10 -0
  126. data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
  127. data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
  128. data/test/test_app/bin/webpacker +14 -0
  129. data/test/test_app/bin/webpacker-dev-server +14 -0
  130. data/test/test_app/config/application.rb +11 -0
  131. data/test/test_app/config/environment.rb +4 -0
  132. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  133. data/test/test_app/config/webpack/webpack.config.js +0 -0
  134. data/test/test_app/config/webpacker.yml +77 -0
  135. data/test/test_app/config/webpacker_other_location.yml +79 -0
  136. data/test/test_app/config/webpacker_public_root.yml +18 -0
  137. data/test/test_app/config.ru +5 -0
  138. data/test/test_app/package.json +13 -0
  139. data/test/test_app/public/packs/manifest.json +50 -0
  140. data/test/test_app/some.config.js +0 -0
  141. data/test/test_app/yarn.lock +11 -0
  142. data/test/test_helper.rb +33 -0
  143. data/test/webpack_runner_test.rb +57 -0
  144. data/test/webpacker_test.rb +34 -0
  145. data/webpacker.gemspec +31 -0
  146. data/yarn.lock +4029 -0
  147. metadata +331 -0
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV["RAILS_ENV"] ||= "development"
4
+ ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
5
+
6
+ require "pathname"
7
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
8
+ Pathname.new(__FILE__).realpath)
9
+
10
+ require "bundler/setup"
11
+
12
+ require "webpacker"
13
+ require "webpacker/dev_server_runner"
14
+
15
+ APP_ROOT = File.expand_path("..", __dir__)
16
+ Dir.chdir(APP_ROOT) do
17
+ Webpacker::DevServerRunner.run(ARGV)
18
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ APP_ROOT = File.expand_path("..", __dir__)
4
+ Dir.chdir(APP_ROOT) do
5
+ yarn = ENV["PATH"].split(File::PATH_SEPARATOR).
6
+ select { |dir| File.expand_path(dir) != __dir__ }.
7
+ product(["yarn", "yarnpkg", "yarn.cmd", "yarn.ps1"]).
8
+ map { |dir, file| File.expand_path(file, dir) }.
9
+ find { |file| File.executable?(file) }
10
+
11
+ if yarn
12
+ exec yarn, *ARGV
13
+ else
14
+ $stderr.puts "Yarn executable was not detected in the system."
15
+ $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
16
+ exit 1
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ say "Copying binstubs"
2
+ directory "#{__dir__}/bin", "bin"
3
+
4
+ chmod "bin", 0755 & ~File.umask, verbose: false
@@ -0,0 +1,5 @@
1
+ const { webpackConfig } = require('@shakacode/shakapacker')
2
+
3
+ // See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
4
+
5
+ module.exports = webpackConfig
@@ -0,0 +1,64 @@
1
+ # Note: You must restart bin/webpacker-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ source_path: app/javascript
5
+ source_entry_path: /
6
+ public_root_path: public
7
+ public_output_path: packs
8
+ cache_path: tmp/webpacker
9
+ webpack_compile_output: true
10
+
11
+ # Additional paths webpack should look up modules
12
+ # ['app/assets', 'engine/foo/app/assets']
13
+ additional_paths: []
14
+
15
+ # Reload manifest.json on all requests so we reload latest compiled packs
16
+ cache_manifest: false
17
+
18
+ development:
19
+ <<: *default
20
+ compile: true
21
+
22
+ # Reference: https://webpack.js.org/configuration/dev-server/
23
+ dev_server:
24
+ https: false
25
+ host: localhost
26
+ port: 3035
27
+ # Hot Module Replacement updates modules while the application is running without a full reload
28
+ hmr: false
29
+ # Defaults to the inverse of hmr. Uncomment to manually set this.
30
+ # live_reload: true
31
+ client:
32
+ # Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
33
+ overlay: true
34
+ # May also be a string
35
+ # webSocketURL:
36
+ # hostname: "0.0.0.0"
37
+ # pathname: "/ws"
38
+ # port: 8080
39
+ # Should we use gzip compression?
40
+ compress: true
41
+ # Note that apps that do not check the host are vulnerable to DNS rebinding attacks
42
+ allowed_hosts: "all"
43
+ pretty: true
44
+ headers:
45
+ 'Access-Control-Allow-Origin': '*'
46
+ static:
47
+ watch:
48
+ ignored: '**/node_modules/**'
49
+
50
+ test:
51
+ <<: *default
52
+ compile: true
53
+
54
+ # Compile test packs to a separate directory
55
+ public_output_path: packs-test
56
+
57
+ production:
58
+ <<: *default
59
+
60
+ # Production depends on precompilation of packs prior to booting for performance.
61
+ compile: false
62
+
63
+ # Cache manifest.json for performance
64
+ cache_manifest: true
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "app",
3
+ "private": true,
4
+ "dependencies": {
5
+ },
6
+ "version": "0.1.0",
7
+ "babel": {
8
+ "presets": [
9
+ "./node_modules/@shakacode/shakapacker/package/babel/preset.js"
10
+ ]
11
+ },
12
+ "browserslist": [
13
+ "defaults"
14
+ ]
15
+ }
@@ -0,0 +1,100 @@
1
+ # Install Webpacker
2
+ copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml"
3
+ copy_file "#{__dir__}/package.json", "package.json"
4
+
5
+ say "Copying webpack core config"
6
+ directory "#{__dir__}/config/webpack", "config/webpack"
7
+
8
+ if Dir.exist?(Webpacker.config.source_path)
9
+ say "The packs app source directory already exists"
10
+ else
11
+ say "Creating packs app source directory"
12
+ empty_directory "app/javascript"
13
+ copy_file "#{__dir__}/application.js", "app/javascript/application.js"
14
+ end
15
+
16
+ apply "#{__dir__}/binstubs.rb"
17
+
18
+ git_ignore_path = Rails.root.join(".gitignore")
19
+ if File.exist?(git_ignore_path)
20
+ append_to_file git_ignore_path do
21
+ "\n" +
22
+ "/public/packs\n" +
23
+ "/public/packs-test\n" +
24
+ "/node_modules\n" +
25
+ "/yarn-error.log\n" +
26
+ "yarn-debug.log*\n" +
27
+ ".yarn-integrity\n"
28
+ end
29
+ end
30
+
31
+ if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
32
+ say "Add JavaScript include tag in application layout"
33
+ insert_into_file app_layout_path.to_s, %(\n <%= javascript_pack_tag "application" %>), before: /\s*<\/head>/
34
+ else
35
+ say "Default application.html.erb is missing!", :red
36
+ say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
37
+ end
38
+
39
+ if (setup_path = Rails.root.join("bin/setup")).exist?
40
+ say "Run bin/yarn during bin/setup"
41
+ insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)
42
+
43
+ # Install JavaScript dependencies
44
+ system! "bin/yarn"
45
+ RUBY
46
+ end
47
+
48
+ if (asset_config_path = Rails.root.join("config/initializers/assets.rb")).exist?
49
+ say "Add node_modules to the asset load path"
50
+ append_to_file asset_config_path, <<-RUBY
51
+
52
+ # Add node_modules folder to the asset load path.
53
+ Rails.application.config.assets.paths << Rails.root.join("node_modules")
54
+ RUBY
55
+ end
56
+
57
+ if (csp_config_path = Rails.root.join("config/initializers/content_security_policy.rb")).exist?
58
+ say "Make note of webpack-dev-server exemption needed to csp"
59
+ insert_into_file csp_config_path, <<-RUBY, after: %(# Rails.application.config.content_security_policy do |policy|)
60
+ # # If you are using webpack-dev-server then specify webpack-dev-server host
61
+ # policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
62
+ RUBY
63
+ end
64
+
65
+ results = []
66
+
67
+ Dir.chdir(Rails.root) do
68
+ if Webpacker::VERSION.match?(/^[0-9]+\.[0-9]+\.[0-9]+$/)
69
+ say "Installing @shakacode/shakapacker@#{Webpacker::VERSION}"
70
+ results << run("yarn add @shakacode/shakapacker@#{Webpacker::VERSION}")
71
+ else
72
+ say "Installing @shakacode/shakapacker@next"
73
+ results << run("yarn add @shakacode/shakapacker@next")
74
+ end
75
+
76
+ package_json = File.read("#{__dir__}/../../package.json")
77
+ peers = JSON.parse(package_json)["peerDependencies"]
78
+ peers_to_add = peers.reduce([]) do |result, (package, version)|
79
+ major_version = version.match(/(\d+)/)[1]
80
+ result << "#{package}@#{major_version}"
81
+ end.join(" ")
82
+
83
+ say "Adding @shakacode/shakapacker peerDependencies"
84
+ results << run("yarn add #{peers_to_add}")
85
+
86
+ say "Installing webpack-dev-server for live reloading as a development dependency"
87
+ results << run("yarn add --dev webpack-dev-server")
88
+ end
89
+
90
+ if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
91
+ say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
92
+ say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
93
+ say "config/initializers/content_security_policy.rb with a snippet like this:", :yellow
94
+ say "policy.connect_src :self, :https, \"http://localhost:3035\", \"ws://localhost:3035\" if Rails.env.development?", :yellow
95
+ end
96
+
97
+ unless results.all?
98
+ say "Webpacker installation failed 😭 See above for details.", :red
99
+ exit 1
100
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ module Shakapacker
5
+ module Utils
6
+ module GitUtils
7
+ def self.uncommitted_changes?(message_handler)
8
+ return false if ENV["COVERAGE"] == "true"
9
+
10
+ status = `git status --porcelain`
11
+ return false if $CHILD_STATUS.success? && status.empty?
12
+
13
+ error = if $CHILD_STATUS.success?
14
+ "You have uncommitted code. Please commit or stash your changes before continuing"
15
+ else
16
+ "You do not have Git installed. Please install Git, and commit your changes before continuing"
17
+ end
18
+ message_handler.add_error(error)
19
+ true
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../webpacker/version"
4
+
5
+ module Shakapacker
6
+ module Utils
7
+ class VersionSyntaxConverter
8
+ def rubygem_to_npm(rubygem_version = Webpacker::VERSION)
9
+ regex_match = rubygem_version.match(/(\d+\.\d+\.\d+)[.\-]?(.+)?/)
10
+ return "#{regex_match[1]}-#{regex_match[2]}" if regex_match[2]
11
+
12
+ regex_match[1].to_s
13
+ end
14
+
15
+ def npm_to_rubygem(npm_version)
16
+ match = npm_version
17
+ .tr("-", ".")
18
+ .strip
19
+ .match(/(\d.*)/)
20
+ match.present? ? match[0] : nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ binstubs_template_path = File.expand_path("../../install/binstubs.rb", __dir__).freeze
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
+
4
+ namespace :webpacker do
5
+ desc "Installs Webpacker binstubs in this application"
6
+ task binstubs: [:check_node, :check_yarn] do |task|
7
+ prefix = task.name.split(/#|webpacker:binstubs/).first
8
+
9
+ if Rails::VERSION::MAJOR >= 5
10
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{binstubs_template_path}'"
11
+ else
12
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{binstubs_template_path}'"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ namespace :webpacker do
2
+ desc "Verifies that bin/webpacker is present"
3
+ task :check_binstubs do
4
+ unless File.exist?(Rails.root.join("bin/webpacker"))
5
+ $stderr.puts "webpacker binstub not found.\n"\
6
+ "Have you run rails webpacker:install ?\n"\
7
+ "Make sure the bin directory and bin/webpacker are not included in .gitignore\n"\
8
+ "Exiting!"
9
+ exit!
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ require "semantic_range"
2
+ namespace :webpacker do
3
+ desc "Verifies if Node.js is installed"
4
+ task :check_node do
5
+ begin
6
+ node_version = `node -v || nodejs -v`.strip
7
+ raise Errno::ENOENT if node_version.blank?
8
+
9
+ pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
10
+ node_range = JSON.parse(pkg_path.read)["engines"]["node"]
11
+ is_valid = SemanticRange.satisfies?(node_version, node_range) rescue false
12
+ semver_major = node_version[/\d+/] rescue nil
13
+ is_unstable = semver_major.to_i.odd? rescue false
14
+
15
+ if is_unstable
16
+ $stderr.puts "Warning: you are using an unstable release of Node.js (#{node_version}). If you encounter issues with Node.js, consider switching to an Active LTS release. More info: https://docs.npmjs.com/try-the-latest-stable-version-of-node"
17
+ end
18
+
19
+ unless is_valid
20
+ $stderr.puts "Webpacker requires Node.js \"#{node_range}\" and you are using #{node_version}"
21
+ $stderr.puts "Please upgrade Node.js https://nodejs.org/en/download/"
22
+ $stderr.puts "Exiting!"
23
+ exit!
24
+ end
25
+ rescue Errno::ENOENT
26
+ $stderr.puts "Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/"
27
+ $stderr.puts "Exiting!"
28
+ exit!
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ require "semantic_range"
2
+ namespace :webpacker do
3
+ desc "Verifies if Yarn is installed"
4
+ task :check_yarn do
5
+ begin
6
+ which_command = Gem.win_platform? ? "where" : "which"
7
+ raise Errno::ENOENT if `#{which_command} yarn`.strip.empty?
8
+
9
+ yarn_version = `yarn --version`.strip
10
+ raise Errno::ENOENT if yarn_version.blank?
11
+
12
+ pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
13
+ yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
14
+ is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
15
+ is_unsupported = SemanticRange.satisfies?(yarn_version, ">=4.0.0") rescue false
16
+
17
+ unless is_valid
18
+ $stderr.puts "Webpacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
19
+ if is_unsupported
20
+ $stderr.puts "This version of Webpacker does not support Yarn #{yarn_version}. Please downgrade to a supported version of Yarn https://yarnpkg.com/lang/en/docs/install/"
21
+ else
22
+ $stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
23
+ end
24
+ $stderr.puts "Exiting!"
25
+ exit!
26
+ end
27
+ rescue Errno::ENOENT
28
+ $stderr.puts "Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/"
29
+ $stderr.puts "Exiting!"
30
+ exit!
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ $stdout.sync = true
2
+
3
+ require "webpacker/configuration"
4
+
5
+ namespace :webpacker do
6
+ desc "Remove old compiled webpacks"
7
+ task :clean, [:keep, :age] => ["webpacker:verify_install", :environment] do |_, args|
8
+ Webpacker.ensure_log_goes_to_stdout do
9
+ Webpacker.clean(Integer(args.keep || 2), Integer(args.age || 3600))
10
+ end
11
+ end
12
+ end
13
+
14
+ skip_webpacker_clean = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
15
+
16
+ unless skip_webpacker_clean
17
+ # Run clean if the assets:clean is run
18
+ if Rake::Task.task_defined?("assets:clean")
19
+ Rake::Task["assets:clean"].enhance do
20
+ Rake::Task["webpacker:clean"].invoke
21
+ end
22
+ else
23
+ Rake::Task.define_task("assets:clean" => "webpacker:clean")
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require "webpacker/configuration"
2
+
3
+ namespace :webpacker do
4
+ desc "Remove the webpack compiled output directory"
5
+ task clobber: ["webpacker:verify_config", :environment] do
6
+ Webpacker.clobber
7
+ $stdout.puts "Removed webpack output path directory #{Webpacker.config.public_output_path}"
8
+ end
9
+ end
10
+
11
+ skip_webpacker_clobber = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
12
+
13
+ unless skip_webpacker_clobber
14
+ # Run clobber if the assets:clobber is run
15
+ if Rake::Task.task_defined?("assets:clobber")
16
+ Rake::Task["assets:clobber"].enhance do
17
+ Rake::Task["webpacker:clobber"].invoke
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ $stdout.sync = true
2
+
3
+ def yarn_install_available?
4
+ rails_major = Rails::VERSION::MAJOR
5
+ rails_minor = Rails::VERSION::MINOR
6
+
7
+ rails_major > 5 || (rails_major == 5 && rails_minor >= 1)
8
+ end
9
+
10
+ def enhance_assets_precompile
11
+ # yarn:install was added in Rails 5.1
12
+ deps = yarn_install_available? ? [] : ["webpacker:yarn_install"]
13
+ Rake::Task["assets:precompile"].enhance(deps) do |task|
14
+ prefix = task.name.split(/#|assets:precompile/).first
15
+
16
+ Rake::Task["#{prefix}webpacker:compile"].invoke
17
+ end
18
+ end
19
+
20
+ namespace :webpacker do
21
+ desc "Compile JavaScript packs using webpack for production with digests"
22
+ task compile: ["webpacker:verify_install", :environment] do
23
+ Webpacker.with_node_env(ENV.fetch("NODE_ENV", "production")) do
24
+ Webpacker.ensure_log_goes_to_stdout do
25
+ if Webpacker.compile
26
+ # Successful compilation!
27
+ else
28
+ # Failed compilation
29
+ exit!
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ # Compile packs after we've compiled all other assets during precompilation
37
+ skip_webpacker_precompile = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
38
+
39
+ unless skip_webpacker_precompile
40
+ if Rake::Task.task_defined?("assets:precompile")
41
+ enhance_assets_precompile
42
+ else
43
+ Rake::Task.define_task("assets:precompile" => ["webpacker:yarn_install", "webpacker:compile"])
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ require "webpacker/version"
2
+
3
+ namespace :webpacker do
4
+ desc "Provide information on Webpacker's environment"
5
+ task :info do
6
+ Dir.chdir(Rails.root) do
7
+ $stdout.puts "Ruby: #{`ruby --version`}"
8
+ $stdout.puts "Rails: #{Rails.version}"
9
+ $stdout.puts "Webpacker: #{Webpacker::VERSION}"
10
+ $stdout.puts "Node: #{`node --version`}"
11
+ $stdout.puts "Yarn: #{`yarn --version`}"
12
+
13
+ $stdout.puts "\n"
14
+ $stdout.puts "@shakacode/shakapacker: \n#{`npm list @shakacode/shakapacker version`}"
15
+
16
+ $stdout.puts "Is bin/webpacker present?: #{File.exist? 'bin/webpacker'}"
17
+ $stdout.puts "Is bin/webpacker-dev-server present?: #{File.exist? 'bin/webpacker-dev-server'}"
18
+ $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ install_template_path = File.expand_path("../../install/template.rb", __dir__).freeze
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
+
4
+ namespace :webpacker do
5
+ desc "Install Webpacker in this application"
6
+ task install: [:check_node, :check_yarn] do |task|
7
+ Webpacker::Configuration.installing = true
8
+
9
+ prefix = task.name.split(/#|webpacker:install/).first
10
+
11
+ if Rails::VERSION::MAJOR >= 5
12
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{install_template_path}'"
13
+ else
14
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{install_template_path}'"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require "webpacker/configuration"
2
+
3
+ namespace :webpacker do
4
+ desc "Verifies if the Webpacker config is present"
5
+ task :verify_config do
6
+ unless Webpacker.config.config_path.exist?
7
+ path = Webpacker.config.config_path.relative_path_from(Pathname.new(pwd)).to_s
8
+ $stderr.puts "Configuration #{path} file not found. \n"\
9
+ "Make sure webpacker:install is run successfully before " \
10
+ "running dependent tasks"
11
+ exit!
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ namespace :webpacker do
2
+ desc "Verifies if Webpacker is installed"
3
+ task verify_install: [:verify_config, :check_node, :check_yarn, :check_binstubs]
4
+ end
@@ -0,0 +1,18 @@
1
+ namespace :webpacker do
2
+ desc "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
3
+ task :yarn_install do
4
+ valid_node_envs = %w[test development production]
5
+ node_env = ENV.fetch("NODE_ENV") do
6
+ valid_node_envs.include?(Rails.env) ? Rails.env : "production"
7
+ end
8
+ Dir.chdir(Rails.root) do
9
+ yarn_flags =
10
+ if `yarn --version`.start_with?("1")
11
+ "--no-progress --frozen-lockfile"
12
+ else
13
+ "--immutable"
14
+ end
15
+ system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ tasks = {
2
+ "webpacker:info" => "Provides information on Webpacker's environment",
3
+ "webpacker:install" => "Installs and setup webpack with Yarn",
4
+ "webpacker:compile" => "Compiles webpack bundles based on environment",
5
+ "webpacker:clean" => "Remove old compiled webpacks",
6
+ "webpacker:clobber" => "Removes the webpack compiled output directory",
7
+ "webpacker:check_node" => "Verifies if Node.js is installed",
8
+ "webpacker:check_yarn" => "Verifies if Yarn is installed",
9
+ "webpacker:check_binstubs" => "Verifies that bin/webpacker is present",
10
+ "webpacker:binstubs" => "Installs Webpacker binstubs in this application",
11
+ "webpacker:verify_install" => "Verifies if Webpacker is installed",
12
+ "webpacker:yarn_install" => "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
13
+ }.freeze
14
+
15
+ desc "Lists all available tasks in Webpacker"
16
+ task :webpacker do
17
+ puts "Available Webpacker tasks are:"
18
+ tasks.each { |task, message| puts task.ljust(30) + message }
19
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Duplicate of the yarn tasks still present in Rails until Webpacker <5 have been deprecated
4
+
5
+ namespace :yarn do
6
+ desc "Install all JavaScript dependencies as specified via Yarn"
7
+ task :install do
8
+ begin
9
+ # Install only production deps when for not usual envs.
10
+ valid_node_envs = %w[test development production]
11
+ node_env = ENV.fetch("NODE_ENV") do
12
+ valid_node_envs.include?(Rails.env) ? Rails.env : "production"
13
+ end
14
+
15
+ yarn_flags =
16
+ if `#{RbConfig.ruby} "#{Rails.root}/bin/yarn" --version`.start_with?("1")
17
+ "--no-progress --frozen-lockfile"
18
+ else
19
+ "--immutable"
20
+ end
21
+
22
+ system(
23
+ { "NODE_ENV" => node_env },
24
+ "#{RbConfig.ruby} \"#{Rails.root}/bin/yarn\" install #{yarn_flags}",
25
+ exception: true
26
+ )
27
+ rescue Errno::ENOENT
28
+ $stderr.puts "bin/yarn was not found."
29
+ $stderr.puts "Please run `bundle exec rails app:update:bin` to create it."
30
+ exit 1
31
+ end
32
+ end
33
+ end
34
+
35
+ # Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use.
36
+ if Rake::Task.task_defined?("assets:precompile") && File.exist?(Rails.root.join("bin", "yarn"))
37
+ Rake::Task["assets:precompile"].enhance [ "yarn:install" ]
38
+ end