Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"husky": "^9.1.7",
"jsdom": "24.0.0",
"lint-staged": "^15.5.0",
"magic-string": "^0.30.21",
"prettier": "^3.6.2",
"sinon": "^21.0.0",
"tap-diff": "^0.1.1",
Expand Down
37 changes: 23 additions & 14 deletions src/lib/precompiler/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import parser from '../templateparser/parser.js'
import generator from '../codegenerator/generator.js'
import MagicString from 'magic-string'

export default (source, filePath, mode) => {
if (
Expand All @@ -26,20 +27,18 @@ export default (source, filePath, mode) => {
/\{.*?template\s*:\s*(['"`])((?:\\?.)*?)\1.*?\}/s.test(source) // object with template key
) {
const templates = source.matchAll(/(?<!\/\/\s*)template\s*:\s*(['"`])((?:\\?.)*?)\1/gs)
let newSource = source
/*
if there are multiple templates in the file, we need to keep
track of the offset caused by the previous replacements
*/
let offset = 0

// Use MagicString to track changes
const s = new MagicString(source)

for (const template of templates) {
if (template[2]) {
const templateContent = template[2]

// Only process if it looks like a Blits template
if (templateContent.match(/^\s*(<!--[\s\S]*?-->|<[A-Za-z][^>]*>)/s)) {
const templateStartIndex = template.index + offset
// Use original indices - MagicString handles position tracking automatically
const templateStartIndex = template.index
const templateEndIndex = templateStartIndex + template[0].length

// Parse the template
Expand All @@ -58,16 +57,26 @@ export default (source, filePath, mode) => {
(fn) => fn.toString()
)}], context: {}}`

offset += replacement.length - template[0].length

newSource =
newSource.substring(0, templateStartIndex) +
replacement +
newSource.substring(templateEndIndex)
// MagicString tracks all changes automatically, no offset needed
s.overwrite(templateStartIndex, templateEndIndex, replacement)
}
}
}
return newSource

const newSource = s.toString()

if (newSource !== source) {
const fileName = filePath.split(/[\\/]/).pop()
return {
code: newSource,
map: s.generateMap({
hires: true,
source: fileName,
includeContent: true,
file: fileName,
}),
}
}
}
return source
}
7 changes: 6 additions & 1 deletion src/lib/reactivityguard/computedprops.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ export default (code) => {
modifiedCode = modifiedCode.replace(mod.original, mod.replacement)
}

return { code: modifiedCode }
// Returning code without a source map
// Vite will automatically chain this with the next plugin's source map (preCompiler)
return {
code: modifiedCode,
map: { mappings: '' },
}
}

return null
Expand Down
20 changes: 15 additions & 5 deletions vite/preCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ export default function () {

// we should only precompile .blits, .js and .ts files
if (
fileExtension === '.js' ||
fileExtension === '.ts' ||
fileExtension === '.blits' ||
fileExtension === '.mjs'
filePath.indexOf('node_modules') === -1 &&
(fileExtension === '.js' ||
fileExtension === '.ts' ||
fileExtension === '.blits' ||
fileExtension === '.mjs')
) {
return compiler(source, filePath, config.mode)
const result = compiler(source, filePath, config.mode)

if (typeof result === 'object') {
return {
code: result.code,
map: result.map,
}
}
// No transformation needed - return null so Vite knows no changes were made
return null
}

// vite expects null if there is no modification
Expand Down