From 6012bf1886e6ecc4f8e612837e03af216cc47854 Mon Sep 17 00:00:00 2001 From: Pavol Caban Date: Wed, 5 Feb 2025 23:50:31 +0100 Subject: [PATCH] Fix Vite dependency extract (#3219) * Fix Vite dependency extract regex * Fix Vite Typescript compatibility --- resources/js/build/wordpress.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/resources/js/build/wordpress.js b/resources/js/build/wordpress.js index d428071..6ddb3bb 100644 --- a/resources/js/build/wordpress.js +++ b/resources/js/build/wordpress.js @@ -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