deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.22k stars 123 forks source link

Vite Plugin #584

Closed tm-3 closed 4 months ago

tm-3 commented 4 months ago

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,
            };
        },
    };
}
marcj commented 4 months ago

interesting, thanks for letting me know. do you wanna create a pull-request? If not I will change it

tm-3 commented 4 months ago

It might be in the tooling I use, my config, or the plugin. I'm also getting some warnings on the source map generation which does not let me attach the vscode debugger to ts files. I would want to look a bit more into this. I have some deadlines I'm against, but I'll submit a PR once I dig into this a bit more and get a good build/test/debug config setup and make sure it's nothing I'm doing first.

tm-3 commented 4 months ago

I've found that if I enable source maps in the plugin configuration everything works as expected. It did not matter if I had them enabled in my tsconfig file or not. Closing this issue.

Thanks!