vite_rails 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7036c3650b1b44c82158220474b56e26488785cefb0a99044444708d603001a
4
- data.tar.gz: 8161dbb668ff8c2308de6783e1dbff81faf60b13cd87393348070a2f8b2f9c18
3
+ metadata.gz: de1ef14e664eb5f06527b1126e5508598d50289c855edbf27d6e373f6ba805e1
4
+ data.tar.gz: 6754500ef38d0dcaba0e7d89de42da5b6f51fff79e215d9a4b02f116a332e13d
5
5
  SHA512:
6
- metadata.gz: a5ecb6327e2009b5fcf8aa91f90989264538db013dafc4b34a361e11b60a8d69bd394d6c6e5043583ff1cfe35fe5464424e26b616304ee30089c9e5a06fa2bda
7
- data.tar.gz: 04124e8f3c0063b1461185fbfb665623b0c49c6ea3712952a857eb1bb00886e420ae64335609f4879377013ef7f32506ef84400af52f9a3385c90b9b5c025193
6
+ metadata.gz: 3207af12e9a3304b12da3f549c4ae1fe2610bb9a5511f164040b8a7e82e2324b472ebbc90163142db6fa06a5752acac2f38fb0c6ecefed0690ae6f8c2981702f
7
+ data.tar.gz: c84109e20d0ba02b594de5e99922abeee23cc69146b955816c29aed23a1b65cc1db7e6e39eae67458fb8ceb74b38784e22fad5ac98f6c209f5697e46e0bca9ac
@@ -1,3 +1,8 @@
1
+ ## Vite Rails 1.0.10 (2020-01-23)
2
+
3
+ - Use `path_to_asset` in `vite_asset_path` so that it's prefixed automatically
4
+ when using a CDN (`config.action_controller.asset_host`).
5
+
1
6
  ## Vite Rails 1.0.9 (2020-01-22)
2
7
 
3
8
  - Ensure `configPath` and `publicDir` are scoped from `root`, both in Ruby and JS.
data/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  <h1 align="center">
2
2
  Vite ⚡️ Rails
3
3
  <p align="center">
4
- <!-- <a href="https://github.com/ElMassimo/vite_rails/actions">
4
+ <a href="https://github.com/ElMassimo/vite_rails/actions">
5
5
  <img alt="Build Status" src="https://github.com/ElMassimo/vite_rails/workflows/build/badge.svg"/>
6
- </a> -->
7
- <!-- <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
6
+ </a>
7
+ <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
8
8
  <img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/gpa.svg"/>
9
9
  </a>
10
10
  <a href="https://codeclimate.com/github/ElMassimo/vite_rails">
11
11
  <img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/vite_rails/badges/coverage.svg"/>
12
- </a> -->
12
+ </a>
13
13
  <a href="https://rubygems.org/gems/vite_rails">
14
14
  <img alt="Gem Version" src="https://img.shields.io/gem/v/vite_rails.svg?colorB=e9573f"/>
15
15
  </a>
@@ -68,7 +68,7 @@ private
68
68
  logger.info 'Building with Vite ⚡️'
69
69
 
70
70
  command = "#{ which_ruby } ./bin/vite build --mode #{ config.mode }"
71
- stdout, stderr, status = Open3.capture3(vite_env, command, chdir: File.expand_path(config.root))
71
+ stdout, stderr, status = Open3.capture3(ViteRails.config.to_env, command, chdir: File.expand_path(config.root))
72
72
 
73
73
  log_build_result(stdout, stderr, status)
74
74
 
@@ -108,13 +108,4 @@ private
108
108
  config.config_path,
109
109
  ].freeze
110
110
  end
111
-
112
- # Internal: Sets additional environment variables for vite-plugin-ruby.
113
- def vite_env
114
- ViteRails.env.merge(
115
- "#{ ViteRails::ENV_PREFIX }_CONFIG_PATH" => config.config_path,
116
- "#{ ViteRails::ENV_PREFIX }_MODE" => config.mode,
117
- "#{ ViteRails::ENV_PREFIX }_ROOT" => config.root,
118
- ).transform_values(&:to_s)
119
- end
120
111
  end
