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

Custom transforms in v2 #270

Closed
1 task
webpro opened this issue Jul 3, 2024 · 3 comments
Closed
1 task

Custom transforms in v2 #270

webpro opened this issue Jul 3, 2024 · 3 comments
Labels
discussion enhancement New feature or request

Comments

@webpro
Copy link

webpro commented Jul 3, 2024

Describe the feature

Perhaps this should be a discussion, but that led me to a more generic forum. Feel free to close or move it.

Great work on v2, glad this seems to be progressing wonderfully!

Using jiti v1, Knip can do this:

import transform from 'jiti/dist/babel.js';
import type { TransformOptions } from 'jiti/dist/types.js';

The goal is to use a custom transform and remove a specific line from the imported file:

https://github.com/webpro-nl/knip/blob/main/packages/knip/src/util/require.ts#L55-L65

(this is some necessary evil because knip is loading the eslint config file from a different cwd)

Can I do something similar in v2, or maybe there's an alternative/better way to go about this?

Currently playing a bit with v2 and 2.0.0-beta.3 TS doesn't allow importing transform and neither does Node.js:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/babel.cjs' is not defined by "exports" in [..]/node_modules/jiti/package.json imported from [..]/knip/dist/util/require.js

Additional information

  • Would you be willing to help implement this feature?
@webpro webpro added the enhancement New feature or request label Jul 3, 2024
@pi0
Copy link
Member

pi0 commented Jul 3, 2024

Thanks for kind words. It is a tricky situation honestly. I also used jiti in places like unbuild/untyed as custom loader with custom transform and i know there are other libs that hook into jiti to add their transformer replacing babel.

But this also pushed back jiti from natively supporting more features and also when using jiti in envs like bun (and in the future vitest, web, deno) we want to bypass jiti as the core functionality (TS support) is natively available in those platforms.

Thanks for sharing usecase. I think if jiti exposes an agnostic transform hook it would fix your issue? (also have you tried using alias option to patch that specific package?)

@pi0 pi0 added the discussion label Jul 3, 2024
@webpro
Copy link
Author

webpro commented Jul 3, 2024

Hah, alias solves my issue completely, eg:

const empty = join(fileURLToPath(import.meta.url), '../empty.js');

const options = {
  interopDefault: true,
  extensions: DEFAULT_EXTENSIONS,
  esmResolve: false,
  alias: {
    '@rushstack/eslint-config/patch/modern-module-resolution': empty,
    '@rushstack/eslint-patch/modern-module-resolution': empty,
  },
};

const createLoader = (options: JitiOptions) => createJiti(process.cwd(), options);

In webpro-nl/knip@b9f1715

Tried in both v1 and v2. Thanks, this is a great cleanup - why didn't I try this before :)

(v2 migration in webpro-nl/knip@c373690 which works great too!)

@webpro
Copy link
Author

webpro commented Jul 16, 2024

Let's just close this one, thanks @pi0! Keep up the great work.

@webpro webpro closed this as completed Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants