From cc379ca613dd4e5863f8556be62ea623b56cfe0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Mussini?= Date: Tue, 16 Jul 2024 18:06:08 -0300 Subject: [PATCH] fix: use ESM by default in new installations (#479) closes #431 --- Gemfile.lock | 48 +++++++++++----------- docs/src/guide/troubleshooting.md | 7 ++++ examples/rails/example_engine/package.json | 3 +- test/engine_rake_tasks_test.rb | 4 +- test/mounted_app/test/dummy/package.json | 1 + vite_ruby/lib/vite_ruby/cli/install.rb | 15 ++++++- vite_ruby/lib/vite_ruby/config.rb | 1 + 7 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 78e83f21..71e8fbae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/docs/src/guide/troubleshooting.md b/docs/src/guide/troubleshooting.md index 0237f22d..b8f4db17 100644 --- a/docs/src/guide/troubleshooting.md +++ b/docs/src/guide/troubleshooting.md @@ -90,6 +90,13 @@ Use the `~/` alias to the [sourceCodeDir] 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 diff --git a/examples/rails/example_engine/package.json b/examples/rails/example_engine/package.json index d9628581..b1de2c08 100644 --- a/examples/rails/example_engine/package.json +++ b/examples/rails/example_engine/package.json @@ -2,5 +2,6 @@ "devDependencies": { "vite": "^4.3.0", "vite-plugin-ruby": "^3.2.0" - } + }, + "type": "module" } diff --git a/test/engine_rake_tasks_test.rb b/test/engine_rake_tasks_test.rb index 170176de..94ffd522 100644 --- a/test/engine_rake_tasks_test.rb +++ b/test/engine_rake_tasks_test.rb @@ -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') @@ -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) diff --git a/test/mounted_app/test/dummy/package.json b/test/mounted_app/test/dummy/package.json index 6307ba07..170d8442 100644 --- a/test/mounted_app/test/dummy/package.json +++ b/test/mounted_app/test/dummy/package.json @@ -1,6 +1,7 @@ { "private": true, "license": "MIT", + "type": "module", "devDependencies": { "ci": "^2.0.0", "ni": "^0.0.2", diff --git a/vite_ruby/lib/vite_ruby/cli/install.rb b/vite_ruby/lib/vite_ruby/cli/install.rb index 3f167fa4..62001786 100644 --- a/vite_ruby/lib/vite_ruby/cli/install.rb +++ b/vite_ruby/lib/vite_ruby/cli/install.rb @@ -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.' @@ -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 diff --git a/vite_ruby/lib/vite_ruby/config.rb b/vite_ruby/lib/vite_ruby/config.rb index 0932d6fd..bcc64c7c 100644 --- a/vite_ruby/lib/vite_ruby/config.rb +++ b/vite_ruby/lib/vite_ruby/config.rb @@ -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