@@ -29,6 +29,7 @@ class ViteRails::Commands
29
29
  def clobber
30
30
  config.build_output_dir.rmtree if config.build_output_dir.exist?
31
31
  config.build_cache_dir.rmtree if config.build_cache_dir.exist?
32
+ config.vite_cache_dir.rmtree if config.vite_cache_dir.exist?
32
33
  end
33
34
 
34
35
  # Public: Receives arguments from a rake task.
@@ -9,11 +9,6 @@ class ViteRails::Config
9
9
 
10
10
  def initialize(attrs)
11
11
  @config = attrs.tap { |config| coerce_values(config) }.freeze
12
-
13
- # Define getters for the configuration options.
14
- CONFIGURABLE_WITH_ENV.each do |option|
15
- define_singleton_method(option) { @config[option] }
16
- end
17
12
  end
18
13
 
19
14
  def protocol
@@ -31,12 +26,26 @@ class ViteRails::Config
31
26
 
32
27
  # Public: The directory where Vite will store the built assets.
33
28
  def build_output_dir
34
- public_dir.join(public_output_dir)
29
+ root.join(public_dir, public_output_dir)
35
30
  end
36
31
 
37
32
  # Public: The directory where the entries are located.
38
33
  def resolved_entrypoints_dir
39
- source_code_dir.join(entrypoints_dir)
34
+ root.join(source_code_dir, entrypoints_dir)
35
+ end
36
+
37
+ # Internal: The directory where Vite stores its processing cache.
38
+ def vite_cache_dir
39
+ root.join('node_modules/.vite')
40
+ end
41
+
42
+ # Public: Sets additional environment variables for vite-plugin-ruby.
43
+ def to_env
44
+ CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env|
45
+ unless (value = @config[option]).nil?
46
+ env["#{ ViteRails::ENV_PREFIX }_#{ option.upcase }"] = value.to_s
47
+ end
48
+ end.merge(ViteRails.env)
40
49
  end
41
50
 
42
51
  private
@@ -46,13 +55,9 @@ private
46
55
  def coerce_values(config)
47
56
  config['mode'] = config['mode'].to_s
48
57
  config['port'] = config['port'].to_i
58
+ coerce_paths(config, 'root', 'public_output_dir')
59
+ config['build_cache_dir'] = config['root'].join(config['build_cache_dir'])
49
60
  coerce_booleans(config, 'auto_build', 'hide_build_console_output', 'https')
50
- coerce_paths(config, 'config_path', 'public_output_dir', 'root')
51
-
52
- # Prefix paths that are relative to the project root.
53
- config.slice('build_cache_dir', 'public_dir', 'source_code_dir').each do |option, dir|
54
- config[option] = config['root'].join(dir) if dir
55
- end
56
61
  end
57
62
 
58
63
  # Internal: Coerces configuration options to boolean.
@@ -68,38 +73,46 @@ private
68
73
  class << self
69
74
  # Public: Returns the project configuration for Vite.
70
75
  def resolve_config(attrs = {})
71
- attrs = attrs.transform_keys(&:to_s)
72
- mode = (attrs['mode'] ||= config_option_from_env('mode') || Rails.env.to_s).to_s
73
- root = Pathname.new(attrs['root'] ||= config_option_from_env('root') || Rails.root || Dir.pwd)
74
- config_path = (attrs['config_path'] ||= config_option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'))
75
- file_attrs = config_from_file(root: root, mode: mode, config_path: config_path)
76
- new DEFAULT_CONFIG.merge(file_attrs).merge(config_from_env).merge(attrs)
76
+ config = attrs.transform_keys(&:to_s).reverse_merge(config_defaults)
77
+ file_path = File.join(config['root'], config['config_path'])
78
+ file_config = config_from_file(file_path, mode: config['mode'])
79
+ new DEFAULT_CONFIG.merge(file_config).merge(config_from_env).merge(config)
77
80
  end
78
81
 
79
82
  private
80
83
 
84
+ # Internal: Default values for a Rails application.
85
+ def config_defaults
86
+ {
87
+ 'asset_host' => option_from_env('asset_host') || Rails.application&.config&.action_controller&.asset_host,
88
+ 'config_path' => option_from_env('config_path') || DEFAULT_CONFIG.fetch('config_path'),
89
+ 'mode' => option_from_env('mode') || Rails.env.to_s,
90
+ 'root' => option_from_env('root') || Rails.root || Dir.pwd,
91
+ }
92
+ end
93
+
81
94
  # Internal: Used to load a JSON file from the specified path.
82
95
  def load_json(path)
83
96
  JSON.parse(File.read(File.expand_path(path))).deep_transform_keys(&:underscore)
84
97
  end
85
98
 
86
99
  # Internal: Retrieves a configuration option from environment variables.
87
- def config_option_from_env(name)
100
+ def option_from_env(name)
88
101
  ViteRails.env["#{ ViteRails::ENV_PREFIX }_#{ name.upcase }"]
89
102
  end
90
103
 
91
104
  # Internal: Extracts the configuration options provided as env vars.
92
105
  def config_from_env
93
- CONFIGURABLE_WITH_ENV.each_with_object({}) do |key, env_vars|
94
- if value = config_option_from_env(key)
95
- env_vars[key] = value
106
+ CONFIGURABLE_WITH_ENV.each_with_object({}) do |option, env_vars|
107
+ if value = option_from_env(option)
108
+ env_vars[option] = value
96
109
  end
97
110
  end
98
111
  end
99
112
 
100
113
  # Internal: Loads the configuration options provided in a JSON file.
101
- def config_from_file(root:, mode:, config_path:)
102
- multi_env_config = load_json(root.join(config_path))
114
+ def config_from_file(path, mode:)
115
+ multi_env_config = load_json(path)
103
116
  multi_env_config.fetch('all', {})
104
117
  .merge(multi_env_config.fetch(mode, {}))
105
118
  rescue Errno::ENOENT => error
@@ -111,6 +124,16 @@ private
111
124
  # Internal: Shared configuration with the Vite plugin for Ruby.
112
125
  DEFAULT_CONFIG = load_json("#{ __dir__ }/../../package/default.vite.json").freeze
113
126
 
127
+ # Internal: Configuration options that can not be provided as env vars.
128
+ NOT_CONFIGURABLE_WITH_ENV = %w[watch_additional_paths].freeze
129
+
114
130
  # Internal: Configuration options that can be provided as env vars.
115
- CONFIGURABLE_WITH_ENV = (DEFAULT_CONFIG.keys + %w[mode root]).freeze
131
+ CONFIGURABLE_WITH_ENV = (DEFAULT_CONFIG.keys + %w[mode root] - NOT_CONFIGURABLE_WITH_ENV).freeze
132
+
133
+ public
134
+
135
+ # Define getters for the configuration options.
136
+ (CONFIGURABLE_WITH_ENV + NOT_CONFIGURABLE_WITH_ENV).each do |option|
137
+ define_method(option) { @config[option] }
138
+ end
116
139
  end
@@ -29,7 +29,6 @@ private
29
29
  def rewrite_uri_for_vite(env)
30
30
  uri = env.fetch('REQUEST_URI') { [env['PATH_INFO'], env['QUERY_STRING']].reject(&:blank?).join('?') }
31
31
  .sub(vite_asset_url_prefix, '/')
32
- .sub('.ts.js', '.ts') # Patch: Rails helpers always append the extension.
33
32
  env['PATH_INFO'], env['QUERY_STRING'] = (env['REQUEST_URI'] = uri).split('?')
34
33
  end
35
34
 
@@ -2,7 +2,7 @@
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
5
+ DEFAULT_VITE_SKIP_PRELOAD_TAGS = Rails.gem_version < Gem::Version.new('5.2.0')
6
6
 
7
7
  # Public: Returns the current Vite Rails instance.
8
8
  def current_vite_instance
@@ -14,12 +14,12 @@ module ViteRails::Helper
14
14
  content_tag('script', '', src: '/@vite/client', type: 'module') if ViteRails.dev_server_running?
15
15
  end
16
16
 
17
- # Public: Computes the relative path for the specified given Vite asset.
17
+ # Public: Resolves the path for the specified Vite asset.
18
18
  #
19
19
  # Example:
20
20
  # <%= vite_asset_path 'calendar.css' %> # => "/vite/assets/calendar-1016838bab065ae1e122.css"
21
21
  def vite_asset_path(name, **options)
22
- current_vite_instance.manifest.lookup!(name, **options).fetch('file')
22
+ path_to_asset current_vite_instance.manifest.lookup!(name, **options).fetch('file')
23
23
  end
24
24
 
25
25
  # Public: Renders a <script> tag for the specified Vite entrypoints.
@@ -53,7 +53,7 @@ module ViteRails::Helper
53
53
  # NOTE: Because TypeScript is not a valid target in browsers, we only specify
54
54
  # the ts file when running the Vite development server.
55
55
  def vite_typescript_tag(*names, **options)
56
- vite_javascript_tag(*names, asset_type: :typescript, **options)
56
+ vite_javascript_tag(*names, asset_type: :typescript, extname: false, **options)
57
57
  end
58
58
 
59
59
  # Public: Renders a <link> tag for the specified Vite entrypoints.
@@ -130,9 +130,10 @@ private
130
130
  [
131
131
  (dev_server_running? && 'Vite has not yet re-built your latest changes.'),
132
132
  (local && !dev_server_running? && "\"autoBuild\": false in your #{ config.mode } configuration."),
133
+ (local && !dev_server_running? && 'The Vite development server has crashed or is no longer available.'),
133
134
  'You have misconfigured config/vite.json file.',
134
135
  (!local && 'Assets have not been precompiled'),
135
- ].select(&:itself)
136
+ ].compact
136
137
  rescue StandardError
