vite_rails 1.0.4 → 1.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09b3f9165e33562bd060ff1c5bc3cb2efb76a68d4de1bc3548a213110df6bba4'
4
- data.tar.gz: 3aac713bfb90f0586c2309217b45d64bc394312ed5fb08be44391d3dcb68d459
3
+ metadata.gz: c1036b7b19ba86a9e76a58b29bac36a0e919bdadeac59846a37a13f5f5d3690b
4
+ data.tar.gz: 51fc54fa2138c581ca9f92182e14faa2108cba30084656718f7792c3bf5947b4
5
5
  SHA512:
6
- metadata.gz: '08b9ee75915396e16078094f1870211e5667cc3f9edf9502844be884a556a6c9378bbcdef8374498761643fab94c0302930d18737d98ceaae013c0da0ac9f2d6'
7
- data.tar.gz: 482461f60d07e91c99084f74089d10fe07ac2db20aad1c324a45f5b6fbb1f4f815dd436993c21798c4c18e116d5bee729b83abe08e85d02757637731a242d7b0
6
+ metadata.gz: 13857bbd2f3b308357b82663e140a1630699259274a5bab6c738c84444ab02aba67ef0b361a9598528f60186ec8a3748584885492026a36c928db77976ec7e5b
7
+ data.tar.gz: e15766ff9f599d4f0e18b2e1a30a3cd38182dc36efc479371c474f54ed0866c8451469952afc267f4247351592a3e6651010b2b5db28ac58e99e3bc60deef3f0
@@ -1,3 +1,11 @@
1
- ## Vite Rails 1.0.0
1
+ ## Vite Rails 1.0.5 (2020-01-20)
2
+
3
+ - Automatically add `<link rel="modulepreload">` and `<link rel="stylesheet">` when using `vite_javascript_tag`, which simplifies usage.
4
+
5
+ ## Vite Rails 1.0.4 (2020-01-19)
6
+
7
+ - Remove Vue specific examples from installation templates, to ensure they always run.
8
+
9
+ ## Vite Rails 1.0.0 (2020-01-18)
2
10
 
3
11
  Initial Version
data/README.md CHANGED
@@ -33,7 +33,7 @@
33
33
  - 🤖 Automatic Entrypoint Detection
34
34
  - ⚡️ Hot Reload
35
35
  - ⚙️ Rake Tasks
36
- - 🪝 Hooks to <kbd>assets:precompile</kbd> and friends
36
+ - 🤝 Integrated with <kbd>assets:precompile</kbd> and friends
37
37
  - And more! (detects changes, and builds automatically if Vite is not running)
38
38
 
39
39
  ## Documentation 📖
@@ -22,7 +22,7 @@ class ViteRails
22
22
  cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }
23
23
 
24
24
  class << self
25
- delegate :config, :builder, :manifest, :commands, :dev_server, to: :instance
25
+ delegate :config, :builder, :manifest, :commands, :dev_server, :dev_server_running?, to: :instance
26
26
  delegate :mode, to: :config
27
27
  delegate :bootstrap, :clean, :clobber, :build, to: :commands
28
28
 
@@ -62,6 +62,11 @@ class ViteRails
62
62
  end
63
63
  end
64
64
 
65
+ # Public: Returns true if the Vite development server is running.
66
+ def dev_server_running?
67
+ ViteRails.run_proxy? && dev_server.running?
68
+ end
69
+
65
70
  # Public: Current instance configuration for Vite.
66
71
  def config
67
72
  @config ||= ViteRails::Config.resolve_config
@@ -14,7 +14,7 @@ class ViteRails::DevServerProxy < Rack::Proxy
14
14
 
15
15
  # Rack: Intercept asset requests and send them to the Vite server.
16
16
  def perform_request(env)
17
- if vite_should_handle?(env['REQUEST_URI']) && dev_server.running?
17
+ if vite_should_handle?(env['REQUEST_URI'], env['HTTP_REFERER']) && dev_server.running?
18
18
  env['REQUEST_URI'] = env['REQUEST_URI']
19
19
  .sub(vite_asset_url_prefix, '/')
20
20
  .sub('.ts.js', '.ts') # Patch: Rails helpers always append the extension.
@@ -36,9 +36,11 @@ private
36
36
 
37
37
  delegate :config, :dev_server, to: :@vite_rails
38
38
 
39
- def vite_should_handle?(url)
40
- url.start_with?(vite_asset_url_prefix) || url.start_with?(VITE_DEPENDENCY_PREFIX) ||
41
- url.include?('?t=') # Direct Hot Reload
39
+ def vite_should_handle?(url, referer)
40
+ return true if url.start_with?(vite_asset_url_prefix) # Vite Asset
41
+ return true if url.start_with?(VITE_DEPENDENCY_PREFIX) # Vite Package Asset
42
+ return true if url.include?('?t=') # Hot Reload
43
+ return true if referer && URI.parse(referer).path.start_with?(vite_asset_url_prefix) # Entry Imported from another Entry.
42
44
  end
43
45
 
44
46
  def vite_asset_url_prefix
@@ -2,6 +2,8 @@
2
2
 
3
3
  # Public: Allows to render HTML tags for scripts and styles processed by Vite.
4
4
  module ViteRails::Helper
5
+ DEFAULT_VITE_SKIP_PRELOAD_TAGS = Rails::VERSION::MAJOR <= 5 && Rails::VERSION::MINOR < 2
6
+
5
7
  # Public: Returns the current Vite Rails instance.
6
8
  def current_vite_instance
7
9
  ViteRails.instance
@@ -12,30 +14,49 @@ module ViteRails::Helper
12
14
  # Example:
13
15
  # <%= vite_asset_path 'calendar.css' %> # => "/vite/assets/calendar-1016838bab065ae1e122.css"
14
16
  def vite_asset_path(name, **options)
15
- current_vite_instance.manifest.lookup!(name, **options)
17
+ current_vite_instance.manifest.lookup!(name, **options).fetch('file')
16
18
  end
17
19
 
18
20
  # Public: Renders a <script> tag for the specified Vite entrypoints.
19
- def vite_javascript_tag(*names, type: 'module', **options)
20
- javascript_include_tag(*sources_from_vite_manifest_entrypoints(names, type: :javascript), type: type, **options)
21
+ def vite_javascript_tag(*names,
22
+ type: 'module',
23
+ asset_type: :javascript,
24
+ skip_preload_tags: DEFAULT_VITE_SKIP_PRELOAD_TAGS,
25
+ skip_style_tags: false,
26
+ crossorigin: 'anonymous',
27
+ **options)
28
+ js_entries = names.map { |name| current_vite_instance.manifest.lookup!(name, type: asset_type) }
29
+ js_tags = javascript_include_tag(*js_entries.map { |entry| entry['file'] }, type: type, crossorigin: crossorigin, **options)
30
+
31
+ unless skip_preload_tags || ViteRails.dev_server.running?
32
+ preload_paths = js_entries.flat_map { |entry| entry['imports'] }.compact.uniq
33
+ preload_tags = preload_paths.map { |path| preload_link_tag(path, crossorigin: crossorigin) }
34
+ end
35
+
36
+ unless skip_style_tags || ViteRails.dev_server.running?
37
+ style_paths = names.map { |name| current_vite_instance.manifest.lookup(name, type: :stylesheet)&.fetch('file') }.compact
38
+ style_tags = stylesheet_link_tag(*style_paths)
39
+ end
40
+
41
+ safe_join [js_tags, preload_tags, style_tags]
21
42
  end
22
43
 
23
44
  # Public: Renders a <script> tag for the specified Vite entrypoints.
24
45
  #
25
46
  # NOTE: Because TypeScript is not a valid target in browsers, we only specify
26
47
  # the ts file when running the Vite development server.
27
- def vite_typescript_tag(*names, type: 'module', **options)
28
- javascript_include_tag(*sources_from_vite_manifest_entrypoints(names, type: :typescript), type: type, **options)
48
+ def vite_typescript_tag(*names, **options)
49
+ vite_javascript_tag(*names, asset_type: :typescript, **options)
29
50
  end
30
51
 
31
52
  # Public: Renders a <link> tag for the specified Vite entrypoints.
32
53
  def vite_stylesheet_tag(*names, **options)
33
- stylesheet_link_tag(*sources_from_vite_manifest_entrypoints(names, type: :stylesheet), **options)
54
+ stylesheet_link_tag(*sources_from_vite_manifest(names, type: :stylesheet), **options)
34
55
  end
35
56
 
36
57
  private
37
58
 
38
- def sources_from_vite_manifest_entrypoints(names, type:)
39
- names.flat_map { |name| vite_asset_path(name, type: type) }.uniq
59
+ def sources_from_vite_manifest(names, type:)
60
+ names.map { |name| vite_asset_path(name, type: type) }
40
61
  end
41
62
  end
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # Example:
7
7
  # lookup_entrypoint('calendar', type: :javascript)
8
- # => "/vite/assets/calendar-1016838bab065ae1e314.js"
8
+ # => { "file" => "/vite/assets/calendar-1016838bab065ae1e314.js", "imports" => [] }
9
9
  #
10
10
  # NOTE: Using "autoBuild": true` in `config/vite.json` file will trigger a build
11
11
  # on demand as needed, before performing any lookup.
