(file, pkg, subpath)
| 539 | } |
| 540 | |
| 541 | function rewriteMonopackageImports(file, pkg, subpath) { |
| 542 | if (path.extname(file) === '.mdx') { |
| 543 | rewriteMdx(file); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | let content = fs.readFileSync(file, 'utf8'); |
| 548 | let ast = recast.parse(content, { |
| 549 | parser: { |
| 550 | parse() { |
| 551 | return parse(content, { |
| 552 | sourceType: 'module', |
| 553 | plugins: ['typescript', 'jsx', 'importAttributes'], |
| 554 | sourceFilename: file, |
| 555 | tokens: true, |
| 556 | errorRecovery: true |
| 557 | }); |
| 558 | } |
| 559 | } |
| 560 | }); |
| 561 | |
| 562 | let hadImports = false; |
| 563 | ast.program.body = ast.program.body.flatMap(node => { |
| 564 | if ( |
| 565 | !( |
| 566 | (node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration') && |
| 567 | node.source |
| 568 | ) |
| 569 | ) { |
| 570 | return [node]; |
| 571 | } |
| 572 | |
| 573 | let source; |
| 574 | if (node.source) { |
| 575 | source = node.source.value; |
| 576 | // if (/\.\/?$/.test(source)) { |
| 577 | // if (!source.endsWith('/')) { |
| 578 | // source += '/'; |
| 579 | // } |
| 580 | // source += 'src/index'; |
| 581 | // } else if (source.endsWith('/src')) { |
| 582 | // source += '/index'; |
| 583 | // } |
| 584 | // if (source.endsWith('/src/index')) { |
| 585 | // let f = path.relative('.', path.resolve(path.dirname(file), source)); |
| 586 | // let [pkg] = parsePackage(f); |
| 587 | // console.log('HERE', f, pkg) |
| 588 | // if (importMap[pkg]) { |
| 589 | // source = pkg; |
| 590 | // } |
| 591 | // } |
| 592 | } |
| 593 | |
| 594 | if (importMap[source]) { |
| 595 | hadImports = true; |
| 596 | return getImportStatements(node, file); |
| 597 | } else if (source.startsWith('.') && pkg) { |
| 598 | hadImports = true; |
no test coverage detected