137
138
  []
138
139
  end
@@ -33,7 +33,7 @@ private
33
33
  cmd.prepend('node', '--trace-deprecation') if args.delete('--trace-deprecation')
34
34
  args.append('--mode', ViteRails.mode) unless args.include?('--mode') || args.include?('-m')
35
35
  cmd += args
36
- Dir.chdir(File.expand_path('.', Dir.pwd)) { Kernel.exec(ViteRails.env, *cmd) }
36
+ Dir.chdir(File.expand_path('.', Dir.pwd)) { Kernel.exec(ViteRails.config.to_env, *cmd) }
37
37
  end
38
38
 
39
39
  # Internal: Resolves to an executable for Vite.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ViteRails
4
- VERSION = '1.0.9'
4
+ VERSION = '1.0.10'
5
5
  end
@@ -11,6 +11,6 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "vite": "^2.0.0-beta.34",
14
- "vite-plugin-ruby": "^1.0.3"
14
+ "vite-plugin-ruby": "^1.0.4"
15
15
  }
16
16
  }
@@ -1,4 +1,5 @@
1
1
  {
2
+ "assetHost": null,
2
3
  "assetsDir": "assets",
3
4
  "autoBuild": false,
4
5
  "buildCacheDir": "tmp/cache/vite",
@@ -15,7 +15,7 @@ class BuilderTest < ViteRails::Test
15
15
  end
16
16
 
17
17
  def vite_env
18
- ViteRails.builder.send(:vite_env)
18
+ ViteRails.config.to_env
19
19
  end
20
20
 
21
21
  def test_custom_environment_variables
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class ConfigurationTest < ViteRails::Test
5
+ class ConfigTest < ViteRails::Test
6
6
  def expand_path(path)
7
7
  File.expand_path(Pathname.new(__dir__).join(path).to_s)
8
8
  end
@@ -24,7 +24,7 @@ class ConfigurationTest < ViteRails::Test
24
24
  end
25
25
 
26
26
  def test_source_code_dir
27
- assert_path 'test_app/app/frontend', @config.source_code_dir
27
+ assert_equal 'app/frontend', @config.source_code_dir
28
28
  end
29
29
 
30
30
  def test_entrypoints_dir
@@ -32,7 +32,7 @@ class ConfigurationTest < ViteRails::Test
32
32
  end
33
33
 
34
34
  def test_public_dir
35
- assert_path 'test_app/public', @config.public_dir
35
+ assert_equal 'public', @config.public_dir
36
36
  end
37
37
 
38
38
  def test_build_output_dir
@@ -85,6 +85,17 @@ class ConfigurationTest < ViteRails::Test
85
85
  end
86
86
  end
87
87
 
88
+ def test_to_env
89
+ env = @config.to_env
90
+ assert_nil env['VITE_RUBY_ASSET_HOST']
91
+
92
+ Rails.application.config.action_controller.asset_host = 'assets-cdn.com'
93
+ env = refresh_config.to_env
94
+ assert_equal env['VITE_RUBY_ASSET_HOST'], 'assets-cdn.com'
95
+ ensure
96
+ Rails.application.config.action_controller.asset_host = nil
97
+ end
98
+
88
99
  def test_environment_vars
89
100
  ViteRails.env = {
90
101
  'VITE_RUBY_AUTO_BUILD' => 'true',
@@ -106,13 +117,13 @@ class ConfigurationTest < ViteRails::Test
106
117
  assert_equal 1920, @config.port
107
118
  assert_equal true, @config.https
108
119
  assert_equal 'https', @config.protocol
109
- assert_equal Pathname.new('config/vite_additional_paths.json'), @config.config_path
120
+ assert_equal 'config/vite_additional_paths.json', @config.config_path
110
121
  assert_pathname 'tmp/vitebuild', @config.build_cache_dir
111
- assert_pathname 'pb', @config.public_dir
122
+ assert_equal 'pb', @config.public_dir
112
123
  assert_equal Pathname.new('ft'), @config.public_output_dir
113
124
  assert_pathname 'pb/ft', @config.build_output_dir
114
125
  assert_equal 'as', @config.assets_dir
115
- assert_pathname 'app', @config.source_code_dir
126
+ assert_equal 'app', @config.source_code_dir
116
127
  assert_equal 'frontend/entrypoints', @config.entrypoints_dir
117
128
  assert_pathname 'app/frontend/entrypoints', @config.resolved_entrypoints_dir
118
129
  assert_equal true, @config.hide_build_console_output
@@ -63,7 +63,7 @@ class HelperTest < ActionView::TestCase
63
63
  assert_equal %(<script src="/vite-production/application.js" crossorigin="anonymous" type="module"></script>),
64
64
  vite_javascript_tag('application')
65
65
 
66
- assert_equal %(<script src="/vite-production/application.ts.js" crossorigin="anonymous" type="module"></script>),
66
+ assert_equal %(<script src="/vite-production/application.ts" crossorigin="anonymous" type="module"></script>),
67
67
  vite_typescript_tag('application')
68
68
  }
69
69
  end
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.9
4
+ version: 1.0.10
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-22 00:00:00.000000000 Z
11
+ date: 2021-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -148,7 +148,7 @@ files:
148
148
  - package/default.vite.json
149
149
  - test/builder_test.rb
150
150
  - test/commands_test.rb
151
- - test/configuration_test.rb
151
+ - test/config_test.rb
152
152
  - test/dev_server_proxy_test.rb
153
153
  - test/dev_server_test.rb
154
154
  - test/engine_rake_tasks_test.rb
@@ -185,8 +185,8 @@ homepage: https://github.com/ElMassimo/vite_rails
185
185
  licenses:
186
186
  - MIT
187
187
  metadata:
188
- source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.9
189
- changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.9/CHANGELOG.md
188
+ source_code_uri: https://github.com/ElMassimo/vite_rails/tree/v1.0.10
189
+ changelog_uri: https://github.com/ElMassimo/vite_rails/blob/v1.0.10/CHANGELOG.md
190
190
  post_install_message:
191
191
  rdoc_options: []
192
192
  require_paths:
@@ -209,7 +209,7 @@ summary: Use Vite in Rails and bring joy to your JavaScript experience
209
209
  test_files:
210
210
  - test/builder_test.rb
211
211
  - test/commands_test.rb
212
- - test/configuration_test.rb
212
+ - test/config_test.rb
213
213
  - test/dev_server_proxy_test.rb
214
214
  - test/dev_server_test.rb
215
215
  - test/engine_rake_tasks_test.rb