Skip to content

Commit

Permalink
Fix tsc sourcemaps metadata (#8734)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Jan 3, 2023
1 parent 737fada commit ea61ad9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
13 changes: 6 additions & 7 deletions packages/core/integration-tests/test/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,11 @@ describe('sourcemaps', function () {
it('should create a valid sourcemap when using the Typescript tsc transformer', async function () {
let inputFilePath = path.join(
__dirname,
'/integration/sourcemap-typescript-tsc/index.ts',
'/integration/sourcemap-typescript-tsc/src/index.ts',
);

await bundle(inputFilePath);
let distDir = path.join(__dirname, '../dist/');
let filename = path.join(distDir, 'index.js');
let b = await bundle(inputFilePath);
let filename = b.getBundles()[0].filePath;
let raw = await outputFS.readFile(filename, 'utf8');
let mapUrlData = await loadSourceMapUrl(outputFS, filename, raw);
if (!mapUrlData) {
Expand All @@ -599,8 +598,8 @@ describe('sourcemaps', function () {
sourceMap.addVLQMap(map);

let mapData = sourceMap.getMap();
assert.equal(mapData.sources.length, 1);
assert.deepEqual(mapData.sources, ['index.ts']);
assert.deepEqual(mapData.sources, ['src/index.ts']);
assert(map.sourcesContent.every(s => s));

let input = await inputFS.readFile(
path.join(path.dirname(filename), map.sourceRoot, map.sources[0]),
Expand All @@ -611,7 +610,7 @@ describe('sourcemaps', function () {
source: input,
generated: raw,
str: 'nonExistsFunc',
sourcePath: 'index.ts',
sourcePath: 'src/index.ts',
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/core/integration-tests/test/typescript-tsc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import assert from 'assert';
import path from 'path';
import {
Expand Down
33 changes: 18 additions & 15 deletions packages/transformers/typescript-tsc/src/TSCTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export default (new Transformer({
},

async transform({asset, config, options}) {
asset.type = 'js';

let code = await asset.getCode();
let [code, originalMap] = await Promise.all([
asset.getCode(),
asset.getMap(),
]);

let transpiled = typescript.transpileModule(
code,
Expand All @@ -30,30 +31,32 @@ export default (new Transformer({
// Don't compile ES `import`s -- scope hoisting prefers them and they will
// otherwise compiled to CJS via babel in the js transformer
module: typescript.ModuleKind.ESNext,
sourceMap: !!asset.env.sourceMap,
sourceMap: Boolean(asset.env.sourceMap),
mapRoot: options.projectRoot,
},
fileName: asset.filePath, // Should be relativePath?
}: TranspileOptions),
);

let map;
let {outputText, sourceMapText} = transpiled;
if (sourceMapText != null) {
map = new SourceMap(options.projectRoot);
map.addVLQMap(JSON.parse(sourceMapText));

if (sourceMapText != null) {
outputText = outputText.substring(
0,
outputText.lastIndexOf('//# sourceMappingURL'),
);

let map = new SourceMap(options.projectRoot);
map.addVLQMap(JSON.parse(sourceMapText));
if (originalMap) {
map.extends(originalMap);
}
asset.setMap(map);
}

return [
{
type: 'js',
content: outputText,
map,
},
];
asset.type = 'js';
asset.setCode(outputText);

return [asset];
},
}): Transformer);

0 comments on commit ea61ad9

Please sign in to comment.