@@ -39,9 +39,10 @@ const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/
39
39
// use Acorn because it's slow. Luckily this doesn't have to be bullet proof
40
40
// since even missed imports can be caught at runtime, and false positives will
41
41
// simply be ignored.
42
- export const importsRE =
43
- // eslint-disable-next-line regexp/no-super-linear-move -- TODO: FIXME backtracking
44
- / (?< ! \/ \/ .* ) (?< = ^ | ; | \* \/ ) \s * i m p o r t (? ! \s + t y p e ) (?: [ \w * { } \n \r \t , ] + f r o m ) ? \s * ( " [ ^ " ] + " | ' [ ^ ' ] + ' ) \s * (? = $ | ; | \/ \/ | \/ \* ) / gm
42
+ // `[ \f\t\v\u00a0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]` is same with
43
+ // `[\r\n\u2028\u2029]` removed from `\s`
44
+ const importsRE =
45
+ / (?< = ^ | ; | \* \/ ) [ \f \t \v \u00a0 \u1680 \u180e \u2000 - \u200a \u202f \u205f \u3000 \ufeff ] * i m p o r t (? ! \s + t y p e ) (?: [ \w * { } \n \r \t , ] + f r o m ) ? \s * ( " [ ^ " ] + " | ' [ ^ ' ] + ' ) \s * (? = $ | ; | \/ \/ | \/ \* ) / gm
45
46
46
47
export async function scanImports ( config : ResolvedConfig ) : Promise < {
47
48
deps : Record < string , string >
@@ -325,7 +326,9 @@ function esbuildScanPlugin(
325
326
// since they may be used in the template
326
327
const contents =
327
328
content +
328
- ( loader . startsWith ( 'ts' ) ? extractImportPaths ( content ) : '' )
329
+ ( loader . startsWith ( 'ts' )
330
+ ? extractImportPaths ( content ) . join ( '\n' )
331
+ : '' )
329
332
330
333
const key = `${ path } ?id=${ scriptId ++ } `
331
334
if ( contents . includes ( 'import.meta.glob' ) ) {
@@ -524,22 +527,22 @@ function esbuildScanPlugin(
524
527
* the solution is to add `import 'x'` for every source to force
525
528
* esbuild to keep crawling due to potential side effects.
526
529
*/
527
- function extractImportPaths ( code : string ) {
530
+ export function extractImportPaths ( code : string ) : string [ ] {
528
531
// empty singleline & multiline comments to avoid matching comments
529
532
code = code
530
533
. replace ( multilineCommentsRE , '/* */' )
531
534
. replace ( singlelineCommentsRE , '' )
532
535
533
- let js = ''
536
+ const imports = [ ]
534
537
let m
535
538
while ( ( m = importsRE . exec ( code ) ) != null ) {
536
539
// This is necessary to avoid infinite loops with zero-width matches
537
540
if ( m . index === importsRE . lastIndex ) {
538
541
importsRE . lastIndex ++
539
542
}
540
- js += `\nimport ${ m [ 1 ] } `
543
+ imports . push ( `import ${ m [ 1 ] } `)
541
544
}
542
- return js
545
+ return imports
543
546
}
544
547
545
548
function shouldExternalizeDep ( resolvedId : string , rawId : string ) : boolean {
0 commit comments