Fix Vite dependency extract (#3219)
* Fix Vite dependency extract regex * Fix Vite Typescript compatibility
This commit is contained in:
@@ -22,7 +22,10 @@ export function wordpressPlugin() {
|
|||||||
function extractNamedImports(imports) {
|
function extractNamedImports(imports) {
|
||||||
const match = imports.match(/{([^}]+)}/)
|
const match = imports.match(/{([^}]+)}/)
|
||||||
if (!match) return []
|
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) {
|
function handleNamedReplacement(namedImports, external) {
|
||||||
@@ -85,17 +88,19 @@ export function wordpressPlugin() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
transform(code, id) {
|
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 = [
|
const imports = [
|
||||||
...(code.match(/^import .+ from ['"]@wordpress\/[^'"]+['"]/gm) || []),
|
...(code.match(/^import[\s\n]+(?:[^;]+?)[\s\n]+from[\s\n]+['"]@wordpress\/[^'"]+['"]/gm) || []),
|
||||||
...(code.match(/^import ['"]@wordpress\/[^'"]+['"]/gm) || []),
|
...(code.match(/^import[\s\n]+['"]@wordpress\/[^'"]+['"]/gm) || []),
|
||||||
]
|
]
|
||||||
|
|
||||||
imports.forEach((statement) => {
|
imports.forEach((statement) => {
|
||||||
const match =
|
const match =
|
||||||
statement.match(/^import (.+) from ['"]@wordpress\/([^'"]+)['"]/) ||
|
statement
|
||||||
statement.match(/^import ['"]@wordpress\/([^'"]+)['"]/)
|
.replace(/[\s\n]+/g, ' ')
|
||||||
|
.match(/^import (.+) from ['"]@wordpress\/([^'"]+)['"]/) ||
|
||||||
|
statement.match(/^import ['"]@wordpress\/([^'"]+)['"]/);
|
||||||
|
|
||||||
if (!match) return
|
if (!match) return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user