Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Only transform files that need to be transformed #26

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ function transformer(

let code = fs.readFileSync(filePath, 'utf8');

const hasImportOrExport = /^\s*(?:import|export)\s/gm;

if (filePath.endsWith('.cjs') && nodeSupportsImport) {
const transformed = transformDynamicImport(filePath, code);
if (transformed) {
code = applySourceMap(transformed, filePath);
}
} else {
} else if (
// Best guesses for files that need to be transformed.
!filePath.endsWith('.js') // not .js, so possibly ts, tsx, mjs, mts etc.
|| code.includes('import(') // dynamic import
|| hasImportOrExport.test(code) // any line starts with import or export
) {
const transformed = transformSync(
code,
filePath,
Expand All @@ -75,6 +82,8 @@ function transformer(
);

code = applySourceMap(transformed, filePath);
} else {
// Skipped, we expect it not to need to be transformed.
}

module._compile(code, filePath);
Expand Down