Skip to content
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

export types in generated file #329

Closed
wants to merge 3 commits into from
Closed

export types in generated file #329

wants to merge 3 commits into from

Conversation

kryali
Copy link

@kryali kryali commented Dec 21, 2024

No description provided.

@bogdan
Copy link
Collaborator

bogdan commented Dec 22, 2024

What is a purpose of the change?

@kryali kryali closed this Dec 23, 2024
@kryali
Copy link
Author

kryali commented Dec 23, 2024

@bogdan apologies i had not intended to contribute this change back to mainline.

I made this change so that I could import the generated types & methods into my client-side js. How do others use the generated code? I fear I may have missed something important or perhaps an editor config (vscode).

@bogdan
Copy link
Collaborator

bogdan commented Dec 23, 2024

Normally people just use the exported methods. Types are not exported because most of them are not a part of public API. Believe only a few do, but people never asked to use them. Give me an example of what kind of code you use on frontend if you want a concrete advice.

@kryali
Copy link
Author

kryali commented Dec 23, 2024

Here is an example of what I use on the front end, pretty simple. Where are the methods exported? Maybe i am importing them incorrectly

import { market_quote_api_path, show_symbol_path } from '../routes';

@bogdan
Copy link
Collaborator

bogdan commented Dec 23, 2024

What is the content of you routes.js and routes.d.ts files?
(you may only include the part that defines market_quote_api_path and show_symbol_path)

@kryali
Copy link
Author

kryali commented Dec 23, 2024

here are the generated snippets

routes.js

/**
 * File generated by js-routes 2.3.0 on 2024-12-19 07:38:57 -0500
 * Based on Rails 7.1.3.4 routes of ::Application
 */
 
  /**
   * Generates rails route to
   * /market/ticker/:symbol(.:format)
   * @param {any} symbol
   * @param {object | undefined} options
   * @returns {string} route path
   */
  show_symbol_path: __jsr.r({"symbol":{"r":true},"format":{}}, [2,[7,"/"],[2,[6,"market"],[2,[7,"/"],[2,[6,"ticker"],[2,[7,"/"],[2,[3,"symbol"],[1,[2,[8,"."],[3,"format"]]]]]]]]]),
  
  
  /**
   * Generates rails route to
   * /api/v1/market/quotes(.:format)
   * @param {object | undefined} options
   * @returns {string} route path
   */
  market_quote_api_path: __jsr.r({"format":{}}, [2,[7,"/"],[2,[6,"api"],[2,[7,"/"],[2,[6,"v1"],[2,[7,"/"],[2,[6,"market"],[2,[7,"/"],[2,[6,"quotes"],[1,[2,[8,"."],[3,"format"]]]]]]]]]]]),

routes.d.ts

/**
 * Generates rails route to
 * /market/ticker/:symbol(.:format)
 * @param {any} symbol
 * @param {object | undefined} options
 * @returns {string} route path
 */
declare const show_symbol_path: ((
  symbol: RequiredRouteParameter,
  options?: {format?: OptionalRouteParameter} & RouteOptions
) => string) & RouteHelperExtras;


/**
 * Generates rails route to
 * /api/v1/market/quotes(.:format)
 * @param {object | undefined} options
 * @returns {string} route path
 */
declare const market_quote_api_path: ((
  options?: {format?: OptionalRouteParameter} & RouteOptions
) => string) & RouteHelperExtras;

@bogdan
Copy link
Collaborator

bogdan commented Dec 23, 2024

This is the code with your patch right? Well, I can't debug that. Please use original source code.
Also what kind of module system do you use? CJS, UMD, ESM or none.

@kryali
Copy link
Author

kryali commented Dec 23, 2024

@bogdan i ran my code against the latest version (2.3.2) and my front-end code works fine now with no changes. not sure why this was needed before but now it is not. thanks for helping! greatly appreciate it

I think I am using CJS module system

@kryali
Copy link
Author

kryali commented Dec 23, 2024

works locally but getting some issues building, this may be what triggered my change

