Skip to content

Commit

Permalink
fix(ssrTransform): preserve line offset when transforming imports
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Dec 18, 2024
1 parent b48b98c commit 5f10b65
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,38 @@ async function ssrTransformScript(
}
const metadataStr = metadata ? `, ${JSON.stringify(metadata)}` : ''

// Track how many lines the original import statement spans, so we can preserve the line offset.
let linesSpanned = 1
for (let i = importNode.start; i < importNode.end; i++) {
if (code[i] === '\n') {
linesSpanned++
}
}

s.update(
importNode.start,
importNode.end,
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(
source,
)}${metadataStr});\n`,
)}${metadataStr});${'\n'.repeat(linesSpanned - 1)}`,
)

if (importNode.start === index) {
// no need to hoist, but update hoistIndex to keep the order
hoistIndex = importNode.end
} else {
// There will be an error if the module is called before it is imported,
// so the module import statement is hoisted to the top
// Check for non-whitespace characters between the last import and the
// current one.
const nonWhitespaceRegex = /\S/g
nonWhitespaceRegex.lastIndex = index
nonWhitespaceRegex.exec(code)

// TODO: Account for comments between imports.
if (importNode.start > nonWhitespaceRegex.lastIndex) {
// By moving the import to the top of the module, we ensure that it's
// imported before it's used.
s.move(importNode.start, importNode.end, index)
} else {
// Only update hoistIndex when not hoisting the current import. This
// ensures that once any import in this module has been hoisted, all
// remaining imports will also be hoisted.
hoistIndex = importNode.end
}

return importId
Expand Down

0 comments on commit 5f10b65

Please sign in to comment.