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

Apply sourcemap in @parcel/transformer-typescript-tsc #7287

Merged
merged 3 commits into from
Nov 14, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// comment

type X = number;

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