Skip to content

Commit

Permalink
feature(vite): new vite plugin + ttypescript plugin
Browse files Browse the repository at this point in the history
ref #401
ref #413
  • Loading branch information
marcj committed May 6, 2023
1 parent 1e84c15 commit eb54818
Show file tree
Hide file tree
Showing 25 changed files with 1,032 additions and 8 deletions.
10 changes: 10 additions & 0 deletions packages/type-compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
* You should have received a copy of the MIT License along with this program.
*/

import { declarationTransformer, transformer } from './src/compiler.js';
import type { Program } from 'typescript';

export * from './src/compiler.js';
export * from './src/loader.js';

export default function myTransformerPlugin(program: Program, opts: {}) {
return {
before: transformer,
afterDeclarations: declarationTransformer,
};
}
3 changes: 2 additions & 1 deletion packages/type-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
"author": "Marc J. Schmidt <[email protected]>",
"license": "MIT",
"peerDependencies": {
"typescript": "^4.8.2"
"typescript": "^4.8.2 || ^5.0.0"
},
"devDependencies": {
"@types/lz-string": "^1.3.34",
"@types/micromatch": "^4.0.2"
},
"dependencies": {
"@deepkit/core": "^1.0.1-alpha.93",
"@deepkit/type-spec": "^1.0.1-alpha.93",
"@marcj/ts-clone-node": "^2.0.0",
"@typescript/vfs": "^1.4.0",
Expand Down
11 changes: 7 additions & 4 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import type {
UnionTypeNode,
ParseConfigHost,
} from 'typescript';
import * as ts from 'typescript';
import ts from 'typescript';

import {
ensureImportIsEmitted,
Expand All @@ -92,7 +92,7 @@ import stripJsonComments from 'strip-json-comments';
import { MappedModifier, ReflectionOp, TypeNumberBrand } from '@deepkit/type-spec';
import { Resolver } from './resolver.js';
import { knownLibFilesForCompilerOptions } from '@typescript/vfs';
import { contains } from 'micromatch';
import * as micromatch from 'micromatch';
import { isObject } from '@deepkit/core';

const {
Expand Down Expand Up @@ -681,6 +681,9 @@ export class ReflectionTransformer implements CustomTransformer {
}

if (sourceFile.kind !== SyntaxKind.SourceFile) {
if ('undefined' === typeof require) {
throw new Error(`Invalid TypeScript library imported. SyntaxKind different ${sourceFile.kind} !== ${SyntaxKind.SourceFile}.`);
}
const path = require.resolve('typescript');
throw new Error(`Invalid TypeScript library imported. SyntaxKind different ${sourceFile.kind} !== ${SyntaxKind.SourceFile}. typescript package path: ${path}`);
}
Expand Down Expand Up @@ -1913,7 +1916,7 @@ export class ReflectionTransformer implements CustomTransformer {
protected isExcluded(filePath: string): boolean {
if (!this.currentReflectionConfig.options.exclude) return false;

return contains(filePath, this.currentReflectionConfig.options.exclude, {
return micromatch.contains(filePath, this.currentReflectionConfig.options.exclude, {
basename: true,
cwd: this.currentReflectionConfig.baseDir
});
Expand Down Expand Up @@ -2545,7 +2548,7 @@ export class ReflectionTransformer implements CustomTransformer {
protected parseReflectionMode(mode: typeof reflectionModes[number] | '' | boolean | string | string[] | undefined, configPathDir: string): typeof reflectionModes[number] {
if (Array.isArray(mode)) {
if (!configPathDir) return 'never';
const matches = contains(this.sourceFile.fileName, mode, {
const matches = micromatch.contains(this.sourceFile.fileName, mode, {
cwd: configPathDir
});

Expand Down
2 changes: 1 addition & 1 deletion packages/type-compiler/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CompilerOptions, SourceFile, TransformationContext } from 'typescript';
import * as ts from 'typescript';
import ts from 'typescript';
import { ReflectionTransformer } from './compiler.js';

export class DeepkitLoader {
Expand Down
2 changes: 1 addition & 1 deletion packages/type-compiler/src/reflection-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type {
StringLiteralLike,
SymbolTable,
} from 'typescript';
import * as ts from 'typescript';
import ts from 'typescript';
import { cloneNode as tsNodeClone, CloneNodeHook } from '@marcj/ts-clone-node';
import { SourceFile } from './ts-types.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/type-compiler/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
SourceFile,
StringLiteral,
} from 'typescript';
import * as ts from 'typescript';
import ts from 'typescript';

const {
createSourceFile,
Expand Down
3 changes: 3 additions & 0 deletions packages/type-compiler/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"references": [
{
"path": "../type-spec/tsconfig.json"
},
{
"path": "../core/tsconfig.json"
}
]
}
3 changes: 3 additions & 0 deletions packages/type-compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"references": [
{
"path": "../type-spec/tsconfig.json"
},
{
"path": "../core/tsconfig.json"
}
]
}
1 change: 1 addition & 0 deletions packages/vite/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests
1 change: 1 addition & 0 deletions packages/vite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deepkit Vite Plugin
Empty file added packages/vite/dist/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions packages/vite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/plugin.js';
Loading

0 comments on commit eb54818

Please sign in to comment.