Skip to content

Commit

Permalink
fix: resolve exports (esm)
Browse files Browse the repository at this point in the history
  • Loading branch information
benyap committed Oct 22, 2021
1 parent ac8fed2 commit 89646f2
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 56 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ convenient solution._
_`resolve-tspaths` uses some reasonable defaults. For most cases, you probably
won't need to specify any options._

#### `--project, -p`
#### `--project <project>, -p <project>`

Specify the `tsconfig` that the program should use. If not provided, it defaults
to `tsconfig.json`.

#### `--src, -s`
#### `--src <path>, -s <path>`

Specify the source directory. If not provided, it defaults to `./src`.

#### `--out, -o`
#### `--out <path>, -o <path>`

Specify the output directory of the compiled code where `resolve-tspaths` should
perform its changes. If not provided, it will default to
`compilerOptions.outDir` from your `tsconfig`.

#### `--ext`
#### `--ext <extensions>`

Provide a comma separated list of file extensions in the output directory that
the program should process. Defaults to `js,d.ts`, which will process `.js` and
Expand Down
51 changes: 27 additions & 24 deletions src/steps/generateChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { dirname, relative, resolve } from "path";
import { FileNotFoundError } from "~/utils/errors";
import type { Alias, Change, ProgramPaths, TextChange } from "~/types";

export const IMPORT_REGEX =
/(?:require\(|import (?:.*from )?)['"]([^'"]*)['"]\)?/g;
export const IMPORT_EXPORT_REGEX =
/(?:require\(|(?:import|export) (?:.*from )?)['"]([^'"]*)['"]\)?/g;

const PATHS = [
".js",
Expand Down Expand Up @@ -73,28 +73,31 @@ export function replaceAliasPathsInFile(
const originalText = readFileSync(filePath, "utf-8");
const changes: TextChange[] = [];

const newText = originalText.replace(IMPORT_REGEX, (original, matched) => {
const result = aliasToRelativePath(
matched,
filePath,
aliases,
programPaths
);

if (!result.replacement) return original;

const index = original.indexOf(matched);
changes.push({
original: result.original,
modified: result.replacement,
});

return (
original.substring(0, index) +
result.replacement +
original.substring(index + matched.length)
);
});
const newText = originalText.replace(
IMPORT_EXPORT_REGEX,
(original, matched) => {
const result = aliasToRelativePath(
matched,
filePath,
aliases,
programPaths
);

if (!result.replacement) return original;

const index = original.indexOf(matched);
changes.push({
original: result.original,
modified: result.replacement,
});

return (
original.substring(0, index) +
result.replacement +
original.substring(index + matched.length)
);
}
);

return { changed: originalText !== newText, text: newText, changes };
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/change/out/exports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {} from "~/root";
export {} from "package";
export {} from "~/nested/non-existent";
export {} from "~/nested/nested-path";
export {} from "@/non-existent";
1 change: 1 addition & 0 deletions test/fixtures/change/out/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions test/fixtures/change/src/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {} from "~/root";
export {} from "package";
export {} from "~/nested/non-existent";
export {} from "~/nested/nested-path";
export {} from "@/non-existent";
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {} from "~/root";
import {} from "package";
import {} from "~/nested/non-existent";
import {} from "~/nested/path";
import {} from "~/nested/nested-path";
import {} from "@/non-existent";

// Module code
Expand Down
Loading

0 comments on commit 89646f2

Please sign in to comment.