webpacker-jets 3.2.100

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 (136) hide show
  1. checksums.yaml +7 -0
  2. data/.eslintignore +4 -0
  3. data/.eslintrc.js +14 -0
  4. data/.gitignore +6 -0
  5. data/.rubocop.yml +124 -0
  6. data/.travis.yml +32 -0
  7. data/CHANGELOG.md +523 -0
  8. data/Gemfile +13 -0
  9. data/Gemfile.lock +154 -0
  10. data/MIT-LICENSE +20 -0
  11. data/README.md +445 -0
  12. data/Rakefile +12 -0
  13. data/docs/assets.md +106 -0
  14. data/docs/cloud9.md +310 -0
  15. data/docs/css.md +116 -0
  16. data/docs/deployment.md +74 -0
  17. data/docs/docker.md +49 -0
  18. data/docs/env.md +65 -0
  19. data/docs/es6.md +53 -0
  20. data/docs/folder-structure.md +66 -0
  21. data/docs/misc.md +23 -0
  22. data/docs/props.md +223 -0
  23. data/docs/testing.md +128 -0
  24. data/docs/troubleshooting.md +119 -0
  25. data/docs/typescript.md +116 -0
  26. data/docs/webpack-dev-server.md +92 -0
  27. data/docs/webpack.md +297 -0
  28. data/docs/yarn.md +12 -0
  29. data/exe/webpack +8 -0
  30. data/exe/webpack-dev-server +8 -0
  31. data/gemfiles/Gemfile-rails-edge +13 -0
  32. data/gemfiles/Gemfile-rails.4.2.x +10 -0
  33. data/gemfiles/Gemfile-rails.5.0.x +10 -0
  34. data/gemfiles/Gemfile-rails.5.1.x +10 -0
  35. data/lib/install/angular.rb +27 -0
  36. data/lib/install/coffee.rb +22 -0
  37. data/lib/install/config/.babelrc +18 -0
  38. data/lib/install/config/.postcssrc.yml +3 -0
  39. data/lib/install/config/webpack/development.js +3 -0
  40. data/lib/install/config/webpack/environment.js +3 -0
  41. data/lib/install/config/webpack/production.js +3 -0
  42. data/lib/install/config/webpack/staging.js +3 -0
  43. data/lib/install/config/webpack/test.js +3 -0
  44. data/lib/install/config/webpacker.yml +79 -0
  45. data/lib/install/elm.rb +38 -0
  46. data/lib/install/erb.rb +22 -0
  47. data/lib/install/examples/angular/hello_angular.js +7 -0
  48. data/lib/install/examples/angular/hello_angular/app/app.component.ts +9 -0
  49. data/lib/install/examples/angular/hello_angular/app/app.module.ts +16 -0
  50. data/lib/install/examples/angular/hello_angular/index.ts +8 -0
  51. data/lib/install/examples/angular/hello_angular/polyfills.ts +73 -0
  52. data/lib/install/examples/angular/tsconfig.json +19 -0
  53. data/lib/install/examples/coffee/hello_coffee.coffee +4 -0
  54. data/lib/install/examples/elm/Main.elm +54 -0
  55. data/lib/install/examples/elm/hello_elm.js +12 -0
  56. data/lib/install/examples/erb/hello_erb.js.erb +6 -0
  57. data/lib/install/examples/react/.babelrc +6 -0
  58. data/lib/install/examples/react/hello_react.jsx +26 -0
  59. data/lib/install/examples/vue/app.vue +22 -0
  60. data/lib/install/examples/vue/hello_vue.js +70 -0
  61. data/lib/install/javascript/packs/application.js +10 -0
  62. data/lib/install/loaders/coffee.js +6 -0
  63. data/lib/install/loaders/elm.js +23 -0
  64. data/lib/install/loaders/erb.js +11 -0
  65. data/lib/install/loaders/typescript.js +6 -0
  66. data/lib/install/loaders/vue.js +13 -0
  67. data/lib/install/react.rb +28 -0
  68. data/lib/install/template.rb +37 -0
  69. data/lib/install/vue.rb +26 -0
  70. data/lib/tasks/installers.rake +24 -0
  71. data/lib/tasks/webpacker.rake +22 -0
  72. data/lib/tasks/webpacker/check_binstubs.rake +12 -0
  73. data/lib/tasks/webpacker/check_node.rake +24 -0
  74. data/lib/tasks/webpacker/check_yarn.rake +24 -0
  75. data/lib/tasks/webpacker/clobber.rake +16 -0
  76. data/lib/tasks/webpacker/compile.rake +41 -0
  77. data/lib/tasks/webpacker/install.rake +13 -0
  78. data/lib/tasks/webpacker/verify_install.rake +16 -0
  79. data/lib/tasks/webpacker/yarn_install.rake +6 -0
  80. data/lib/webpacker.rb +32 -0
  81. data/lib/webpacker/commands.rb +23 -0
  82. data/lib/webpacker/compiler.rb +88 -0
  83. data/lib/webpacker/configuration.rb +87 -0
  84. data/lib/webpacker/dev_server.rb +61 -0
  85. data/lib/webpacker/dev_server_proxy.rb +26 -0
  86. data/lib/webpacker/dev_server_runner.rb +51 -0
  87. data/lib/webpacker/helper.rb +66 -0
  88. data/lib/webpacker/instance.rb +44 -0
  89. data/lib/webpacker/manifest.rb +75 -0
  90. data/lib/webpacker/railtie.rb +82 -0
  91. data/lib/webpacker/rake_tasks.rb +6 -0
  92. data/lib/webpacker/runner.rb +22 -0
  93. data/lib/webpacker/version.rb +9 -0
  94. data/lib/webpacker/webpack_runner.rb +15 -0
  95. data/package.json +67 -0
  96. data/package/__tests__/environment.js +74 -0
  97. data/package/config.js +34 -0
  98. data/package/config_types/__tests__/config_list.js +123 -0
  99. data/package/config_types/__tests__/config_object.js +43 -0
  100. data/package/config_types/config_list.js +85 -0
  101. data/package/config_types/config_object.js +55 -0
  102. data/package/config_types/index.js +7 -0
  103. data/package/environment.js +116 -0
  104. data/package/environments/development.js +46 -0
  105. data/package/environments/production.js +37 -0
  106. data/package/environments/test.js +3 -0
  107. data/package/index.js +20 -0
  108. data/package/rules/babel.js +13 -0
  109. data/package/rules/css.js +39 -0
  110. data/package/rules/file.js +13 -0
  111. data/package/rules/index.js +11 -0
  112. data/package/rules/sass.js +15 -0
  113. data/package/utils/__tests__/deep_assign.js +11 -0
  114. data/package/utils/__tests__/deep_merge.js +10 -0
  115. data/package/utils/__tests__/objectify.js +9 -0
  116. data/package/utils/deep_assign.js +22 -0
  117. data/package/utils/deep_merge.js +23 -0
  118. data/package/utils/helpers.js +32 -0
  119. data/package/utils/objectify.js +4 -0
  120. data/test/command_test.rb +27 -0
  121. data/test/compiler_test.rb +35 -0
  122. data/test/configuration_test.rb +69 -0
  123. data/test/dev_server_test.rb +24 -0
  124. data/test/helper_test.rb +52 -0
  125. data/test/manifest_test.rb +28 -0
  126. data/test/rake_tasks_test.rb +29 -0
  127. data/test/test_app/Rakefile +3 -0
  128. data/test/test_app/app/javascript/packs/application.js +10 -0
  129. data/test/test_app/config/application.rb +11 -0
  130. data/test/test_app/config/environment.rb +4 -0
  131. data/test/test_app/config/webpacker.yml +65 -0
  132. data/test/test_app/public/packs/manifest.json +6 -0
  133. data/test/test_helper.rb +29 -0
  134. data/webpacker-jets.gemspec +29 -0
  135. data/yarn.lock +5768 -0
  136. metadata +250 -0
@@ -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 do
4
+ system "yarn install --no-progress --production"
5
+ end
6
+ end
data/lib/webpacker.rb ADDED
@@ -0,0 +1,32 @@
1
+ # webpack relies on rails which uses these core_ext but since
2
+ # we're not entirely requiring rails we need to require all the core_ext
3
+ # individually, yeah a bit of a pain
4
+ require "active_support/core_ext/object"
5
+ require "active_support/core_ext/hash"
6
+ require "active_support/core_ext/module/attribute_accessors"
7
+ require "active_support/logger"
8
+ require "active_support/tagged_logging"
9
+
10
+ module Webpacker
11
+ extend self
12
+
13
+ def instance=(instance)
14
+ @instance = instance
15
+ end
16
+
17
+ def instance
18
+ @instance ||= Webpacker::Instance.new
19
+ end
20
+
21
+ delegate :logger, :logger=, :env, to: :instance
22
+ delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance
23
+ delegate :bootstrap, :clobber, :compile, to: :commands
24
+ end
25
+
26
+ require "webpacker/instance"
27
+ require "webpacker/configuration"
28
+ require "webpacker/manifest"
29
+ require "webpacker/compiler"
30
+ require "webpacker/commands"
31
+ require "webpacker/dev_server"
32
+ require "webpacker/rake_tasks"
@@ -0,0 +1,23 @@
1
+ class Webpacker::Commands
2
+ delegate :config, :compiler, :manifest, to: :@webpacker
3
+
4
+ def initialize(webpacker)
5
+ @webpacker = webpacker
6
+ end
7
+
8
+ def clobber
9
+ config.public_output_path.rmtree if config.public_output_path.exist?
10
+ config.cache_path.rmtree if config.cache_path.exist?
11
+ end
12
+
13
+ def bootstrap
14
+ config.refresh
15
+ manifest.refresh
16
+ end
17
+
18
+ def compile
19
+ compiler.compile.tap do |success|
20
+ manifest.refresh if success
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,88 @@
1
+ require "open3"
2
+ require "digest/sha1"
3
+
4
+ class Webpacker::Compiler
5
+ # Additional paths that test compiler needs to watch
6
+ # Webpacker::Compiler.watched_paths << 'bower_components'
7
+ cattr_accessor(:watched_paths) { [] }
8
+
9
+ # Additional environment variables that the compiler is being run with
10
+ # Webpacker::Compiler.env['FRONTEND_API_KEY'] = 'your_secret_key'
11
+ cattr_accessor(:env) { {} }
12
+
13
+ delegate :config, :logger, to: :@webpacker
14
+
15
+ def initialize(webpacker)
16
+ @webpacker = webpacker
17
+ end
18
+
19
+ def compile
20
+ if stale?
21
+ record_compilation_digest
22
+ run_webpack
23
+ else
24
+ true
25
+ end
26
+ end
27
+
28
+ # Returns true if all the compiled packs are up to date with the underlying asset files.
29
+ def fresh?
30
+ watched_files_digest == last_compilation_digest
31
+ end
32
+
33
+ # Returns true if the compiled packs are out of date with the underlying asset files.
34
+ def stale?
35
+ !fresh?
36
+ end
37
+
38
+ private
39
+ def last_compilation_digest
40
+ compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist?
41
+ end
42
+
43
+ def watched_files_digest
44
+ files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
45
+ Digest::SHA1.hexdigest(files.map { |f| "#{File.basename(f)}/#{File.mtime(f).utc.to_i}" }.join("/"))
46
+ end
47
+
48
+ def record_compilation_digest
49
+ config.cache_path.mkpath
50
+ compilation_digest_path.write(watched_files_digest)
51
+ end
52
+
53
+ def run_webpack
54
+ logger.info "Compiling…"
55
+
56
+ sterr, stdout, status = Open3.capture3(webpack_env, "bundle exec webpack")
57
+
58
+ if status.success?
59
+ logger.info "Compiled all packs in #{config.public_output_path}"
60
+ else
61
+ logger.error "Compilation failed:\n#{sterr}\n#{stdout}"
62
+ end
63
+
64
+ status.success?
65
+ end
66
+
67
+ def default_watched_paths
68
+ [
69
+ *config.resolved_paths_globbed,
70
+ "#{config.source_path.relative_path_from(Jets.root)}/**/*",
71
+ "yarn.lock", "package.json",
72
+ "config/webpack/**/*"
73
+ ].freeze
74
+ end
75
+
76
+ def compilation_digest_path
77
+ config.cache_path.join(".last-compilation-digest-#{Webpacker.env}")
78
+ end
79
+
80
+ def webpack_env
81
+ begin
82
+ asset_host = ActionController::Base.helpers.compute_asset_host
83
+ rescue NameError
84
+ asset_host = nil
85
+ end
86
+ env.merge("NODE_ENV" => @webpacker.env, "WEBPACKER_ASSET_HOST" => asset_host)
87
+ end
88
+ end
@@ -0,0 +1,87 @@
1
+ class Webpacker::Configuration
2
+ delegate :root_path, :config_path, :env, to: :@webpacker
3
+
4
+ def initialize(webpacker)
5
+ @webpacker = webpacker
6
+ end
7
+
8
+ def refresh
9
+ @data = load
10
+ end
11
+
12
+ def dev_server
13
+ fetch(:dev_server)
14
+ end
15
+
16
+ def compile?
17
+ fetch(:compile)
18
+ end
19
+
20
+ def source_path
21
+ root_path.join(fetch(:source_path))
22
+ end
23
+
24
+ def resolved_paths
25
+ fetch(:resolved_paths)
26
+ end
27
+
28
+ def resolved_paths_globbed
29
+ resolved_paths.map { |p| "#{p}/**/*" }
30
+ end
31
+
32
+ def source_entry_path
33
+ source_path.join(fetch(:source_entry_path))
34
+ end
35
+
36
+ def public_path
37
+ root_path.join("public")
38
+ end
39
+
40
+ def public_output_path
41
+ public_path.join(fetch(:public_output_path))
42
+ end
43
+
44
+ def public_manifest_path
45
+ public_output_path.join("manifest.json")
46
+ end
47
+
48
+ def cache_manifest?
49
+ fetch(:cache_manifest)
50
+ end
51
+
52
+ def cache_path
53
+ root_path.join(fetch(:cache_path))
54
+ end
55
+
56
+ def extensions
57
+ fetch(:extensions)
58
+ end
59
+
60
+ private
61
+ def fetch(key)
62
+ data.fetch(key, defaults[key])
63
+ end
64
+
65
+ def data
66
+ @data ||= load
67
+ end
68
+
69
+ def load
70
+ YAML.load(config_path.read)[env].deep_symbolize_keys
71
+
72
+ rescue Errno::ENOENT => e
73
+ raise "Webpacker configuration file not found #{config_path}. " \
74
+ "Please run rails webpacker:install " \
75
+ "Error: #{e.message}"
76
+
77
+ rescue Psych::SyntaxError => e
78
+ raise "YAML syntax error occurred while parsing #{config_path}. " \
79
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
80
+ "Error: #{e.message}"
81
+ end
82
+
83
+ def defaults
84
+ @defaults ||= \
85
+ HashWithIndifferentAccess.new(YAML.load_file(File.expand_path("../../install/config/webpacker.yml", __FILE__))[env])
86
+ end
87
+ end
@@ -0,0 +1,61 @@
1
+ class Webpacker::DevServer
2
+ # Configure dev server connection timeout (in seconds), default: 0.01
3
+ # Webpacker.dev_server.connect_timeout = 1
4
+ cattr_accessor(:connect_timeout) { 0.01 }
5
+
6
+ delegate :config, to: :@webpacker
7
+
8
+ def initialize(webpacker)
9
+ @webpacker = webpacker
10
+ end
11
+
12
+ def running?
13
+ Socket.tcp(host, port, connect_timeout: connect_timeout).close
14
+ true
15
+ rescue
16
+ false
17
+ end
18
+
19
+ def hot_module_replacing?
20
+ case fetch(:hmr)
21
+ when true, "true"
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ def host
29
+ fetch(:host)
30
+ end
31
+
32
+ def port
33
+ fetch(:port)
34
+ end
35
+
36
+ def https?
37
+ case fetch(:https)
38
+ when true, "true"
39
+ true
40
+ else
41
+ false
42
+ end
43
+ end
44
+
45
+ def protocol
46
+ https? ? "https" : "http"
47
+ end
48
+
49
+ def host_with_port
50
+ "#{host}:#{port}"
51
+ end
52
+
53
+ private
54
+ def fetch(key)
55
+ ENV["WEBPACKER_DEV_SERVER_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
56
+ end
57
+
58
+ def defaults
59
+ config.send(:defaults)[:dev_server]
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+ require "rack/proxy"
2
+
3
+ class Webpacker::DevServerProxy < Rack::Proxy
4
+ def rewrite_response(response)
5
+ _status, headers, _body = response
6
+ headers.delete "transfer-encoding"
7
+ headers.delete "content-length" if Webpacker.dev_server.running? && Webpacker.dev_server.https?
8
+ response
9
+ end
10
+
11
+ def perform_request(env)
12
+ if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && Webpacker.dev_server.running?
13
+ env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = Webpacker.dev_server.host_with_port
14
+ env["SCRIPT_NAME"] = ""
15
+
16
+ super(env)
17
+ else
18
+ @app.call(env)
19
+ end
20
+ end
21
+
22
+ private
23
+ def public_output_uri_path
24
+ Webpacker.config.public_output_path.relative_path_from(Webpacker.config.public_path)
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ require "shellwords"
2
+ require "yaml"
3
+ require "socket"
4
+ require "webpacker/runner"
5
+
6
+ module Webpacker
7
+ class DevServerRunner < Webpacker::Runner
8
+ def run
9
+ load_config
10
+ detect_port!
11
+ execute_cmd
12
+ end
13
+
14
+ private
15
+ def load_config
16
+ @config_file = File.join(@app_path, "config/webpacker.yml")
17
+ dev_server = YAML.load_file(@config_file)[ENV["JETS_ENV"]]["dev_server"]
18
+
19
+ @hostname = dev_server["host"]
20
+ @port = dev_server["port"]
21
+ @pretty = dev_server.fetch("pretty", true)
22
+
23
+ rescue Errno::ENOENT, NoMethodError
24
+ $stdout.puts "webpack dev_server configuration not found in #{@config_file}."
25
+ $stdout.puts "Please run jets webpacker:install to install Webpacker"
26
+ exit!
27
+ end
28
+
29
+ def detect_port!
30
+ server = TCPServer.new(@hostname, @port)
31
+ server.close
32
+
33
+ rescue Errno::EADDRINUSE
34
+ $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
35
+ exit!
36
+ end
37
+
38
+ def execute_cmd
39
+ env = { "NODE_PATH" => @node_modules_path.shellescape }
40
+ cmd = [
41
+ "#{@node_modules_path}/.bin/webpack-dev-server",
42
+ "--config", @webpack_config
43
+ ]
44
+ cmd += ["--progress", "--color"] if @pretty
45
+
46
+ Dir.chdir(@app_path) do
47
+ exec env, *cmd
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,66 @@
1
+ module Webpacker::Helper
2
+ # Computes the relative path for a given Webpacker asset.
3
+ # Return relative path using manifest.json and passes it to asset_path helper
4
+ # This will use asset_path internally, so most of their behaviors will be the same.
5
+ #
6
+ # Example:
7
+ #
8
+ # <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
9
+ def asset_pack_path(name, **options)
10
+ asset_path(Webpacker.manifest.lookup!(name), **options)
11
+ end
12
+
13
+ # Computes the absolute path for a given Webpacker asset.
14
+ # Return absolute path using manifest.json and passes it to asset_url helper
15
+ # This will use asset_url internally, so most of their behaviors will be the same.
16
+ #
17
+ # Example:
18
+ #
19
+ # <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
20
+ def asset_pack_url(name, **options)
21
+ asset_url(Webpacker.manifest.lookup!(name), **options)
22
+ end
23
+
24
+ # Creates a script tag that references the named pack file, as compiled by webpack per the entries list
25
+ # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
26
+ # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
27
+ #
28
+ # Example:
29
+ #
30
+ # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
31
+ # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
32
+ def javascript_pack_tag(*names, **options)
33
+ javascript_include_tag(*sources_from_pack_manifest(names, type: :javascript), **options)
34
+ end
35
+
36
+ # Creates a link tag that references the named pack file, as compiled by webpack per the entries list
37
+ # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
38
+ # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
39
+ #
40
+ # Note: If the development server is running and hot module replacement is active, this will return nothing.
41
+ # In that setup you need to configure your styles to be inlined in your JavaScript for hot reloading.
42
+ #
43
+ # Examples:
44
+ #
45
+ # # In development mode with hot module replacement:
46
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
47
+ # nil
48
+ #
49
+ # # In production mode:
50
+ # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
51
+ # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
52
+ def stylesheet_pack_tag(*names, **options)
53
+ unless Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
54
+ stylesheet_link_tag(*sources_from_pack_manifest(names, type: :stylesheet), **options)
55
+ end
56
+ end
57
+
58
+ private
59
+ def sources_from_pack_manifest(names, type:)
60
+ names.map { |name| Webpacker.manifest.lookup!(pack_name_with_extension(name, type: type)) }
61
+ end
62
+
63
+ def pack_name_with_extension(name, type:)
64
+ "#{name}#{compute_asset_extname(name, type: type)}"
65
+ end
66
+ end