Coder Social home page Coder Social logo

Comments (4)

florian-lefebvre avatar florian-lefebvre commented on June 13, 2024 1

mmh could it be possible to take the content and format it without a super huge dependency instead (or even none)? this way users don't have to think about it, they pass their file content badly formatted and we do it for them?

from astro-integration-kit.

BryceRussell avatar BryceRussell commented on June 13, 2024

Misunderstanding: Related to this issue #65 about generating virtual module import/exports, if AIK generates import/exports it could also generate types for the module which would require a way to buffer lines/types and compile into a string for addDts

+1 for having this functionality native to addDts or AIK in general it would make it a lot easier to generate good clean looking types (and do so dynamically)

I have a small implementation for something similar here to get an idea on how to do something like this without a package. I originally authored this as a plugin for AIK but had to move away from defineIntegration for more control over typing. This is what my API looked while I had a plugin:

"astro:config:setup": ({ createDtsBuffer }) => {
  
  const {
    createLineBuffer,
    addLinesToDts,
    addLinesToDtsInterface,
    addLinesToDtsNamespace,
    addLinesToDtsModule,
    writeDtsBuffer,
  } = createDtsBuffer("my-integration")
  
  // Add lines to .d.ts file
  addLinesToDts(`
    type ThemeName = "${themeName}";
    type ThemeConfig = NonNullable<Parameters<typeof import("${entrypoint}").default>[0]>["config"]
  `)
  
  // Add lines for virtual module 'my-module'
  addLinesToDtsModule(
    "my-module",
    Object.entries(objectModule)
      .map(([name, entrypoint]) => `export const "${camelCase(name)}": typeof import("${resolveImport(entrypoint)}").default;`)
  );
  
  // Create your own line buffer (useful for generating nested types like objects inside interfaces)
  const typeBuffer = createLineBuffer()
  
  typeBuffer.add(Object.entries(objectModule).map(() => ...))
  
  // Using the line buffer
  addLinesToDtsInterface(
    "SomeInterface",
    typeBuffer.lines
  )
  
  
  // Compile buffer into a string and write to .d.ts file
  writeDtsBuffer()

}

(This is just an example of how I solved this, not necessarily a suggestion for a full AIK API)

from astro-integration-kit.

jdtjenkins avatar jdtjenkins commented on June 13, 2024

@florian-lefebvre Yeah I'd be up for both! There are times when you want to dynamically create dts content or virtual module content and a helper like FileFactory or the far, far superior createLineBuffer @BryceRussell mentioned is a handy way to do it.

That being said we can definitely run the content pass into addDts through unindent and then at least then the user doesn't have to worry about that for one off-files or whatever and still gets neat af files

from astro-integration-kit.

florian-lefebvre avatar florian-lefebvre commented on June 13, 2024

That one sounds interesting, idk if it has support for TS https://github.com/benjamn/recast

EDIT: it has support for it by specifying a specific parser

from astro-integration-kit.

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.