webpacker-legacy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +15 -0
  2. data/.eslintrc.js +14 -0
  3. data/.gitignore +4 -0
  4. data/.rubocop.yml +124 -0
  5. data/.travis.yml +22 -0
  6. data/CHANGELOG.md +148 -0
  7. data/Gemfile +11 -0
  8. data/Gemfile.lock +137 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +1118 -0
  11. data/Rakefile +12 -0
  12. data/lib/install/angular.rb +22 -0
  13. data/lib/install/bin/webpack-dev-server.tt +43 -0
  14. data/lib/install/bin/webpack.tt +27 -0
  15. data/lib/install/config/.babelrc +17 -0
  16. data/lib/install/config/.postcssrc.yml +3 -0
  17. data/lib/install/config/loaders/core/assets.js +12 -0
  18. data/lib/install/config/loaders/core/babel.js +5 -0
  19. data/lib/install/config/loaders/core/coffee.js +4 -0
  20. data/lib/install/config/loaders/core/erb.js +9 -0
  21. data/lib/install/config/loaders/core/sass.js +15 -0
  22. data/lib/install/config/loaders/installers/angular.js +4 -0
  23. data/lib/install/config/loaders/installers/elm.js +20 -0
  24. data/lib/install/config/loaders/installers/react.js +5 -0
  25. data/lib/install/config/loaders/installers/vue.js +41 -0
  26. data/lib/install/config/webpack/configuration.js +35 -0
  27. data/lib/install/config/webpack/development.js +32 -0
  28. data/lib/install/config/webpack/production.js +35 -0
  29. data/lib/install/config/webpack/shared.js +58 -0
  30. data/lib/install/config/webpack/test.js +6 -0
  31. data/lib/install/config/webpacker.yml +38 -0
  32. data/lib/install/elm.rb +33 -0
  33. data/lib/install/examples/angular/hello_angular.js +7 -0
  34. data/lib/install/examples/angular/hello_angular/app/app.component.ts +9 -0
  35. data/lib/install/examples/angular/hello_angular/app/app.module.ts +16 -0
  36. data/lib/install/examples/angular/hello_angular/index.ts +6 -0
  37. data/lib/install/examples/angular/hello_angular/polyfills.ts +19 -0
  38. data/lib/install/examples/angular/tsconfig.json +19 -0
  39. data/lib/install/examples/elm/Main.elm +54 -0
  40. data/lib/install/examples/elm/hello_elm.js +11 -0
  41. data/lib/install/examples/react/.babelrc +6 -0
  42. data/lib/install/examples/react/hello_react.jsx +26 -0
  43. data/lib/install/examples/vue/app.vue +22 -0
  44. data/lib/install/examples/vue/hello_vue.js +15 -0
  45. data/lib/install/javascript/packs/application.js +10 -0
  46. data/lib/install/react.rb +36 -0
  47. data/lib/install/template.rb +44 -0
  48. data/lib/install/vue.rb +19 -0
  49. data/lib/tasks/installers.rake +22 -0
  50. data/lib/tasks/webpacker.rake +19 -0
  51. data/lib/tasks/webpacker/check_node.rake +20 -0
  52. data/lib/tasks/webpacker/check_webpack_binstubs.rake +11 -0
  53. data/lib/tasks/webpacker/check_yarn.rake +15 -0
  54. data/lib/tasks/webpacker/clobber.rake +17 -0
  55. data/lib/tasks/webpacker/compile.rake +38 -0
  56. data/lib/tasks/webpacker/install.rake +23 -0
  57. data/lib/tasks/webpacker/verify_install.rake +18 -0
  58. data/lib/tasks/webpacker/yarn_install.rake +6 -0
  59. data/lib/webpacker-legacy.rb +2 -0
  60. data/lib/webpacker.rb +40 -0
  61. data/lib/webpacker/compiler.rb +20 -0
  62. data/lib/webpacker/configuration.rb +60 -0
  63. data/lib/webpacker/env.rb +23 -0
  64. data/lib/webpacker/file_loader.rb +24 -0
  65. data/lib/webpacker/helper.rb +62 -0
  66. data/lib/webpacker/manifest.rb +51 -0
  67. data/lib/webpacker/railtie.rb +18 -0
  68. data/lib/webpacker/version.rb +3 -0
  69. data/package.json +31 -0
  70. data/test/configuration_test.rb +32 -0
  71. data/test/env_test.rb +14 -0
  72. data/test/helper_test.rb +23 -0
  73. data/test/manifest_test.rb +30 -0
  74. data/test/test_app/config/secrets.yml +5 -0
  75. data/test/test_app/public/packs/manifest.json +4 -0
  76. data/test/webpacker_test.rb +15 -0
  77. data/webpacker.gemspec +23 -0
  78. data/yarn.lock +1014 -0
  79. metadata +200 -0
@@ -0,0 +1,11 @@
1
+ namespace :webpacker do
2
+ desc "Verifies that bin/webpack & bin/webpack-dev-server are present."
3
+ task :check_webpack_binstubs do
4
+ unless File.exist?("bin/webpack") && File.exist?("bin/webpack-dev-server")
5
+ puts "Webpack binstubs not found.\n"\
6
+ "Make sure the bin directory or binstubs are not included in .gitignore\n"\
7
+ "Exiting!"
8
+ exit!
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ namespace :webpacker do
2
+ desc "Verifies if yarn is installed"
3
+ task :check_yarn do
4
+ required_yarn_version = "0.20.1"
5
+
6
+ begin
7
+ yarn_version = `yarn --version`
8
+
9
+ raise Errno::ENOENT if yarn_version.blank? || Gem::Version.new(yarn_version) < Gem::Version.new(required_yarn_version)
10
+ rescue Errno::ENOENT
11
+ puts "Webpacker requires Yarn version >= #{required_yarn_version}. Please download and install the latest version from https://yarnpkg.com/lang/en/docs/install/"
12
+ puts "Exiting!" && exit!
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require "webpacker/configuration"
2
+
3
+ namespace :webpacker do
4
+ desc "Remove the webpack compiled output directory"
5
+ task clobber: ["webpacker:verify_install", :environment] do
6
+ output_path = Webpacker::Configuration.output_path
7
+ FileUtils.rm_r(output_path) if File.exist?(output_path)
8
+ puts "Removed webpack output path directory #{output_path}"
9
+ end
10
+ end
11
+
12
+ # Run clobber if the assets:clobber is run
13
+ if Rake::Task.task_defined?("assets:clobber")
14
+ Rake::Task["assets:clobber"].enhance do
15
+ Rake::Task["webpacker:clobber"].invoke
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+
3
+ require "open3"
4
+ require "webpacker/env"
5
+ require "webpacker/configuration"
6
+
7
+ namespace :webpacker do
8
+ desc "Compile javascript packs using webpack for production with digests"
9
+ task compile: ["webpacker:verify_install", :environment] do
10
+ puts "[Webpacker] Compiling assets 🎉"
11
+
12
+ asset_host = ActionController::Base.helpers.compute_asset_host
13
+ env = { "NODE_ENV" => Webpacker.env, "ASSET_HOST" => asset_host }.freeze
14
+
15
+ stdout_str, stderr_str, status = Open3.capture3(env, "./bin/webpack")
16
+
17
+ if status.success?
18
+ $stdout.puts "\e[32m[Webpacker] Compiled digests for all packs in #{Webpacker::Configuration.entry_path}:\e[0m"
19
+ $stdout.puts "\e[32m#{JSON.parse(File.read(Webpacker::Configuration.manifest_path))}\e[0m"
20
+ else
21
+ $stdout.puts "[Webpacker] Compilation Failed"
22
+ $stdout.puts "\e[31m#{stdout_str}\e[0m"
23
+ $stderr.puts "\e[31m#{stderr_str}\e[0m"
24
+ exit!
25
+ end
26
+ end
27
+ end
28
+
29
+ # Compile packs after we've compiled all other assets during precompilation
30
+ if Rake::Task.task_defined?("assets:precompile")
31
+ Rake::Task["assets:precompile"].enhance do
32
+ unless Rake::Task.task_defined?("yarn:install")
33
+ # For Rails < 5.1
34
+ Rake::Task["webpacker:yarn_install"].invoke
35
+ end
36
+ Rake::Task["webpacker:compile"].invoke
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ WEBPACKER_APP_TEMPLATE_PATH = File.expand_path("../../install/template.rb", File.dirname(__FILE__))
2
+
3
+ if Webpacker.rails_less_than_42?
4
+ namespace :rails do
5
+ desc 'Redefined by Webpacker'
6
+ task template: :environment do
7
+ puts 'Adding environment for legacy apps'
8
+ end
9
+ end
10
+ end
11
+
12
+ namespace :webpacker do
13
+ desc "Install webpacker in this application"
14
+ task install: [:check_node, :check_yarn] do
15
+ if Rails::VERSION::MAJOR >= 5
16
+ exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
17
+ elsif Rails::VERSION::MAJOR >= 4
18
+ exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
19
+ else
20
+ exec "rake rails:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require "webpacker/configuration"
4
+
5
+ namespace :webpacker do
6
+ desc "Verifies if webpacker is installed"
7
+ task verify_install: [:check_node, :check_yarn, :check_webpack_binstubs] do
8
+ if File.exist?(Webpacker::Configuration.file_path)
9
+ puts "Webpacker is installed 🎉 🍰"
10
+ puts "Using #{Webpacker::Configuration.file_path} file for setting up webpack paths"
11
+ else
12
+ puts "Configuration config/webpacker.yml file not found. \n"\
13
+ "Make sure webpacker:install is run successfully before " \
14
+ "running dependent tasks"
15
+ exit!
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ namespace :webpacker do
2
+ desc "Support for older Rails versions.Install all JavaScript dependencies as specified via Yarn"
3
+ task :yarn_install, [:arg1, :arg2] do |task, args|
4
+ system "yarn #{args[:arg1]} #{args[:arg2]}"
5
+ end
6
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require 'webpacker'
data/lib/webpacker.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Webpacker
2
+ extend self
3
+
4
+ def bootstrap
5
+ Webpacker::Env.load
6
+ Webpacker::Configuration.load
7
+ Webpacker::Manifest.load
8
+ end
9
+
10
+ def compile
11
+ Webpacker::Compiler.compile
12
+ Webpacker::Manifest.load
13
+ end
14
+
15
+ def env
16
+ Webpacker::Env.current.inquiry
17
+ end
18
+
19
+ def self.rails32?
20
+ Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 2
21
+ end
22
+
23
+ def self.rails40?
24
+ Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR == 0
25
+ end
26
+
27
+ def self.rails41?
28
+ Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR == 1
29
+ end
30
+
31
+ def self.rails_less_than_42?
32
+ rails32? || rails40? || rails41?
33
+ end
34
+ end
35
+
36
+ require "webpacker/env"
37
+ require "webpacker/configuration"
38
+ require "webpacker/manifest"
39
+ require "webpacker/compiler"
40
+ require "webpacker/railtie" if defined?(Rails)
@@ -0,0 +1,20 @@
1
+ require "rake"
2
+
3
+ module Webpacker::Compiler
4
+ extend self
5
+
6
+ def compile
7
+ compile_task.invoke
8
+ compile_task.reenable
9
+ end
10
+
11
+ private
12
+ def compile_task
13
+ @compile_task ||= load_rake_task("webpacker:compile")
14
+ end
15
+
16
+ def load_rake_task(name)
17
+ @load_rakefile ||= Rake.load_rakefile(Rails.root.join("Rakefile"))
18
+ Rake::Task[name]
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ # Loads webpacker configuration from config/webpacker.yml
2
+
3
+ require "webpacker/file_loader"
4
+
5
+ class Webpacker::Configuration < Webpacker::FileLoader
6
+ class << self
7
+ def entry_path
8
+ source_path.join(fetch(:source_entry_path))
9
+ end
10
+
11
+ def output_path
12
+ public_path.join(fetch(:public_output_path))
13
+ end
14
+
15
+ def manifest_path
16
+ output_path.join("manifest.json")
17
+ end
18
+
19
+ def source_path
20
+ Rails.root.join(source)
21
+ end
22
+
23
+ def public_path
24
+ Rails.root.join("public")
25
+ end
26
+
27
+ def file_path(options = {})
28
+ root = options[:root] || Rails.root
29
+ root.join("config/webpacker.yml")
30
+ end
31
+
32
+ def default_file_path
33
+ file_path(root: Pathname.new(File.dirname(__FILE__)).join("../install"))
34
+ end
35
+
36
+ def source
37
+ fetch(:source_path)
38
+ end
39
+
40
+ def fetch(key)
41
+ data.fetch(key, defaults[key])
42
+ end
43
+
44
+ def data
45
+ load if Webpacker.env.development?
46
+ raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance
47
+ instance.data
48
+ end
49
+
50
+ def defaults
51
+ @defaults ||= HashWithIndifferentAccess.new(YAML.load(default_file_path.read)["default"])
52
+ end
53
+ end
54
+
55
+ private
56
+ def load
57
+ return super unless File.exist?(@path)
58
+ HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Webpacker.env])
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ # Singleton registry for determining NODE_ENV from config/webpacker.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 file_path
12
+ Rails.root.join("config", "webpacker.yml")
13
+ end
14
+ end
15
+
16
+ private
17
+ def load
18
+ environments = File.exist?(@path) ? YAML.load(File.read(@path)).keys : [].freeze
19
+ return ENV["NODE_ENV"] if environments.include?(ENV["NODE_ENV"])
20
+ return Rails.env if environments.include?(Rails.env)
21
+ "production"
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ # Provides a base singleton-configuration pattern for loading a file, given a path
2
+ class Webpacker::FileLoader
3
+ class NotFoundError < StandardError; end
4
+ class FileLoaderError < StandardError; end
5
+
6
+ class_attribute :instance
7
+ attr_accessor :data
8
+
9
+ class << self
10
+ def load(path = file_path)
11
+ self.instance = new(path)
12
+ end
13
+ end
14
+
15
+ private
16
+ def initialize(path)
17
+ @path = path
18
+ @data = load
19
+ end
20
+
21
+ def load
22
+ {}.freeze
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ module Webpacker::Helper
2
+ # Computes the full path for a given webpacker asset.
3
+ # Return relative path using manifest.json and passes it to asset_url helper
4
+ # This will use asset_path internally, so most of their behaviors will be the same.
5
+ # Examples:
6
+ #
7
+ # In development mode:
8
+ # <%= asset_pack_path 'calendar.js' %> # => "/packs/calendar.js"
9
+ # In production mode:
10
+ # <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
11
+ def asset_pack_path(name, *options)
12
+ asset_path(Webpacker::Manifest.lookup(name), *options)
13
+ end
14
+ # Creates a script tag that references the named pack file, as compiled by Webpack per the entries list
15
+ # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
16
+ # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
17
+ #
18
+ # Examples:
19
+ #
20
+ # # In development mode:
21
+ # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
22
+ # <script src="/packs/calendar.js" data-turbolinks-track="reload"></script>
23
+ #
24
+ # # In production mode:
25
+ # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
26
+ # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
27
+ def javascript_pack_tag(name, *options)
28
+ javascript_include_tag(Webpacker::Manifest.lookup("#{name}#{compute_asset_extname(name, type: :javascript)}"), *options)
29
+ end
30
+
31
+ # Creates a link tag that references the named pack file, as compiled by Webpack per the entries list
32
+ # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
33
+ # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
34
+ #
35
+ # Examples:
36
+ #
37
+ # # In development mode:
38
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
39
+ # <link rel="stylesheet" media="screen" href="/packs/calendar.css" data-turbolinks-track="reload" />
40
+ #
41
+ # # In production mode:
42
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
43
+ # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
44
+ def stylesheet_pack_tag(name, *options)
45
+ stylesheet_link_tag(Webpacker::Manifest.lookup("#{name}#{compute_asset_extname(name, type: :stylesheet)}"), *options)
46
+ end
47
+
48
+ def compute_asset_extname(source, options = {})
49
+ return if options[:extname] == false
50
+ extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
51
+ if extname && File.extname(source) != extname
52
+ extname
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ ASSET_EXTENSIONS = {
59
+ javascript: ".js",
60
+ stylesheet: ".css"
61
+ }
62
+ end
@@ -0,0 +1,51 @@
1
+ # Singleton registry for accessing the packs path using generated manifest.
2
+ # This allows javascript_pack_tag, stylesheet_pack_tag, asset_pack_path to take a reference to,
3
+ # say, "calendar.js" or "calendar.css" and turn it into "/packs/calendar.js" or
4
+ # "/packs/calendar.css" in development. In production mode, it returns compiles
5
+ # files, # "/packs/calendar-1016838bab065ae1e314.js" and
6
+ # "/packs/calendar-1016838bab065ae1e314.css" for long-term caching
7
+
8
+ require "webpacker/file_loader"
9
+
10
+ class Webpacker::Manifest < Webpacker::FileLoader
11
+ class << self
12
+ def file_path
13
+ Webpacker::Configuration.manifest_path
14
+ end
15
+
16
+ def lookup(name)
17
+ load if Webpacker.env.development?
18
+
19
+ if Webpacker.env.test?
20
+ find(name) || compile_and_find!(name)
21
+ else
22
+ find!(name)
23
+ end
24
+ end
25
+
26
+ def lookup_path(name)
27
+ Rails.root.join(File.join(Webpacker::Configuration.public_path, lookup(name)))
28
+ end
29
+
30
+ private
31
+ def find(name)
32
+ instance.data[name.to_s] if instance
33
+ end
34
+
35
+ def find!(name)
36
+ raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Manifest.load must be called first") unless instance
37
+ instance.data[name.to_s] || raise(Webpacker::FileLoader::NotFoundError.new("Can't find #{name} in #{file_path}. Is webpack still compiling?"))
38
+ end
39
+
40
+ def compile_and_find!(name)
41
+ Webpacker.compile
42
+ find!(name)
43
+ end
44
+ end
45
+
46
+ private
47
+ def load
48
+ return super unless File.exist?(@path)
49
+ JSON.parse(File.read(@path))
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ require "rails/railtie"
2
+
3
+ require "webpacker/helper"
4
+
5
+ class Webpacker::Engine < ::Rails::Engine
6
+ initializer :webpacker do |app|
7
+ ActiveSupport.on_load :action_controller do
8
+ ActionController::Base.helper Webpacker::Helper
9
+ end
10
+
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)
17
+ end
18
+ end