Skip to content

Commit

Permalink
feat: support .tsx file as entry
Browse files Browse the repository at this point in the history
  • Loading branch information
uinz committed Aug 29, 2024
1 parent df8668b commit 44b2487
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/utils/get-source-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const tryExtensions = async (
};

const extensionMap = {
'.d.ts': ['.d.ts', '.d.mts', '.d.cts', '.ts', '.mts', '.cts'],
'.d.mts': ['.d.mts', '.d.ts', '.d.cts', '.ts', '.mts', '.cts'],
'.d.cts': ['.d.cts', '.d.ts', '.d.mts', '.ts', '.mts', '.cts'],
'.d.ts': ['.d.ts', '.d.mts', '.d.cts', '.ts', '.tsx', '.mts', '.cts'],
'.d.mts': ['.d.mts', '.d.ts', '.d.cts', '.ts', '.tsx', '.mts', '.cts'],
'.d.cts': ['.d.cts', '.d.ts', '.d.mts', '.ts', '.tsx', '.mts', '.cts'],
'.js': ['.js', '.ts', '.tsx', '.mts', '.cts'],
'.mjs': ['.mjs', '.js', '.cjs', '.mts', '.cts', '.ts'],
'.cjs': ['.cjs', '.js', '.mjs', '.mts', '.cts', '.ts'],
'.mjs': ['.mjs', '.js', '.cjs', '.mts', '.cts', '.ts', '.tsx'],
'.cjs': ['.cjs', '.js', '.mjs', '.mts', '.cts', '.ts', '.tsx'],
} as const;

const distExtensions = Object.keys(extensionMap) as (keyof typeof extensionMap)[];
Expand Down
27 changes: 26 additions & 1 deletion tests/specs/builds/output-commonjs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { pkgroll } from '../../utils.js';
import { packageFixture, createPackageJson } from '../../fixtures.js';
import { packageFixture, createPackageJson, createTsconfigJson } from '../../fixtures.js';

export default testSuite(({ describe }, nodePath: string) => {
describe('output: commonjs', ({ test }) => {
Expand Down Expand Up @@ -126,6 +126,31 @@ export default testSuite(({ describe }, nodePath: string) => {
expect(content).toMatch('exports.sayHello =');
});

test('{ type: commonjs, field: component, srcExt: mjs, distExt: cjs }', async () => {
await using fixture = await createFixture({
...packageFixture(),
'package.json': createPackageJson({
main: './dist/component.cjs',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
jsx: 'preserve',
},
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/component.cjs', 'utf8');
expect(content).toMatch('exports.Component = Component');
});

test('nested directory', async () => {
await using fixture = await createFixture({
...packageFixture(),
Expand Down
20 changes: 20 additions & 0 deletions tests/specs/builds/output-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ export default testSuite(({ describe }, nodePath: string) => {
expect(content).toMatch('export { cjs$1 as default }');
});

test('{ type: commonjs, field: component, srcExt: tsx, distExt: mjs }', async () => {
await using fixture = await createFixture({
...packageFixture(),
'package.json': createPackageJson({
main: './dist/component.mjs',
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/component.mjs', 'utf8');
expect(content).toMatch('export { Component }');
});

test('{ type: commonjs, field: main, srcExt: mts, distExt: mjs }', async () => {
await using fixture = await createFixture({
...packageFixture(),
Expand Down
75 changes: 75 additions & 0 deletions tests/specs/builds/output-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,81 @@ export default testSuite(({ describe }, nodePath: string) => {
expect(content).toMatch('declare function');
});

test('{ srcExt: tsx, distExt: d.ts }', async () => {
await using fixture = await createFixture({
...packageFixture({ installTypeScript: true }),
'package.json': createPackageJson({
types: './dist/component.d.ts',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
jsx: 'preserve',
},
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/component.d.ts', 'utf8');
expect(content).toMatch('declare const Component');
});

test('{ srcExt: tsx, distExt: d.mts }', async () => {
await using fixture = await createFixture({
...packageFixture({ installTypeScript: true }),
'package.json': createPackageJson({
types: './dist/component.d.mts',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
jsx: 'preserve',
},
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/component.d.mts', 'utf8');
expect(content).toMatch('declare const Component');
});

test('{ srcExt: tsx, distExt: d.cts }', async () => {
await using fixture = await createFixture({
...packageFixture({ installTypeScript: true }),
'package.json': createPackageJson({
types: './dist/component.d.cts',
}),
'tsconfig.json': createTsconfigJson({
compilerOptions: {
jsx: 'preserve',
},
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/component.d.cts', 'utf8');
expect(content).toMatch('declare const Component');
});

test('{ srcExt: .mts, distExt: d.cts }', async () => {
await using fixture = await createFixture({
...packageFixture({ installTypeScript: true }),
Expand Down

0 comments on commit 44b2487

Please sign in to comment.