deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

Vite Plugin

Open tm-3 opened this issue 1 year ago • 0 comments

I use the Wallaby.JS extension along with vite/vitest while I'm working away. Wallaby does a lot in the editor to highlight errors, show values, and in the case of this issue it adds indicators in the gutters that show code coverage, paths not taken, and errors: image

When running with the vite plugin enabled it moves the indicators out of alignment by one line: image

This can be fixed by adding a '\n' + to the return of the transform function in the plugin.ts and it's probably more to do with the output of the Object.assign.

Close this issue out any time, but I wanted to bring it up because it seems like one of those little things that comes up in a strange place that will cost someone hours of their life and a bit more of their hairline.

import { createFilter } from '@rollup/pluginutils';
import ts from 'typescript';
import { declarationTransformer, transformer } from '@deepkit/type-compiler';
import type { Plugin } from 'vite';
import { cwd } from 'process';

export interface Options {
    include?: string;
    exclude?: string;
    tsConfig?: string;
    transformers?: ts.CustomTransformers;
    compilerOptions?: ts.CompilerOptions;
}

export function deepkitType(options: Options = {}): Plugin {
    const filter = createFilter(options.include ?? ['**/*.tsx', '**/*.ts'], options.exclude ?? 'node_modules/**');
    const transformers = options.transformers || {
        before: [transformer],
        after: [declarationTransformer],
    };
    return {
        name: 'deepkit-type',
        enforce: 'pre',
        transform(code: string, fileName: string) {
            if (!filter(fileName)) return null;
            const transformed = ts.transpileModule(code, {
                'compilerOptions': Object.assign({
                    'target': ts.ScriptTarget.ESNext,
                    'module': ts.ModuleKind.ESNext,
                    configFilePath: options.tsConfig || cwd() + '/tsconfig.json',
                }, options.compilerOptions || {}),
                fileName,
                transformers
            });

            return {
                code: '\n' + transformed.outputText,
                map:  transformed.sourceMapText,
            };
        },
    };
}

tm-3 avatar Jul 01 '24 00:07 tm-3