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: normalize sourcemap sources when generating from file name #10255

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/babel-generator/src/source-map.js
Expand Up @@ -7,8 +7,9 @@ import sourceMap from "source-map";
export default class SourceMap {
constructor(opts, code) {
this._cachedMap = null;
this._code = code;
this._opts = opts;
const { _opts, _code } = normalizeSourceFileName(opts, code);
this._code = _code;
this._opts = _opts;
this._rawMappings = [];
}

Expand Down Expand Up @@ -73,6 +74,9 @@ export default class SourceMap {
this._lastGenLine = generatedLine;
this._lastSourceLine = line;
this._lastSourceColumn = column;
if (filename) {
filename = filename.replace(/\\/g, "/");
}

// We are deliberately not using the `source-map` library here to allow
// callers to use these mappings without any overhead
Expand All @@ -94,3 +98,19 @@ export default class SourceMap {
});
}
}

function normalizeSourceFileName(opts, code) {
let _opts = opts;
const _code = code;
if (typeof code === "string") {
_opts = {
...opts,
sourceFileName: _opts.sourceFileName.replace(/\\/g, "/"),
};
} else if (typeof code === "object") {
Object.keys(code).forEach(sourceFileName => {
_code[sourceFileName.replace(/\\/g, "/")] = code[sourceFileName];
});
}
return { _opts, _code };
}