Skip to content

Commit

Permalink
fix: use ESM by default in new installations (#479)
Browse files Browse the repository at this point in the history
closes #431
  • Loading branch information
ElMassimo authored Jul 16, 2024
1 parent 93ae26f commit cc379ca
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 29 deletions.
48 changes: 23 additions & 25 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,26 @@ GEM
ast (2.4.2)
base64 (0.2.0)
benchmark-ips (2.12.0)
bigdecimal (3.1.4)
builder (3.2.4)
bigdecimal (3.1.8)
builder (3.3.0)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.2.2)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.3)
docile (1.4.0)
drb (2.2.0)
ruby2_keywords
dry-cli (1.0.0)
erubi (1.12.0)
drb (2.2.1)
dry-cli (1.1.0)
erubi (1.13.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
irb (1.9.0)
rdoc
reline (>= 0.3.8)
io-console (0.7.2)
irb (1.14.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.6.3)
loofah (2.22.0)
crass (~> 1.0.2)
Expand All @@ -135,8 +134,8 @@ GEM
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.5)
mini_portile2 (2.8.5)
minitest (5.20.0)
mini_portile2 (2.8.7)
minitest (5.24.1)
minitest-reporters (1.6.1)
ansi
builder
Expand All @@ -154,7 +153,7 @@ GEM
net-smtp (0.4.0)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4)
nokogiri (1.16.6)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.23.0)
Expand All @@ -167,10 +166,10 @@ GEM
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
psych (5.1.1.1)
psych (5.1.2)
stringio
racc (1.7.3)
rack (3.0.8)
racc (1.8.0)
rack (3.1.7)
rack-proxy (0.7.7)
rack
rack-session (2.0.0)
Expand Down Expand Up @@ -210,11 +209,11 @@ GEM
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.0)
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.8.1)
reline (0.4.0)
reline (0.5.9)
io-console (~> 0.5)
rexml (3.2.6)
rubocop (1.24.1)
Expand All @@ -234,15 +233,14 @@ GEM
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
spring (2.1.1)
stringio (3.0.9)
thor (1.3.0)
stringio (3.1.1)
thor (1.3.1)
timeout (0.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -251,7 +249,7 @@ GEM
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.12)
zeitwerk (2.6.16)

PLATFORMS
ruby
Expand Down
7 changes: 7 additions & 0 deletions docs/src/guide/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ Use the `~/` alias to the <kbd>[sourceCodeDir]</kbd> to disambiguate the referen
<%= vite_stylesheet_tag '~/style.css' unless ViteRuby.instance.dev_server_running? %>
```

### The CJS build of Vite's Node API is deprecated

Please refer to [Vite's troubleshooting guide](https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated).

You might need to add `"type": "module"` to your `package.json`, or rename
`vite.config.ts` to `vite.config.mjs`.

## HMR Issues

### Hot Module Refresh does not work for React
Expand Down
3 changes: 2 additions & 1 deletion examples/rails/example_engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"devDependencies": {
"vite": "^4.3.0",
"vite-plugin-ruby": "^3.2.0"
}
},
"type": "module"
}
4 changes: 2 additions & 2 deletions test/engine_rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_rake_tasks
app_frontend_dir.join('ssr/ssr.js').write(SSR_ENTRYPOINT)

within_mounted_app { `bundle exec rake app:vite:build_all` }
assert_path_exists app_ssr_dir.join('ssr.mjs')
assert_path_exists app_ssr_dir.join('ssr.js')
refute_path_exists app_ssr_dir.join('.vite/manifest.json')
refute_path_exists app_ssr_dir.join('.vite/manifest-assets.json')

Expand Down Expand Up @@ -89,7 +89,7 @@ def test_cli_commands
}

FileUtils.mkdir_p(app_ssr_dir.to_s)
ssr_path = app_ssr_dir.join('ssr.mjs')
ssr_path = app_ssr_dir.join('ssr.js')
ssr_path.write('')
stub_kernel_exec('node', ssr_path.to_s) {
ViteRuby::CLI::SSR.new.call(mode: ViteRuby.mode)
Expand Down
1 change: 1 addition & 0 deletions test/mounted_app/test/dummy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"license": "MIT",
"type": "module",
"devDependencies": {
"ci": "^2.0.0",
"ni": "^0.0.2",
Expand Down
15 changes: 14 additions & 1 deletion vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'stringio'
require 'json'

class ViteRuby::CLI::Install < Dry::CLI::Command
desc 'Performs the initial configuration setup to get started with Vite Ruby.'
Expand Down Expand Up @@ -79,7 +80,19 @@ def create_configuration_files
# Internal: Installs vite and vite-plugin-ruby at the project level.
def install_js_dependencies
package_json = root.join('package.json')
write(package_json, '{}') unless package_json.exist?
unless package_json.exist?
write(package.json, <<~JSON)
{
"private": true,
"type": "module"
}
JSON
end

if (JSON.parse(package_json.read)['type'] != 'module' rescue nil)
FileUtils.mv root.join('vite.config.ts'), root.join('vite.config.mts'), force: true, verbose: true
end

deps = js_dependencies.join(' ')
run_with_capture("#{ npm_install } -D #{ deps }", stdin_data: "\n")
end
Expand Down
1 change: 1 addition & 0 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def config_from_file(path, mode:)
vite.config.js
vite.config.mjs
vite.config.ts
vite.config.mts
windi.config.ts
yarn.lock
].freeze
Expand Down

0 comments on commit cc379ca

Please sign in to comment.