Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace CJS require declarations with ESM imports #95

Merged
merged 6 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ Watch this 5-minute video and learn how to migrate from CommonJS to ESM:

Here you can see the transformations that `ts2esm` applies.

### Require Statements

Before:

```ts
const fs = require('node:fs');
const path = require('path');
```

After:

```ts
import fs from 'node:fs';
import path from 'path';
```

### Import Declarations

Before:
Expand Down
12 changes: 12 additions & 0 deletions src/converter/convertFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ describe('convertFile', () => {

await expect(modifiedFile.getText()).toMatchFileSnapshot(snapshot);
});

it('turns CJS require statements into ESM imports', async () => {
const projectDir = path.join(fixtures, 'require-import');
const projectConfig = path.join(projectDir, 'tsconfig.json');
const snapshot = path.join(projectDir, 'src', 'main.snap.ts');
const project = ProjectUtil.getProject(projectConfig);

const sourceFile = project.getSourceFile('main.ts')!;
const modifiedFile = convertFile(projectConfig, sourceFile, true);

await expect(modifiedFile.getText()).toMatchFileSnapshot(snapshot);
});
});
19 changes: 17 additions & 2 deletions src/converter/convertFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SourceFile, SyntaxKind} from 'ts-morph';
import {rewrite} from '../main.js';
import {ProjectUtil} from '../util/ProjectUtil.js';
import {replaceRequire} from './replaceRequire.js';

export function convertFile(tsConfigFilePath: string, sourceFile: SourceFile, dryRun: boolean) {
const filePath = sourceFile.getFilePath();
Expand All @@ -10,6 +11,15 @@ export function convertFile(tsConfigFilePath: string, sourceFile: SourceFile, dr

let madeChanges: boolean = false;

// Update "require" variable assignments to "import" declarations
sourceFile.getVariableStatements().forEach(statement => {
const updatedRequire = replaceRequire(sourceFile, statement);
if (updatedRequire) {
madeChanges = true;
}
});

// Add explicit file extensions to imports
sourceFile.getImportDeclarations().forEach(importDeclaration => {
importDeclaration.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach(stringLiteral => {
const hasAttributesClause = !!importDeclaration.getAttributes();
Expand All @@ -20,10 +30,13 @@ export function convertFile(tsConfigFilePath: string, sourceFile: SourceFile, dr
sourceFilePath: sourceFile.getFilePath(),
stringLiteral,
});
madeChanges ||= adjustedImport;
if (adjustedImport) {
madeChanges = true;
}
});
});

// Add explicit file extensions to exports
sourceFile.getExportDeclarations().forEach(exportDeclaration => {
exportDeclaration.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach(stringLiteral => {
const hasAttributesClause = !!exportDeclaration.getAttributes();
Expand All @@ -34,7 +47,9 @@ export function convertFile(tsConfigFilePath: string, sourceFile: SourceFile, dr
sourceFilePath: filePath,
stringLiteral,
});
madeChanges ||= adjustedExport;
if (adjustedExport) {
madeChanges = true;
}
});
});

Expand Down
45 changes: 45 additions & 0 deletions src/converter/replaceRequire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {SourceFile, SyntaxKind, VariableStatement} from 'ts-morph';

export function replaceRequire(sourceFile: SourceFile, statement: VariableStatement) {
// Get variable declaration
const declaration = statement.getDeclarations()[0];
if (!declaration) {
return false;
}

// Get call expression from variable declaration
// @see https://github.com/dsherret/ts-morph/issues/682#issuecomment-520246214
const initializer = declaration.getInitializerIfKind(SyntaxKind.CallExpression);
if (!initializer) {
return false;
}

// Verify that we have a "require" call
const identifier = initializer.getExpression().asKind(SyntaxKind.Identifier);
if (identifier?.getText() !== 'require') {
return false;
}

// Extract the argument passed to "require" and use its value
const requireArguments = initializer.getArguments();
const packageName = requireArguments[0];
if (!packageName) {
return false;
}

// Narrowing the argument down to its literal value
const packageNameLiteral = packageName.asKind(SyntaxKind.StringLiteral);
if (!packageNameLiteral) {
return false;
}

// Add import declaration
sourceFile.addImportDeclaration({
defaultImport: declaration.getName(),
moduleSpecifier: packageNameLiteral.getLiteralValue(),
});

// Remove the original "require" statement
statement.remove();
return true;
}
4 changes: 4 additions & 0 deletions src/test/fixtures/require-import/src/main.snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import fs from "node:fs";
import path from "path";
fs.access('.', () => {});
console.log(path.basename);
5 changes: 5 additions & 0 deletions src/test/fixtures/require-import/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('node:fs');
const path = require('path');

fs.access('.', () => {});
console.log(path.basename);
10 changes: 10 additions & 0 deletions src/test/fixtures/require-import/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"module": "NodeNext",
"skipLibCheck": true,
"strict": true,
"target": "ES2020"
}
}