#14 32.34 $ esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --tree-shaking=true --public-path=/assets --minify
#14 32.39 ✘ [ERROR] The constant "configure" must be initialized
#14 32.39 
#14 32.39     app/javascript/routes.d.ts:83:13:
#14 32.39       83 │ export const configure: RouterExposedMethods['configure'];
#14 32.39          ╵              ~~~~~~~~~
#14 32.39 
#14 32.39 ✘ [ERROR] The constant "config" must be initialized
#14 32.39 
#14 32.39     app/javascript/routes.d.ts:85:13:
#14 32.39       85 │ export const config: RouterExposedMethods['config'];
#14 32.39          ╵              ~~~~~~
#14 32.39 
#14 32.40 ✘ [ERROR] The constant "serialize" must be initialized
#14 32.40 
#14 32.40     app/javascript/routes.d.ts:87:13:
#14 32.40       87 │ export const serialize: RouterExposedMethods['serialize'];
#14 32.40          ╵              ~~~~~~~~~
#14 32.40 
#14 32.40 ✘ [ERROR] The constant "gainloss_path" must be initialized
#14 32.40 
#14 32.40     app/javascript/routes.d.ts:95:13:
#14 32.40       95 │ export const gainloss_path: ((
#14 32.40          ╵              ~~~~~~~~~~~~~
#14 32.40 
#14 32.40 ✘ [ERROR] The constant "groups_path" must be initialized
#14 32.40 
#14 32.40     app/javascript/routes.d.ts:105:13:
#14 32.40       105 │ export const groups_path: ((
#14 32.40           ╵              ~~~~~~~~~~~
#14 32.40 
#14 32.40 ✘ [ERROR] The constant "market_earnings_path" must be initialized
#14 32.40 
#14 32.40     app/javascript/routes.d.ts:115:13:
#14 32.40       115 │ export const market_earnings_path: ((
#14 32.40           ╵              ~~~~~~~~~~~~~~~~~~~~
#14 32.40 
#14 34.19 6 of 36 errors shown (disable the message limit with --log-limit=0)
#14 34.22 node:child_process:929
#14 34.22     throw err;
#14 34.22     ^
#14 34.22 
#14 34.22 Error: Command failed: /madrich/node_modules/@esbuild/linux-x64/bin/esbuild app/javascript/application.js app/javascript/blaze.js app/javascript/htmx.js app/javascript/navigator.js app/javascript/option_chain.js app/javascript/routes.d.ts app/javascript/routes.js app/javascript/utils.js --bundle --sourcemap --outdir=app/assets/builds --tree-shaking=true --public-path=/assets --minify

@bogdan
Copy link
Collaborator

bogdan commented Dec 23, 2024

It looks like the problem is that routes.d.ts is trying to be compiled, but it should be completely ignored by esbuild. This file is only useful in development.

@kryali
Copy link
Author

kryali commented Dec 23, 2024

here is generated code from latest 2.3.2

/**
 * @file Generated by js-routes 2.3.4. Based on Rails 7.1.3.4 routes of Madrich::Application.
 * @version 913e6a9ceaedc4775667854913d883a71e209188a0cc7638113c3dfb757f7623
 * @see https://github.com/railsware/js-routes
 */
export const configure: RouterExposedMethods['configure'];

export const config: RouterExposedMethods['config'];

export const serialize: RouterExposedMethods['serialize'];
/**
 * Generates rails route to
 * /api/v1/market/quotes(.:format)
 * @param {object | undefined} options
 * @returns {string} route path
 */
export const market_quote_api_path: ((
  options?: {format?: OptionalRouteParameter} & RouteOptions
) => string) & RouteHelperExtras;

@kryali
Copy link
Author

kryali commented Dec 23, 2024

@bogdan ah I see. I will ignore it from esbuild then.

@bogdan
Copy link
Collaborator

bogdan commented Dec 23, 2024

If you are using any gem providing esbuild wrapping, you should probably report a bug there because ignoring d.ts files by default is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants