Fix Vite dependency extract (#3219)

* Fix Vite dependency extract regex

* Fix Vite Typescript compatibility
This commit is contained in:
Pavol Caban
2025-02-05 23:50:31 +01:00
committed by GitHub
parent 04b2404e47
commit 6012bf1886

View File

@@ -22,7 +22,10 @@ export function wordpressPlugin() {
function extractNamedImports(imports) {
const match = imports.match(/{([^}]+)}/)
if (!match) return []
return match[1].split(',').map((s) => s.trim())
return match[1]
.split(',')
.map((s) => s.trim())
.filter((s) => s !== '')
}
function handleNamedReplacement(namedImports, external) {
@@ -85,17 +88,19 @@ export function wordpressPlugin() {
}
},
transform(code, id) {
if ((!id.endsWith('.js')) && !id.endsWith('.jsx')) return
if ((!id.endsWith('.js')) && !id.endsWith('.jsx') && !id.endsWith('.ts') && !id.endsWith('.tsx')) return
const imports = [
...(code.match(/^import .+ from ['"]@wordpress\/[^'"]+['"]/gm) || []),
...(code.match(/^import ['"]@wordpress\/[^'"]+['"]/gm) || []),
...(code.match(/^import[\s\n]+(?:[^;]+?)[\s\n]+from[\s\n]+['"]@wordpress\/[^'"]+['"]/gm) || []),
...(code.match(/^import[\s\n]+['"]@wordpress\/[^'"]+['"]/gm) || []),
]
imports.forEach((statement) => {
const match =
statement.match(/^import (.+) from ['"]@wordpress\/([^'"]+)['"]/) ||
statement.match(/^import ['"]@wordpress\/([^'"]+)['"]/)
statement
.replace(/[\s\n]+/g, ' ')
.match(/^import (.+) from ['"]@wordpress\/([^'"]+)['"]/) ||
statement.match(/^import ['"]@wordpress\/([^'"]+)['"]/);
if (!match) return