Skip to content

Option to extend transform, not rewrite it #415

@Gwynerva

Description

@Gwynerva

Describe the feature

Right now if you define transform option when creating jiti, it will completely drop default behaviour.
So to add support for custom extensions one have to create a dummy jiti instance, take default transition from there and use it in actual jiti:

    const defaultJiti = createJiti(ERUDIT.config.paths.project, {
        fsCache: false,
        moduleCache: false,
        jsx: {
            runtime: 'automatic',
            importSource: '@erudit-js/prose',
        },
    });

    const defaultTransform = defaultJiti.transform.bind(defaultJiti);

    jiti = createJiti(ERUDIT.config.paths.project, {
        fsCache: false, // true
        moduleCache: false,
        jsx: {
            runtime: 'automatic',
            importSource: '@erudit-js/prose',
        },
        extensions: [
            '.ts',
            '.tsx',
            '.js',
            '.jsx',
            '.json',
            '.jpg',
            '.png',
            '.svg',
        ],
        transform: (opts) => {
            if (opts.filename && opts.filename.endsWith('.jpg')) {
                console.log('Transforming JPG:', opts.filename);

                return { code: `exports.default = 'foobar';` };
            }
            return { code: defaultTransform(opts) };
        },
    });

I think it would be a good idea to support extending default transition or passing default transition method inside redefined transition to use it there.

Sounds like a good idea to me... Or I am doing something wrong and not where it is intended to be done?

Context

I use jiti in my auto-generated documentation project to read documentation .tsx files containing prose (written in my own custom JSX DSL):

import testImage from './testImage.jpg';
import { pifagorTheorem } from './pifagor.tsx';

export default (<>
    <p>Paragraph<p>
    {pifagorTheorem}
    <Image module={testImage} />
    ...
</>)

Obviously, I can't use ESM await import(...) to import such prose files because Node does not know how to handle TypeScript, JSX and custom extensions. So I use jiti to import such files.

Additional information

  • Would you be willing to help implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions