-
Notifications
You must be signed in to change notification settings - Fork 66
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
Improve JS Sourcemaps #674
Conversation
🦋 Changeset detectedLatest commit: 86bd203 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
type HoistedScripts struct { | ||
Hoisted [][]byte | ||
Body []byte | ||
Hoisted [][]byte | ||
HoistedLocs []loc.Loc | ||
Body [][]byte | ||
BodyLocs []loc.Loc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously we were throwing away all location info when hoisting imports/exports, which was obviously unhelpful!
exports := make([][]byte, 0) | ||
exportLocs := make([]loc.Loc, 0) | ||
bodies := make([][]byte, 0) | ||
bodiesLocs := make([]loc.Loc, 0) | ||
|
||
if len(render.Body) > 0 { | ||
for i, innerBody := range render.Body { | ||
innerStart := render.BodyLocs[i].Start | ||
if len(bytes.TrimSpace(innerBody)) == 0 { | ||
continue | ||
} | ||
|
||
// Extract exports | ||
preprocessed := js_scanner.HoistExports(append(innerBody, '\n')) | ||
if len(preprocessed.Hoisted) > 0 { | ||
for j, exported := range preprocessed.Hoisted { | ||
exportedLoc := preprocessed.HoistedLocs[j] | ||
exportLocs = append(exportLocs, loc.Loc{Start: start + innerStart + exportedLoc.Start}) | ||
exports = append(exports, exported) | ||
} | ||
} | ||
|
||
if len(preprocessed.Body) > 0 { | ||
for j, body := range preprocessed.Body { | ||
bodyLoc := preprocessed.BodyLocs[j] | ||
bodiesLocs = append(bodiesLocs, loc.Loc{Start: start + innerStart + bodyLoc.Start}) | ||
bodies = append(bodies, body) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interleaving import hoisting with export hoisting, which gets kinda hard to follow, but the main point is that we're preserving the location of each statement.
if len(bytes.TrimSpace(body)) == 0 { | ||
continue | ||
} | ||
p.printTextWithSourcemap(string(body), bodyLoc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we can print these with intact location info!
* Add internal/js_scanner/js_scanner_test.go#FuzzHoistImport Runnable via `go test ./internal/js_scanner -fuzz=FuzzHoistImports` * Update internal/js_scanner/js_scanner_test.go Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * fix(js_scanner): assert that i < len(source) when scanning Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> Co-authored-by: Nate Moore <nate@astro.build>
woot 🥳 |
This reverts commit 20497f4.
Changes
Testing
Tested manually using https://evanw.github.io/source-map-visualization/
CleanShot.2022-12-19.at.14.08.16.mp4
Docs
N/A