Skip to content

Commit

Permalink
chore: format code with prettier (#212)
Browse files Browse the repository at this point in the history
see #211 

Ran `yarn prettier --write .`
  • Loading branch information
danielpza authored Aug 6, 2024
1 parent c7dce5e commit 0d9cd56
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
- name: Setup Node.js 18.x to publish to npmjs.org
uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
node-version: "18.x"
registry-url: "https://registry.npmjs.org"

- name: Install Packages
run: yarn install --frozen-lockfile
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/declarations/*
CHANGELOG.md
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ Add it to _plugins_ in your _tsconfig.json_
}
}
```

#### Example result

`core/index.ts`

```tsx
// The following transforms path to '../utils/sum'
import { sum } from "@utils/sum";
Expand All @@ -53,10 +55,10 @@ import { sum } from "@utils/sum";

- **Compile with `tsc`** — Use [ts-patch](https://github.com/nonara/ts-patch)

- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.

`tsconfig.json`

- **Use with ts-node** — Add `typescript-transform-paths/register` to `require` config.

`tsconfig.json`
```jsonc
{
"ts-node": {
Expand All @@ -72,6 +74,7 @@ import { sum } from "@utils/sum";
- **Use with NX** — Add the `typescript-transform-paths/nx-transformer` to project config

`project.json`

```jsonc
{
/* ... */
Expand All @@ -81,7 +84,7 @@ import { sum } from "@utils/sum";
"options": {
/* ... */
"transformers": [
{
{
"name": "typescript-transform-paths/nx-transformer",
"options": { "afterDeclarations": true }
}
Expand All @@ -93,6 +96,7 @@ import { sum } from "@utils/sum";
```

## Virtual Directories

TS allows defining
[virtual directories](https://www.typescriptlang.org/docs/handbook/module-resolution.html#virtual-directories-with-rootdirs)
via the `rootDirs` compiler option.
Expand All @@ -101,10 +105,10 @@ To enable virtual directory mapping, use the `useRootDirs` plugin option.
```jsonc
{
"compilerOptions": {
"rootDirs": [ "src", "generated" ],
"rootDirs": ["src", "generated"],
"baseUrl": ".",
"paths": {
"#root/*": [ "./src/*", "./generated/*" ]
"#root/*": ["./src/*", "./generated/*"]
},
"plugins": [
{ "transform": "typescript-transform-paths", "useRootDirs": true },
Expand All @@ -126,36 +130,40 @@ To enable virtual directory mapping, use the `useRootDirs` plugin option.
```

`src/file1.ts`

```ts
import '#root/file2.ts' // resolves to './file2'
import "#root/file2.ts"; // resolves to './file2'
```

`src/subdir/sub-file.ts`

```ts
import '#root/file2.ts' // resolves to '../file2'
import '#root/file1.ts' // resolves to '../file1'
import "#root/file2.ts"; // resolves to '../file2'
import "#root/file1.ts"; // resolves to '../file1'
```

## Custom Control

### Exclusion patterns

You can disable transformation for paths based on the resolved file path. The `exclude` option allows specifying glob
patterns to match against resolved file path.
patterns to match against resolved file path.

For an example context in which this would be useful, see [Issue #83](https://github.com/LeDDGroup/typescript-transform-paths/issues/83)

Example:

```jsonc
{
"compilerOptions": {
"paths": {
"sub-module1/*": [ "../../node_modules/sub-module1/*" ],
"sub-module2/*": [ "../../node_modules/sub-module2/*" ],
"sub-module1/*": ["../../node_modules/sub-module1/*"],
"sub-module2/*": ["../../node_modules/sub-module2/*"]
},
"plugins": [
{
"transform": "typescript-transform-paths",
"exclude": [ "**/node_modules/**" ]
{
"transform": "typescript-transform-paths",
"exclude": ["**/node_modules/**"]
}
]
}
Expand All @@ -164,7 +172,7 @@ Example:

```ts
// This path will not be transformed
import * as sm1 from 'sub-module1/index'
import * as sm1 from "sub-module1/index";
```

### @transform-path tag
Expand All @@ -173,7 +181,7 @@ Use the `@transform-path` tag to explicitly specify the output path for a single

```ts
// @transform-path https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js
import react from 'react' // Output path will be the url above
import react from "react"; // Output path will be the url above
```

### @no-transform-path
Expand All @@ -182,7 +190,7 @@ Use the `@no-transform-path` tag to explicitly disable transformation for a sing

```ts
// @no-transform-path
import 'normally-transformed' // This will remain 'normally-transformed', even though it has a different value in paths config
import "normally-transformed"; // This will remain 'normally-transformed', even though it has a different value in paths config
```

## Articles
Expand Down
24 changes: 12 additions & 12 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { JestConfigWithTsJest } from 'ts-jest/dist/types';
import { JestConfigWithTsJest } from "ts-jest/dist/types";

const config: JestConfigWithTsJest = {
testEnvironment: "node",
preset: 'ts-jest',
testRegex: '.*(test|spec)\\.tsx?$',
moduleFileExtensions: [ 'ts', 'tsx', 'js', 'jsx', 'json', 'node' ],
preset: "ts-jest",
testRegex: ".*(test|spec)\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: '<rootDir>/test/tsconfig.json'
}
]
tsconfig: "<rootDir>/test/tsconfig.json",
},
],
},
modulePaths: [ "<rootDir>" ],
roots: [ '<rootDir>' ],
}
modulePaths: ["<rootDir>"],
roots: ["<rootDir>"],
};

export default config;
4 changes: 2 additions & 2 deletions nx-transformer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict'
module.exports = require('./').nxTransformerPlugin;
"use strict";
module.exports = require("./").nxTransformerPlugin;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "jest",
"release": "standard-version",
"--------------": "",
"format": "prettier --write \"{src,test}/**/{*.js,!(*.d).ts}\"",
"format": "prettier --write .",
"clean": "npx -y rimraf -g dist **/*.tsbuildinfo ./test/projects/nx/dist",
"clean:all": "yarn run clean && npx -y rimraf -g node_modules **/node_modules **/yarn.lock yarn.lock",
"reset": "yarn run clean:all && yarn install && yarn build",
Expand Down
4 changes: 2 additions & 2 deletions register.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
let tsNode;
try {
tsNode = require('ts-node');
tsNode = require("ts-node");
} catch {
throw new Error(
`Cannot resolve ts-node. Make sure ts-node is installed before using typescript-transform-paths/register`
);
}

tsNode.register();
require('./').register();
require("./").register();
3 changes: 2 additions & 1 deletion src/utils/elide-import-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export function elideImportOrExportDeclaration(
isExportSpecifier,
} = tsInstance;

const isNamespaceExport = tsInstance.isNamespaceExport ?? ((node: Node): node is NamespaceExport => node.kind === SyntaxKind.NamespaceExport);
const isNamespaceExport =
tsInstance.isNamespaceExport ?? ((node: Node): node is NamespaceExport => node.kind === SyntaxKind.NamespaceExport);

if (tsInstance.isImportDeclaration(node)) {
// Do not elide a side-effect only import declaration.
Expand Down
6 changes: 1 addition & 5 deletions test/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ const tsp2 = require("tsp2");
* ****************************************************************************************************************** */

const rootDir = __dirname;
const tsDirs = [
"typescript-three",
"typescript-four-seven",
"typescript",
];
const tsDirs = ["typescript-three", "typescript-four-seven", "typescript"];

/* ****************************************************************************************************************** *
* Patch TS Modules
Expand Down
6 changes: 3 additions & 3 deletions test/projects/extras/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"include": [ "src" ],
"include": ["src"],

"ts-node": {
"require": [ "typescript-transform-paths/register" ]
"require": ["typescript-transform-paths/register"]
},

"compilerOptions": {
Expand All @@ -17,7 +17,7 @@

"baseUrl": "./src",
"paths": {
"#identifier": [ "./id.ts" ]
"#identifier": ["./id.ts"]
},

"plugins": [
Expand Down
4 changes: 2 additions & 2 deletions test/projects/general/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"target": "es5",
"module": "ESNext",
"outDir": "__built",
"moduleResolution" : "node",
"moduleResolution": "node",

"declaration": true,
"baseUrl": "./",
"paths": {
"path": ["https://external.url/path.js"],
"@*": ["*"],
"#utils/*": [ "./utils/*", "./secondary/*" ],
"#utils/*": ["./utils/*", "./secondary/*"],
"*": ["*"]
},

Expand Down
9 changes: 3 additions & 6 deletions test/projects/nx/packages/library1/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"targets": {
"build": {
"executor": "@nrwl/node:webpack",
"outputs": [
"{options.outputPath}"
],
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/library1",
"main": "packages/library1/src/index.ts",
Expand All @@ -23,9 +21,8 @@
}
]
},
"dependsOn": [
]
"dependsOn": []
}
},
"tags": [ ]
"tags": []
}
3 changes: 1 addition & 2 deletions test/projects/nx/packages/library2/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"root": "packages/library2",
"sourceRoot": "packages/library2/src",
"projectType": "library",
"targets": {
},
"targets": {},
"tags": []
}
2 changes: 1 addition & 1 deletion test/projects/project-ref/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"files": [ "index.ts" ],
"files": ["index.ts"],
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "../lib/a",
Expand Down
4 changes: 2 additions & 2 deletions test/projects/project-ref/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"files": [ "index.ts", "local/index.ts" ],
"files": ["index.ts", "local/index.ts"],
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "../lib/b",
"outDir": "../lib/b"
},
"references": [
{
Expand Down
6 changes: 3 additions & 3 deletions test/projects/project-ref/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

"baseUrl": ".",
"paths": {
"#a/*": [ "./a/*" ],
"#b/*": [ "./b/*" ]
"#a/*": ["./a/*"],
"#b/*": ["./b/*"]
},
"plugins": [
{
"transform": "../../../../src/index.ts",
"transform": "../../../../src/index.ts"
},
{
"transform": "../../../../src/index.ts",
Expand Down
14 changes: 7 additions & 7 deletions test/projects/specific/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": [ "src", "generated" ],
"include": ["src", "generated"],

"compilerOptions": {
"noEmit": true,
Expand All @@ -10,15 +10,15 @@
"moduleResolution": "node",
"declaration": true,

"rootDirs": [ "src", "generated" ],
"rootDirs": ["src", "generated"],

"baseUrl": ".",
"paths": {
"#root/*": [ "./src/*", "./generated/*" ],
"#exclusion/*": [ "./src/excluded/*" ],
"#elision": [ "./src/type-elision" ],
"#elision/*": [ "./src/type-elision/*" ],
"#packages/*": [ "./src/packages/*" ]
"#root/*": ["./src/*", "./generated/*"],
"#exclusion/*": ["./src/excluded/*"],
"#elision": ["./src/type-elision"],
"#elision/*": ["./src/type-elision/*"],
"#packages/*": ["./src/packages/*"]
},
"resolveJsonModule": true
}
Expand Down
Loading

0 comments on commit 0d9cd56

Please sign in to comment.