-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Source map line numbers are wrong when using typescript #5182
Comments
Thanks for the issue report and help in reproducing this. If this only happens when using TypeScript you probably will get a better response filing this issue in the corresponding subproject. Though I'm not entirely sure whether that's https://github.com/sveltejs/svelte-preprocess or https://github.com/sveltejs/language-tools (if this is an issue in |
This is probably just because preprocessors don’t support sourcemaps. There is a PR for this somewhere. |
sveltejs/svelte-preprocess#171 says "Added general |
Setting the
Source maps are generated and reference the wrong line. Hm. |
Any sourcemaps generated by svelte-preprocess (and I don't know that it is generating any) currently won't do anything because the main compiler doesn't handle inputting sourcemaps - and so the sourcemaps in the compiled components will point to lines in the already-preprocessed versions of the components. Support for this will need to be added in Svelte (there is a PR for this somewhere that hasn't been reviewed yet), and the bundler plugins will need to be updated to pass the sourcemaps from the preprocessor to the compiler, and also the preprocessor will need to actually return sourcemaps. |
Ah, thanks for the clarification. And here's the PR for reference: #5015 |
I tried to check status on this, but gets lost in github refs. Are we close to a solution? |
This PR with the bulk of the work, as far as I can see, is approved and waiting to be merged: #5584 So I believe the answer is yes we are close. |
still an issue with import alias repro
errors start at line 50
src/components/apps/WallpaperApp/Wallpaper.svelte import { wallpapersConfig } from '$src/configs/wallpapers/wallpaper.config'; relevant config ... vite.config.ts export default defineConfig({
resolve: {
alias: {
'$src': path.resolve('./src')
},
}, tsconfig.json {
"compilerOptions": {
//"baseUrl": ".",
"paths": {
"$src/*": ["src/*"]
},
},
} i tried to patch alias imports to relative imports with /*
https://stackoverflow.com/questions/57188027/refactor-aliased-imports-to-relative-paths
pnpm i -D jscodeshift @babel/core
# problem: jscodeshift cannot parse *.svelte files
npx jscodeshift --extensions=svelte --parser=svelte -t codemods/alias-to-relative-import.cjs src
# JS dryrun + print
npx jscodeshift -t codemods/alias-to-relative-import.cjs src -d -p
# JS
npx jscodeshift -t codemods/alias-to-relative-import.cjs src
# TS dryrun + print
npx jscodeshift --extensions=ts --parser=ts -t codemods/alias-to-relative-import.cjs src -d -p
# TS
npx jscodeshift --extensions=ts --parser=ts -t codemods/alias-to-relative-import.cjs src
*/
const path = require("path");
function replacePathAlias(currentFilePath, importPath, pathMap) {
// if windows env, convert backslashes to "/" first
currentFilePath = path.posix.join(...currentFilePath.split(path.sep));
const regex = createRegex(pathMap);
return importPath.replace(regex, replacer);
function replacer(_, alias, rest) {
const mappedImportPath = pathMap[alias] + rest;
// use path.posix to also create foward slashes on windows environment
let mappedImportPathRelative = path.posix.relative(
path.dirname(currentFilePath),
mappedImportPath
);
// append "./" to make it a relative import path
if (!mappedImportPathRelative.startsWith("../")) {
mappedImportPathRelative = `./${mappedImportPathRelative}`;
}
logReplace(currentFilePath, mappedImportPathRelative);
return mappedImportPathRelative;
}
}
function createRegex(pathMap) {
const mapKeysStr = Object.keys(pathMap).reduce((acc, cur) => `${acc}|${cur}`);
const regexStr = `^(${mapKeysStr})(.*)$`;
return new RegExp(regexStr, "g");
}
const log = true;
function logReplace(currentFilePath, mappedImportPathRelative) {
if (log)
console.log(
"current processed file:",
currentFilePath,
"; Mapped import path relative to current file:",
mappedImportPathRelative
);
}
/**
* Corresponds to tsconfig.json paths or webpack aliases
* E.g. "@/app/store/AppStore" -> "./src/app/store/AppStore"
*/
const pathMapping = {
//"@": "./src",
//"foo": "bar",
"$src": "./src",
};
module.exports = function transform(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
root.find(j.ImportDeclaration).forEach(replaceNodepathAliases);
root.find(j.ExportAllDeclaration).forEach(replaceNodepathAliases);
/**
* Filter out normal module exports, like export function foo(){ ...}
* Include export {a} from "mymodule" etc.
*/
root
.find(j.ExportNamedDeclaration, (node) => node.source !== null)
.forEach(replaceNodepathAliases);
return root.toSource();
function replaceNodepathAliases(impExpDeclNodePath) {
impExpDeclNodePath.value.source.value = replacePathAlias(
file.path,
impExpDeclNodePath.value.source.value,
pathMapping
);
}
}; |
@milahu I am facing the same issue |
@milahu this sounds like a bug in either Vite (e.g. this plugin) or |
@milahu @benmccann not sure it's relevant to this issue but I get the following warning from |
@umaranis can you file an issue in the sveltekit repo with instructions to reproduce it? I've never seen that warning. Not sure if I use a different package manager than you or just tuned it out or what |
This comment was marked as spam.
This comment was marked as spam.
fixed in the vite main branch this affects only development mode, |
@milahu I reverted that PR two days ago. Just making sure, did you clone the repo since then? Or alternatively, does it work with 4.0.0-beta.0? |
Here's details about the sourcemap issue in Vite that was attempted to be fixed and then reverted: vitejs/vite#7767 (comment) |
sorry, ninja edit vitejs/vite@4c85c0a fails which makes sense, because my code has const optimizedWallpapers = import.meta.glob('../../assets/wallpapers/*.{webp,jpg}', {
eager: true,
query: { width: 2000, quality: 95, format: 'webp' },
}) as Record<string, NodeModule>; |
Awesome! Glad it's fixed!! |
Describe the bug
When using typescript within .svelte files, the generated source map references the wrong line number, it is too low.
To Reproduce
src/App.svelte
like this:src/App.svelte
as TypeScript:App.svelte
:I've published a reproduction repo here: https://github.com/fd0/svelte-bug-typescript-sourcemap (including
package-lock.json
).Expected behavior
The error message should refer to the correct line in the source code (line 5 instead of line 3).
Information about your Svelte project:
Severity
It's very annoying, but does not block me (or anybody else). Debugging takes much longer.
Additional context
Thank you very much for adding TypeScript to Svelte, I love it!
The text was updated successfully, but these errors were encountered: