Skip to content

Commit

Permalink
Apply sourcemap in @parcel/transformer-typescript-tsc (#7287)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 14, 2021
1 parent f651829 commit 8928430
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}
@@ -0,0 +1,5 @@
// comment

type X = number;

nonExistsFunc();
@@ -0,0 +1,6 @@
{
"name": "parcel-sourcemap-integration-test",
"version": "1.0.0",
"license": "MIT",
"private": true
}
Empty file.
40 changes: 40 additions & 0 deletions packages/core/integration-tests/test/sourcemaps.js
Expand Up @@ -563,6 +563,46 @@ 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',
);

await bundle(inputFilePath);
let distDir = path.join(__dirname, '../dist/');
let filename = path.join(distDir, 'index.js');
let raw = await outputFS.readFile(filename, 'utf8');
let mapUrlData = await loadSourceMapUrl(outputFS, filename, raw);
if (!mapUrlData) {
throw new Error('Could not load map');
}
let map = mapUrlData.map;

assert.equal(map.file, 'index.js.map');
assert(raw.includes('//# sourceMappingURL=index.js.map'));
// assert.equal(map.sourceRoot, '/__parcel_source_root/');

let sourceMap = new SourceMap('/');
sourceMap.addVLQMap(map);

let mapData = sourceMap.getMap();
assert.equal(mapData.sources.length, 1);
assert.deepEqual(mapData.sources, ['index.ts']);

let input = await inputFS.readFile(
path.join(path.dirname(filename), map.sourceRoot, map.sources[0]),
'utf8',
);
checkSourceMapping({
map: sourceMap,
source: input,
generated: raw,
str: 'nonExistsFunc',
sourcePath: 'index.ts',
});
});

it('should create a valid sourcemap for a CSS bundle', async function () {
async function test(minify) {
let inputFilePath = path.join(
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/typescript-tsc/package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@parcel/plugin": "^2.0.1",
"@parcel/source-map": "^2.0.0",
"@parcel/ts-utils": "^2.0.1"
},
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions packages/transformers/typescript-tsc/src/TSCTransformer.js
Expand Up @@ -5,13 +5,14 @@ import type {TranspileOptions} from 'typescript';
import {Transformer} from '@parcel/plugin';
import {loadTSConfig} from '@parcel/ts-utils';
import typescript from 'typescript';
import SourceMap from '@parcel/source-map';

export default (new Transformer({
loadConfig({config, options}) {
return loadTSConfig(config, options);
},

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

let code = await asset.getCode();
Expand All @@ -29,15 +30,29 @@ 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,
},
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));

outputText = outputText.substring(
0,
outputText.lastIndexOf('//# sourceMappingURL'),
);
}

return [
{
type: 'js',
content: transpiled.outputText,
content: outputText,
map,
},
];
},
Expand Down

0 comments on commit 8928430

Please sign in to comment.