4.0.0 • Published 2 years ago

@ssen/require-typescript v4.0.0

Weekly downloads
55
License
MIT
Repository
github
Last release
2 years ago

Require from Typescript source

// hello.ts
export const hello: number = 3;
// main
import { requireTypescript } from '@ssen/require-typescript';

const { hello } = requireTypescript < { hello: number } > './hello.ts';
console.assert(hello === 3);

Test Codes

__tests__/requireTypescript.test.ts

import path from 'path';
import { requireTypescript } from '@ssen/require-typescript';
import process from 'process';
import { describe, test, expect } from 'vitest';

describe('requireTypescript', () => {
  test.each(['basic', 'js'])('should get exports from %s', (dir: string) => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), `test/fixtures/require-typescript/${dir}/hello`),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/with-import`,
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/with-module`,
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(
      path.join(process.cwd(), `test/fixtures/require-typescript/${dir}/func`),
    );
    expect(func(1, 2)).toBe(3);

    const { some } = requireTypescript<{ some: string }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/dirname`,
      ),
    );
    expect(some).toBe(
      path.join(
        path.join(
          process.cwd(),
          `test/fixtures/require-typescript/${dir}/hello`,
        ),
      ),
    );
  });

  test('should get exports with index file', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), 'test/fixtures/require-typescript/index/hello'),
    );
    expect(hello).toBe(1);
  });

  test('should get typescript exports', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/hello.ts',
      ),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/with-import.ts',
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/with-module.ts',
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/func.ts',
      ),
    );
    expect(func(1, 2)).toBe(3);
  });

  test('should get javascript exports', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), 'test/fixtures/require-typescript/js/hello.js'),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/js/with-import.js',
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/js/with-module.js',
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(path.join(process.cwd(), 'test/fixtures/require-typescript/js/func.js'));
    expect(func(1, 2)).toBe(3);
  });
});
4.0.0-alpha.1

2 years ago

4.0.0

2 years ago

3.0.3

2 years ago

3.0.2-alpha.1

3 years ago

3.0.2

3 years ago

3.0.0

3 years ago

3.0.0-alpha.7

3 years ago

3.0.0-alpha.5

3 years ago

3.0.0-alpha.1

3 years ago

3.0.0-alpha.3

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.2

3 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.2.0-alpha.1

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

2.1.0-alpha.1

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

1.0.0-alpha.9

4 years ago

1.0.0-alpha.8

4 years ago

1.0.0-alpha.7

4 years ago

1.0.0-alpha.6

4 years ago

1.0.0-alpha.5

4 years ago

1.0.0-alpha.4

4 years ago

1.0.0-alpha.3

4 years ago

1.0.0-alpha.1

4 years ago

0.2.9

4 years ago

0.2.10

4 years ago

0.2.7

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.1.0

4 years ago