Skip to content

Commit cc379ca

Browse files
authored
fix: use ESM by default in new installations (#479)
closes #431
1 parent 93ae26f commit cc379ca

File tree

7 files changed

+50
-29
lines changed

7 files changed

+50
-29
lines changed

Gemfile.lock

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,26 @@ GEM
9999
ast (2.4.2)
100100
base64 (0.2.0)
101101
benchmark-ips (2.12.0)
102-
bigdecimal (3.1.4)
103-
builder (3.2.4)
102+
bigdecimal (3.1.8)
103+
builder (3.3.0)
104104
byebug (11.1.3)
105105
coderay (1.1.3)
106-
concurrent-ruby (1.2.2)
106+
concurrent-ruby (1.3.3)
107107
connection_pool (2.4.1)
108108
crass (1.0.6)
109109
date (3.3.3)
110110
docile (1.4.0)
111-
drb (2.2.0)
112-
ruby2_keywords
113-
dry-cli (1.0.0)
114-
erubi (1.12.0)
111+
drb (2.2.1)
112+
dry-cli (1.1.0)
113+
erubi (1.13.0)
115114
globalid (1.2.1)
116115
activesupport (>= 6.1)
117-
i18n (1.14.1)
116+
i18n (1.14.5)
118117
concurrent-ruby (~> 1.0)
119-
io-console (0.6.0)
120-
irb (1.9.0)
121-
rdoc
122-
reline (>= 0.3.8)
118+
io-console (0.7.2)
119+
irb (1.14.0)
120+
rdoc (>= 4.0.0)
121+
reline (>= 0.4.2)
123122
json (2.6.3)
124123
loofah (2.22.0)
125124
crass (~> 1.0.2)
@@ -135,8 +134,8 @@ GEM
135134
marcel (1.0.2)
136135
method_source (1.0.0)
137136
mini_mime (1.1.5)
138-
mini_portile2 (2.8.5)
139-
minitest (5.20.0)
137+
mini_portile2 (2.8.7)
138+
minitest (5.24.1)
140139
minitest-reporters (1.6.1)
141140
ansi
142141
builder
@@ -154,7 +153,7 @@ GEM
154153
net-smtp (0.4.0)
155154
net-protocol
156155
nio4r (2.5.9)
157-
nokogiri (1.15.4)
156+
nokogiri (1.16.6)
158157
mini_portile2 (~> 2.8.2)
159158
racc (~> 1.4)
160159
parallel (1.23.0)
@@ -167,10 +166,10 @@ GEM
167166
pry-byebug (3.10.1)
168167
byebug (~> 11.0)
169168
pry (>= 0.13, < 0.15)
170-
psych (5.1.1.1)
169+
psych (5.1.2)
171170
stringio
172-
racc (1.7.3)
173-
rack (3.0.8)
171+
racc (1.8.0)
172+
rack (3.1.7)
174173
rack-proxy (0.7.7)
175174
rack
176175
rack-session (2.0.0)
@@ -210,11 +209,11 @@ GEM
210209
thor (~> 1.0, >= 1.2.2)
211210
zeitwerk (~> 2.6)
212211
rainbow (3.1.1)
213-
rake (13.1.0)
214-
rdoc (6.6.0)
212+
rake (13.2.1)
213+
rdoc (6.7.0)
215214
psych (>= 4.0.0)
216215
regexp_parser (2.8.1)
217-
reline (0.4.0)
216+
reline (0.5.9)
218217
io-console (~> 0.5)
219218
rexml (3.2.6)
220219
rubocop (1.24.1)
@@ -234,15 +233,14 @@ GEM
234233
rubocop (>= 1.7.0, < 2.0)
235234
rubocop-ast (>= 0.4.0)
236235
ruby-progressbar (1.13.0)
237-
ruby2_keywords (0.0.5)
238236
simplecov (0.17.1)
239237
docile (~> 1.1)
240238
json (>= 1.8, < 3)
241239
simplecov-html (~> 0.10.0)
242240
simplecov-html (0.10.2)
243241
spring (2.1.1)
244-
stringio (3.0.9)
245-
thor (1.3.0)
242+
stringio (3.1.1)
243+
thor (1.3.1)
246244
timeout (0.4.0)
247245
tzinfo (2.0.6)
248246
concurrent-ruby (~> 1.0)
@@ -251,7 +249,7 @@ GEM
251249
websocket-driver (0.7.6)
252250
websocket-extensions (>= 0.1.0)
253251
websocket-extensions (0.1.5)
254-
zeitwerk (2.6.12)
252+
zeitwerk (2.6.16)
255253

256254
PLATFORMS
257255
ruby

docs/src/guide/troubleshooting.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ Use the `~/` alias to the <kbd>[sourceCodeDir]</kbd> to disambiguate the referen
9090
<%= vite_stylesheet_tag '~/style.css' unless ViteRuby.instance.dev_server_running? %>
9191
```
9292

93+
### The CJS build of Vite's Node API is deprecated
94+
95+
Please refer to [Vite's troubleshooting guide](https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated).
96+
97+
You might need to add `"type": "module"` to your `package.json`, or rename
98+
`vite.config.ts` to `vite.config.mjs`.
99+
93100
## HMR Issues
94101

95102
### Hot Module Refresh does not work for React

examples/rails/example_engine/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"devDependencies": {
33
"vite": "^4.3.0",
44
"vite-plugin-ruby": "^3.2.0"
5-
}
5+
},
6+
"type": "module"
67
}

test/engine_rake_tasks_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_rake_tasks
3838
app_frontend_dir.join('ssr/ssr.js').write(SSR_ENTRYPOINT)
3939

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

@@ -89,7 +89,7 @@ def test_cli_commands
8989
}
9090

9191
FileUtils.mkdir_p(app_ssr_dir.to_s)
92-
ssr_path = app_ssr_dir.join('ssr.mjs')
92+
ssr_path = app_ssr_dir.join('ssr.js')
9393
ssr_path.write('')
9494
stub_kernel_exec('node', ssr_path.to_s) {
9595
ViteRuby::CLI::SSR.new.call(mode: ViteRuby.mode)

test/mounted_app/test/dummy/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"private": true,
33
"license": "MIT",
4+
"type": "module",
45
"devDependencies": {
56
"ci": "^2.0.0",
67
"ni": "^0.0.2",

vite_ruby/lib/vite_ruby/cli/install.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'stringio'
4+
require 'json'
45

56
class ViteRuby::CLI::Install < Dry::CLI::Command
67
desc 'Performs the initial configuration setup to get started with Vite Ruby.'
@@ -79,7 +80,19 @@ def create_configuration_files
7980
# Internal: Installs vite and vite-plugin-ruby at the project level.
8081
def install_js_dependencies
8182
package_json = root.join('package.json')
82-
write(package_json, '{}') unless package_json.exist?
83+
unless package_json.exist?
84+
write(package.json, <<~JSON)
85+
{
86+
"private": true,
87+
"type": "module"
88+
}
89+
JSON
90+
end
91+
92+
if (JSON.parse(package_json.read)['type'] != 'module' rescue nil)
93+
FileUtils.mv root.join('vite.config.ts'), root.join('vite.config.mts'), force: true, verbose: true
94+
end
95+
8396
deps = js_dependencies.join(' ')
8497
run_with_capture("#{ npm_install } -D #{ deps }", stdin_data: "\n")
8598
end

vite_ruby/lib/vite_ruby/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def config_from_file(path, mode:)
197197
vite.config.js
198198
vite.config.mjs
199199
vite.config.ts
200+
vite.config.mts
200201
windi.config.ts
201202
yarn.lock
202203
].freeze

0 commit comments

Comments
 (0)