@@ -29,7 +29,8 @@ class ViteRails::Manifest
29
29
  # Returns a relative path, or nil if the asset is not found.
30
30
  #
31
31
  # Example:
32
- # ViteRails.manifest.lookup('calendar.js') # => "/vite/assets/calendar-1016838bab065ae1e122.js"
32
+ # ViteRails.manifest.lookup('calendar.js')
33
+ # # { "file" => "/vite/assets/calendar-1016838bab065ae1e122.js", "imports" => [] }
33
34
  def lookup(name, type:)
34
35
  build if should_build?
35
36
 
@@ -43,12 +44,7 @@ class ViteRails::Manifest
43
44
 
44
45
  private
45
46
 
46
- delegate :config, :builder, :dev_server, to: :@vite_rails
47
-
48
- # Public: Returns true if the Vite development server is running.
49
- def dev_server_running?
50
- ViteRails.run_proxy? && dev_server.running?
51
- end
47
+ delegate :config, :builder, :dev_server_running?, to: :@vite_rails
52
48
 
53
49
  # NOTE: Auto compilation is convenient when running tests, when the developer
54
50
  # won't focus on the frontend, or when running the Vite server is not desired.
@@ -59,9 +55,9 @@ private
59
55
  # Internal: Finds the specified entry in the manifest.
60
56
  def find_manifest_entry(name)
61
57
  if dev_server_running?
62
- "/#{ config.public_output_dir.join(name.to_s) }"
63
- elsif file = manifest.dig(name.to_s, 'file')
64
- "/#{ config.public_output_dir.join(file) }"
58
+ { 'file' => "/#{ config.public_output_dir.join(name.to_s) }" }
59
+ else
60
+ manifest[name.to_s]
65
61
  end
66
62
  end
67
63
 
@@ -83,7 +79,10 @@ private
83
79
  # Internal: Returns a Hash with the entries in the manifest.json.
84
80
  def load_manifest
85
81
  if config.manifest_path.exist?
86
- JSON.parse(config.manifest_path.read)
82
+ JSON.parse(config.manifest_path.read).each do |_, entry|
83
+ entry['file'] = within_public_output_dir(entry['file'])
84
+ entry['imports'] = entry['imports']&.map { |path| within_public_output_dir(path) }
85
+ end
87
86
  else
88
87
  {}
89
88
  end
@@ -96,6 +95,11 @@ private
96
95
  "#{ name }.#{ extension_for_type(entry_type) }"
97
96
  end
98
97
 
98
+ # Internal: Scopes the paths in the manifest to the output folder in public.
99
+ def within_public_output_dir(path)
100
+ "/#{ config.public_output_dir.join(path) }"
101
+ end
102
+
99
103
  # Internal: Allows to receive :javascript and :stylesheet as :type in helpers.
100
104
  def extension_for_type(entry_type)
101
105
  case entry_type
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ViteRails
4
- VERSION = '1.0.4'
4
+ VERSION = '1.0.5'
5
5
  end
@@ -2,27 +2,11 @@
2
2
  "name": "only-for-workflows",
3
3
  "version": "unknown",
4
4
  "scripts": {
5
- "test": "jest",
6
- "lint": "eslint package/"
5
+ "lint": "npm -C package run lint",
6
+ "test": "npm -C package run test"
7
7
  },
8
8
  "dependencies": {
9
- "vite": "^2.0.0-beta.30",
10
- "vite-plugin-ruby": "1.0.0"
11
- },
12
- "devDependencies": {
13
- "@vitejs/plugin-vue": "^1.0.6",
14
- "eslint": "^7.16.0",
15
- "eslint-config-airbnb": "^18.2.0",
16
- "eslint-config-prettier": "^7.1.0",
17
- "eslint-plugin-import": "^2.22.1",
18
- "eslint-plugin-jsx-a11y": "^6.3.1",
19
- "eslint-plugin-react": "^7.21.4",
20
- "jest": "^26.5.3"
21
- },
22
- "jest": {
23
- "testRegex": "(/__tests__/.*|(\\.|/))\\.jsx?$",
24
- "roots": [
25
- "<rootDir>/package"
26
- ]
9
+ "vite": "^2.0.0-beta.34",
10
+ "vite-plugin-ruby": "^1.0.1"
27
11
  }
28
12
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vite_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-19 00:00:00.000000000 Z
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -186,8 +186,8 @@ homepage: https://github.com/ElMassimo/vite_rails
186
186
  licenses:
187
187
  - MIT
188
188
  metadata:
189
- source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.4
190
- changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.4/CHANGELOG.md
189
+ source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.5
190
+ changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.5/CHANGELOG.md
191
191
  post_install_message:
192
192
  rdoc_options: []
193
193
  require_paths: