Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
fix: source map filename for mts and cts files (#6)
Browse files Browse the repository at this point in the history
* fix: source map filename for mts and cts files

* fix: cts & mts source map file

* style: lintfix

Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
DylanPiercey and privatenumber authored Jun 21, 2022
1 parent 346b953 commit 7348ce3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
18 changes: 16 additions & 2 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ export function transformSync(
code,
[
// eslint-disable-next-line @typescript-eslint/no-shadow
code => esbuildTransformSync(code, esbuildOptions),
(code) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const transformed = esbuildTransformSync(code, esbuildOptions);
if (esbuildOptions.sourcefile !== filePath) {
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
}
return transformed;
},
transformDynamicImport,
] as const,
);
Expand Down Expand Up @@ -85,7 +92,14 @@ export async function transform(

const transformed = await applyTransformers(code, [
// eslint-disable-next-line @typescript-eslint/no-shadow
code => esbuildTransform(code, esbuildOptions),
async (code) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const transformed = await esbuildTransform(code, esbuildOptions);
if (esbuildOptions.sourcefile !== filePath) {
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
}
return transformed;
},
transformDynamicImport,
] as const);

Expand Down
52 changes: 35 additions & 17 deletions tests/specs/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ export default testSuite(({ describe }) => {
).not.toThrow();
});

// test('failing: sourcemap', () => {
// const fileName = 'file.mts';
// const transformed = transformSync(
// fixtures.ts,
// fileName,
// {
// format: 'esm',
// },
// );

// expect(transformed.map).not.toBe('');

// const map = JSON.parse(transformed.map);

// expect(map.sources.length).toBe(1);
// expect(map.sources[0]).toBe(fileName);
// });
test('sourcemap file', () => {
const fileName = 'file.mts';
const transformed = transformSync(
fixtures.ts,
fileName,
{
format: 'esm',
},
);

expect(transformed.map).not.toBe('');

const map = JSON.parse(transformed.map);

expect(map.sources.length).toBe(1);
expect(map.sources[0]).toBe(fileName);
});
});

describe('async', ({ test }) => {
Expand All @@ -85,6 +85,24 @@ export default testSuite(({ describe }) => {
const imported = await import(base64Module(transformed.code));
expect(JSON.stringify(imported)).toMatch('{"default":"default value","named":"named"}');
});

test('sourcemap file', async () => {
const fileName = 'file.cts';
const transformed = await transform(
fixtures.ts,
fileName,
{
format: 'esm',
},
);

expect(transformed.map).not.toBe('');

const map = JSON.parse(transformed.map);

expect(map.sources.length).toBe(1);
expect(map.sources[0]).toBe(fileName);
});
});
});
});

0 comments on commit 7348ce3

Please sign in to comment.