-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"The CJS build of Vite's Node API is deprecated." #431
Comments
Enabling "type": "module" in |
Sorry if this is a silly question, my JS environment managing knowledge is pretty basic... @jon-codes are you saying that's a change to vite-ruby that is required (vite-ruby's own package.json?), or this is something I just put in my own package.json? Can you provide an example of how to |
Sure @jrochkind, you can enable it in your own The Here's an example from my // package.json
{
"type": "module",
"scripts": {
"format": "prettier --check .",
"format:fix": "prettier --write .",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
"dependencies": {
"@hotwired/turbo-rails": "^8.0.2"
},
"devDependencies": {
"@fullhuman/postcss-purgecss": "^5.0.0",
"@types/node": "^20.11.19",
"autoprefixer": "^10.4.17",
"eslint": "^8.56.0",
"eslint-plugin-simple-import-sort": "^12.0.0",
"postcss": "^8.4.35",
"postcss-nesting": "^12.0.2",
"prettier": "^3.2.5",
"vite": "^5.1.3",
"vite-plugin-rails": "^0.5.0"
},
"engines": {
"node": "20.11.1",
"npm": ">=10"
},
"os": [
"linux",
"darwin"
]
} I'm using a // vite.config.js
/* esbuild-env node */
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import RailsPlugin from "vite-plugin-rails";
// ESM modules do not have `__filename` and `__dirname` constants, unlike CJS modules.
// See: https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export default defineConfig({
plugins: [RailsPlugin()],
resolve: {
alias: {
"@vendor": resolve(__dirname, "vendor"),
"@fonts": resolve(__dirname, "assets/fonts"),
"@icons": resolve(__dirname, "assets/icons"),
"@images": resolve(__dirname, "assets/images"),
"@stylesheets": resolve(__dirname, "assets/stylesheets"),
},
},
}); |
Thank you for that explanation! I will have to go through it more slowly and try things out.
vite-ruby generator right now creates
Do you think this needs/should be changed? I think the file does not actually use anything that's non-JS typescript or anything like that? Not sure why it has had It looks like possibly the generated file needs to be different too, per your comments on |
To resolve this issue on my system I replaced my Here is my import { defineConfig } from 'vite';
import RubyPlugin from 'vite-plugin-ruby';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
export default defineConfig({
plugins: [
RubyPlugin(),
],
css: {
postcss: {
plugins: [
tailwindcss,
autoprefixer,
],
},
},
assetsInclude: ['**/*.png', '**/*.jpg', '**/*.jpeg', '**/*.gif', '**/*.svg'],
}); EDIT: It turns out the first reply is all I needed. Adding |
Thanks @loftwah It looks to me like simply renaming my existing I didn't need to make any changes to Just name change of vite.config from @ElMassimo Does this make sense to you? Would it make sense to change the vite-ruby generator to generate filename We could prepare a PR, which changes the file, references to it, examples, etc. eg
|
Ah or I noticed #431 too @ElMassimo some maintainer advice would be appreciated, would love to get a PR in to avoid this confusion for newcomers! |
…t module) avoids vite deprecation message. Vite deprecation message looks like: ``` remote: Build with Vite complete: /tmp/build_93418466/public/vite remote: The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. ``` And started in vite 5.x. Relation to .ts typescript and simple solution suggested from ElMassimo/vite_ruby#431 Seems to work great.
Ultimately I believe the solution, as others have mentioned, is to set This is the {
"devDependencies": {
"vite": "^5.3.4",
"vite-plugin-ruby": "^5.0.0"
}
} Perhaps one step in the right direction would be for the installer to initialize the I.e. change this line:
to this: unless package_json.exist?
write(package.json, <<~JSON)
{
"private": true,
"type": "module"
}
JSON
end |
I think it's important the out of the box generators for vite-ruby/rails genreate something that does not produce deprecation warnings! I'm not a JS expert... just changing the suffix of the vite.config to In fact, not understanding the implications even for myself is part of why I want someone who understands this better to have the vite-ruby generators do the right thing, so I can just do what they do! |
@jrochkind the way I see it, ESM (meaning ES modules, which is what
However, I see your point that for an existing project, changing the |
@jrochkind sorry, to clarify: I am adding some ideas to this thread for possible fixes, just to further the conversation, and for maintainers to see when reviewing this issue. I do not have decision-making authority on this project. 🙂 |
@jrochkind @mattbrictson I was about to reply with a similar observation about ESM being the preferred/default module type for Vite projects in Node. Other observations:
I think the major downside, as mentioned, is that the change is riskier & requires updating more files than just updating the extension to |
Thank you that's all very helpful! I will add I have no opinion of my own of what is best for vite-ruby generator, beyond thinking that vite-ruby generator ought to genreate something that does not cause deprecation warnings! But unless maintainer shows up to welcome a PR... ? |
I'll clean up my branch using the |
Agreed, happy to take a PR with that change, as it's safe for new projects. Given the deprecation of the CJS API, it would be viable to detect an existing Finally, we should add |
Thanks for the discussion folks, and @mattbrictson for suggesting ESM by default for new installations. This is now released in Users facing this issue can refer to the Troubleshooting guide. |
@ElMassimo I'm getting an error running
It should be write(package_json, <<~JSON)
Edit: |
Good catch, it looks like there's no coverage for this path. I'll cut a new release. |
Using vite-rails
3.0.17
, vite_ruby3.5.0
, and vite5.0.10
, I'm getting this deprecation warning output in my console when runningassets:precompile
.I upgraded to vite 5.x after upgrading to vite_ruby 3.5.0, as it seemed to prefer vite 5? But I wonder if it's triggering this deprecation warning in vite 5, and if that can/should be fixed?
The text was updated successfully, but these errors were encountered: