-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Incorrect sourcemaps are being generated with @parcel/transformer-typescript-tsc #7570
Comments
I played around with the transformer and found out that if we don't pass the
EDIT: |
Back at it again and after entering a deep rabbit hole I think I've fixed it, at least for my own use case. import type { TranspileOptions } from 'typescript';
import typescript from 'typescript';
import { Transformer } from '@parcel/plugin';
import SourceMap from '@parcel/source-map';
import { loadTSConfig } from '@parcel/ts-utils';
import { relativeUrl } from '@parcel/utils';
export default new Transformer({
loadConfig({ config, options }) {
return loadTSConfig(config, options);
},
async transform({ asset, config, options }) {
asset.type = 'js';
let code = await asset.getCode();
let transpiled = typescript.transpileModule(code, {
compilerOptions: {
// React is the default. Users can override this by supplying their own tsconfig,
// which many TypeScript users will already have for typechecking, etc.
jsx: typescript.JsxEmit.React,
...config,
// Always emit output
noEmit: false,
// 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?
} as TranspileOptions);
const originalMap = await asset.getMap();
let map: SourceMap;
let { outputText, sourceMapText } = transpiled;
if (sourceMapText != null) {
map = new SourceMap(options.projectRoot);
type Writeable<T> = { -readonly [P in keyof T]: Writeable<T[P]> };
const sourcePath = relativeUrl(options.projectRoot, asset.filePath);
const jsourceMap = JSON.parse(sourceMapText) as Writeable<
Parameters<typeof map['addVLQMap']>[0]
>;
jsourceMap.file = sourcePath;
// replace path with one relative to root
jsourceMap.sources[0] = sourcePath;
map.addVLQMap(jsourceMap);
// extend original if there is one
if (originalMap) {
map.extends(originalMap.toBuffer());
}
outputText = outputText.substring(
0,
outputText.lastIndexOf('//# sourceMappingURL')
);
}
return [
{
type: 'js',
content: outputText,
map,
},
];
},
}); |
@ImpendingDoom28
I'm using a mono repo so in my case it was a simple matter of adding it under |
@iSplasher Thank you with for this detailed explanation and transformer! Maybe you consider to add it to the NPM registry? Or maybe I can try to do it... |
I tried to create NPM package for this and it works like a charm! I'll give the credits to you @iSplasher Leaving a link for everyone, who wants to install it: https://www.npmjs.com/package/parcel-transformer-tsc-sourcemaps |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. |
This worked! Life saving. A shame this doesn't work out of the box with the regular Parcel transformer |
PR to fix this: #8734 |
Correct sourcemaps for TypeScript files will now be generated. (See parcel-bundler/parcel#7570 for more details.)
Correct sourcemaps for TypeScript files will now be generated. (See parcel-bundler/parcel#7570 for more details.)
🐛 bug report
A sourcemap bug occurs when using
You get duplicate entries in the
sources
field in the generated.map
file.Only the subsequent sources that only refer to the filenames are mapped (
server.ts
,index.ts
,same.ts
),but these obviously point nowhere relative to the
sourceRoot
so they are invalid.Additionally, if two filenames are identical when importing from another package in the mono repo,
their entries get overridden (notice how there is only one
same.ts
).You can easily see this if you inspect the sourcemap on https://sourcemap-visualiser.vercel.app/
A
sourcemap-info.json
file has already been generated in the repro repo.If you remove the transformer plugin from
.parcelrc
, correct sourcemaps are generated.🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
Correct sourcemap gets generated
😯 Current Behavior
An incorrect sourcemap is generated
💁 Possible Solution
A workaround is to not use the the tsc transformer plugin, so remove this
but this is not an option for me due to #7425
💻 Code Sample
I've set up a full reproduction of the issue here https://github.com/iSplasher/parcel-sourcemap-bug
🌍 Your Environment
The text was updated successfully, but these errors were encountered: