Skip to content

Conversation

zsilbi
Copy link
Collaborator

@zsilbi zsilbi commented May 28, 2025

Resolves #6
Resolves #33
Resolves #35

Sure ideally we can start with an mkdist-compatible context API for custom extensions in transpiler.

Originally posted by @pi0 in #6

I made this as a starting point for further discussions about #6, it's mainly based on the mkdist loaders.

New features in this PR:

Plugin support for transform entries.

Plugin monorepo will be available here soon: https://github.com/unjs/obuild-plugins
Until the packages are not published, they are packed and resolved from the /plugins dir in this PR.

Possible setup:

 {
    type: "transform",
    input: "src/",
    outDir: "dist/",
    plugins: [
      oxcDts({ force: true }), // Force generate, skip emitting a file if isolatedDeclarations fails
      tsgoDts(), // Reuse .d.(m)ts from `oxc-dts` and generate for TS files that `oxc-dts` could not work on
      vueTscDts(), // Reuse .d.(m)ts from `oxc-dts` and `tsgo-dts` and generate only for Vue files
      vueSFCTransformer(), // Transform Vue SFCs
      oxcTransform(), // Transform others
      sass(),
      postcss(),
    ],
  }

Plugins have a transform() method, we call these recursively in the order that is provided in the configuration.

  • if it returns with an array of files, we go through each file and try to call the next plugins transform method with it
  • if it returns undefined we go to the next plugin

Plugins can also have temp. storage assigned to them for each entry context. This is useful when you for example define the plugins once and use them for multiple entries with the same configuration.

TS Config resolution

On build start we find and resolve all paths in the TS Config to absolute paths based on where the actual configuration coming from. (TS config entries can also be added into the transform entries).
Later the transform plugins can use these to make relative paths based on their requirements.

Source map serialization/path rewrite

The plugins don't have enough context to know where each file will be written to, this is done here.
Added mapDir config option to write these separately. (This directory is not cleaned on each build automatically)

declarationDir support

When provided in TS config, the declarations are written in the provided directory (This directory is not cleaned on each build automatically)

Copy link

codecov bot commented May 28, 2025

Codecov Report

Attention: Patch coverage is 98.86621% with 5 lines in your changes missing coverage. Please review.

Project coverage is 84.10%. Comparing base (dabd9a0) to head (81ea286).

Files with missing lines Patch % Lines
src/builders/transform.ts 95.91% 4 Missing ⚠️
src/utils.ts 91.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main      #34       +/-   ##
===========================================
+ Coverage   72.37%   84.10%   +11.72%     
===========================================
  Files           9       19       +10     
  Lines         601      868      +267     
  Branches       96      178       +82     
===========================================
+ Hits          435      730      +295     
+ Misses        165      137       -28     
  Partials        1        1               

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pi0
Copy link
Member

pi0 commented May 28, 2025

That was fast 🚀

I will check better later. In the meantime, can you examine how compatible the current implementation is with mkdist and vue-sfc-transformer API? 🙏🏼

@pi0 pi0 changed the title feat(transformer): extendable transformers, jsx/tsx transform, source maps feat: extendable transformers May 28, 2025
@zsilbi
Copy link
Collaborator Author

zsilbi commented May 29, 2025

I will check better later. In the meantime, can you examine how compatible the current implementation is with mkdist and vue-sfc-transformer API? 🙏🏼

Using the loader from vue-sfc-transformer/mkdist the transformation works, of course without declarations emit as that's handled in mkdist.

@zsilbi zsilbi force-pushed the feat/extendable-transformers branch from eaccb0d to 253cf48 Compare May 29, 2025 00:29
@pi0
Copy link
Member

pi0 commented May 29, 2025

Cool! We probably have to also support alternative dts transformer based on this later for (non infered) type gen.

@zsilbi
Copy link
Collaborator Author

zsilbi commented May 29, 2025

Cool! We probably have to also support alternative dts transformer based on this later for (non infered) type gen.

I'll leave some notes here about the current process if we want to do this later.

With mkdist the Vue declarations are generated in the following way:

  1. vue-sfc-transformer/mkdist transforms them as .js files
    https://github.com/nuxt-contrib/vue-sfc-transformer/blob/483fbf466332ef97d9ec3dbb58c9f2c6d4fa03d7/src/utils/mkdist.ts#L87-L92

  2. mkdist adds declaration outputs for every .js files (why not only .ts?) in the js loader
    https://github.com/unjs/mkdist/blob/a7f029d9dc4ee083f7426d383f1622df4927eb09/src/loaders/js.ts#L22-L33

  3. vue-sfc-transformer/mkdist filters these added delaration outputs and adds them to it's own outputs
    https://github.com/nuxt-contrib/vue-sfc-transformer/blob/483fbf466332ef97d9ec3dbb58c9f2c6d4fa03d7/src/utils/mkdist.ts#L93

  4. mkdist later processes all declarations files using two utility loaders
    https://github.com/unjs/mkdist/blob/a7f029d9dc4ee083f7426d383f1622df4927eb09/src/make.ts#L111-L126

@pi0
Copy link
Member

pi0 commented May 29, 2025

Thanks for the notes.

/cc @danielroe @Teages re vue-sfc types part.

@zsilbi zsilbi changed the title feat: extendable transformers feat: transform plugins Jun 17, 2025
@zsilbi zsilbi marked this pull request as ready for review June 17, 2025 20:14
@zsilbi
Copy link
Collaborator Author

zsilbi commented Jun 17, 2025

I moved every non-core code to the new monorepo and updated the first post in this PR to reflect the changes more accurately.

@zsilbi
Copy link
Collaborator Author

zsilbi commented Jun 19, 2025

Important

I had to add "version": "0.3.0" to the package.json in order to get the CI working here with the new @obuild packages as they have >=0.3.0 set as peer version.

@zsilbi zsilbi requested a review from pi0 June 19, 2025 13:29
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.

Support declarations generation after transform Add support for transforming .tsx and .jsx Extendable transformers
3 participants