Giter Club home page Giter Club logo

Comments (11)

stevenpetryk avatar stevenpetryk commented on July 30, 2024 1

Yep! But it’s a devDependency—users don’t download it just by using this library.

from contentful-typescript-codegen.

bsgreenb avatar bsgreenb commented on July 30, 2024 1

@stevenpetryk Was about to correct myself. Also I was posting to the wrong repo anyways! I realized this file is a relic of my attempting to use this before. With Gatsby I've used graphql-codegen, because (I think) this doesn't work with Gatsby+Contentful setup

from contentful-typescript-codegen.

stevenpetryk avatar stevenpetryk commented on July 30, 2024 1

Hi @G-Rath, I don't work at Intercom anymore and so I cannot make changes to this project. I would recommend tagging a recent committer.

from contentful-typescript-codegen.

stevenpetryk avatar stevenpetryk commented on July 30, 2024

I didn’t want ts-node to be a dependency of the project when it’s not necessary. You’re welcome to make it TypeScript if you prefer, and compile it to JS.

from contentful-typescript-codegen.

bsgreenb avatar bsgreenb commented on July 30, 2024

@stevenpetryk ts-node already is a dependency https://github.com/intercom/contentful-typescript-codegen/blob/master/package.json#L68

from contentful-typescript-codegen.

G-Rath avatar G-Rath commented on July 30, 2024

@stevenpetryk would you be open to supporting using TypeScript with ts-node as an optional peer dependency?

I've implemented this in a few projects like jest & postcss, and would be happy to do it again here if you're happy with that :)

from contentful-typescript-codegen.

GabrielAnca avatar GabrielAnca commented on July 30, 2024

Hi @G-Rath 👋🏻 I'd be interested in seeing how that would work! I'd be open to that as long as we keep backwards compatibility 😃 Would you mind going in a bit more detail on how your proposal would work? Or maybe if it's easier you can create a PR with your proposal and tag me there!

from contentful-typescript-codegen.

G-Rath avatar G-Rath commented on July 30, 2024

@GabrielAnca no problem - I'll look at doing a PR over the weekend.

The general way it's implemented is by doing a require for ts-node and if that's successful enabling it as a register before requiring the config file, then disabling it afterwards.

The implementation generally ends up looking something like this:

let registerer;

export const loadTSConfig = (configPath: string) => {
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    registerer ||= require('ts-node').register() as Service;
  } catch (e: any) {
    if (e.code === 'MODULE_NOT_FOUND') {
      throw new Error(
        `'ts-node' is required for TypeScript configuration files. Make sure it is installed\nError: ${e.message}`
      );
    }

    throw e;
  }

  registerer.enabled(true);

  // eslint-disable-next-line @typescript-eslint/no-var-requires
  const configObject = interopRequireDefault(require(configPath)).default;

  registerer.enabled(false);

  return configObject;
};

There's some variation depending on how the config loading is actually done which I've not looked at yet e.g. for postcss that was using a config loading library (cosmic-config, iirc) so it was implemented like above but as a loader (which the config library handled determining when to actually try and use that loader), whereas for jest we check if the config file we've found ends with .ts (and consider jest.config.ts as a valid config when searching on-disk) and call the config before disabling the registerer if it's a function so that dynamic imports still work.

(tbh it's getting to the point where I've been considering publishing a package for handling it, because of how similar the core code is 🙈)

from contentful-typescript-codegen.

G-Rath avatar G-Rath commented on July 30, 2024

Looking over the codebase, I think it should be a matter of wrapping these lines in some logic mixed with the above.

What I can't see is an interface that defines what the config should look like as this is type any - @GabrielAnca is that something you could help with creating? (as I expect you probably know all the supported options and their types better than me at this point)

from contentful-typescript-codegen.

G-Rath avatar G-Rath commented on July 30, 2024

@GabrielAnca I just realised that this is using TypeScript 3 - would you be interested in me upgrading it to use v4? (and prettier?)

from contentful-typescript-codegen.

GabrielAnca avatar GabrielAnca commented on July 30, 2024

🎉 This issue has been resolved in version 3.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

from contentful-typescript-codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.