Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tsc sourcemaps metadata #8734

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);