-
Notifications
You must be signed in to change notification settings - Fork 759
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
Support for Vite #1134
Comments
I'd be interested in seeing the popularity of vite or other packaging tools as I've not used it before. So this is a +1 if it's popular or upcoming and we have someone who is qualified to maintain this gem that would use vite regularly. |
In development mode Vite seem to use I tried this plugin ( const componentPath = (module) => module.replace('./components/', '').replace('.jsx', '')
const glob = require('glob').sync
// thanks: https://github.com/thomaschaaf/esbuild-plugin-import-glob
// thanks: https://github.com/rails/jsbundling-rails/compare/main...xiaohui-zhangxh:main
const ImportGlobPlugin = () => ({
name: 'require-context',
setup: (build) => {
build.onResolve({ filter: /\*/ }, async (args) => {
if (args.resolveDir === '') {
return; // Ignore unresolvable paths
}
return {
path: args.path,
namespace: 'import-glob',
pluginData: {
resolveDir: args.resolveDir,
},
};
});
build.onLoad({ filter: /.*/, namespace: 'import-glob' }, async (args) => {
const files = (
glob(args.path, {
cwd: args.pluginData.resolveDir,
})
).sort();
let importerCode = `
${files
.map((module, index) => {
return `import * as module${index} from '${module}'`})
.join(';')}
export default [${files
.map((module, index) => `module${index}.default`)
.join(',')}];
export const context = {
${files.map((module, index) => `'${componentPath(module)}': module${index}.default`).join(',')}
}
`;
return { contents: importerCode, resolveDir: args.pluginData.resolveDir };
});
},
}); Then in entrypoint this makes my components globally visible to react-rails import { context } from './components/**/*.jsx';
Object.keys(context).forEach((key) => {
window[key] = context[key]
}) |
Couldn't fix this using @pustomytnyk solution as it errors out because
|
@pacMakaveli where that should be located? entrypoint? Thanks |
I've put it in my main |
Did anyone manage to implement vite + SSR successfully? |
@Alxzu Yes, although not in the context of |
Can i help in fixing this issue? |
Hi! I found a possible solution (for Vite Ruby) on this line number https://github.com/reactjs/react-rails/blob/master/react_ujs/index.js#L79 I saw that and understood how it was made the constructor so, I take the code and it change a little bit and I do the following approach:
I expect that this approach it works for you. |
@memoxmrdl That's awesome! I got the non-SSR pages working. Is it going to work with SSR? I tried but nothing was displayed with no errors. |
Actually I found the exception:
|
Thanks for posting this memoxmrdl, it was super helpful. Our components tend to be named Note that this won't work on windows as it assumes // Based on https://github.com/reactjs/react-rails/issues/1134#issuecomment-1415112288
export const viteConstructorRequireContext = function(reqCtx) {
const componentNameMatcher = className => {
return path => {
return (
path.includes(`/${className}.js`) || path.includes(`/${className}/index.js`)
);
};
};
const fromRequireContext = function(reqCtx) {
return function(className) {
const componentPath = Object.keys(reqCtx).find(componentNameMatcher(className));
const component = reqCtx[componentPath];
return component.default;
}
}
const fromCtx = fromRequireContext(reqCtx);
return function(className) {
var component;
try {
// `require` will raise an error if this className isn't found:
component = fromCtx(className);
} catch (firstErr) {
console.error(firstErr);
}
return component;
}
} |
FWIW, I'm considering supporting Vite with https://github.com/shakacode/react_on_rails, including SSR. If anybody is interested in that, reply here, and consider our Slack Channel. I suspect that Vite might work very easily with https://www.shakacode.com/react-on-rails-pro/, which is an easy migration from react-rails. |
This is a feature suggestion, and I imagine it would be a ton of work, so I mainly just wanted to start a thread for discussion.
React-Rails already supports both Sprockets and Webpack. Adding support for Vite would be great. There's a relatively new vite_ruby gem, and the author there commented on the feasibility of using it with react-rails. In my case, the app I wanted to try it with uses SSR, so I didn't get very far in testing it out.
Any thoughts?
The text was updated successfully, but these errors were encountered: