diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index bb9e5bd0..de70edf7 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -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 diff --git a/.prettierignore b/.prettierignore index d99c02fc..92310232 100755 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ src/declarations/* +CHANGELOG.md diff --git a/README.md b/README.md index 8a8f7503..60a7af6a 100644 --- a/README.md +++ b/README.md @@ -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"; @@ -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": { @@ -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 { /* ... */ @@ -81,7 +84,7 @@ import { sum } from "@utils/sum"; "options": { /* ... */ "transformers": [ - { + { "name": "typescript-transform-paths/nx-transformer", "options": { "afterDeclarations": true } } @@ -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. @@ -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 }, @@ -126,13 +130,16 @@ 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 @@ -140,22 +147,23 @@ import '#root/file1.ts' // resolves to '../file1' ### 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/**"] } ] } @@ -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 @@ -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 @@ -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 diff --git a/jest.config.ts b/jest.config.ts index 4a6af550..e4a448dc 100755 --- a/jest.config.ts +++ b/jest.config.ts @@ -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: '/test/tsconfig.json' - } - ] + tsconfig: "/test/tsconfig.json", + }, + ], }, - modulePaths: [ "" ], - roots: [ '' ], -} + modulePaths: [""], + roots: [""], +}; export default config; diff --git a/nx-transformer.js b/nx-transformer.js index 60d92d50..8be46333 100644 --- a/nx-transformer.js +++ b/nx-transformer.js @@ -1,2 +1,2 @@ -'use strict' -module.exports = require('./').nxTransformerPlugin; +"use strict"; +module.exports = require("./").nxTransformerPlugin; diff --git a/package.json b/package.json index 81f26eae..b8f73ac4 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/register.js b/register.js index 09242271..172f7ff9 100755 --- a/register.js +++ b/register.js @@ -1,6 +1,6 @@ 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` @@ -8,4 +8,4 @@ try { } tsNode.register(); -require('./').register(); +require("./").register(); diff --git a/src/utils/elide-import-export.ts b/src/utils/elide-import-export.ts index 3d82ef81..85f84b61 100755 --- a/src/utils/elide-import-export.ts +++ b/src/utils/elide-import-export.ts @@ -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. diff --git a/test/prepare.js b/test/prepare.js index e242745c..dcf0e089 100755 --- a/test/prepare.js +++ b/test/prepare.js @@ -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 diff --git a/test/projects/extras/tsconfig.json b/test/projects/extras/tsconfig.json index f41803b6..edd6eb8b 100755 --- a/test/projects/extras/tsconfig.json +++ b/test/projects/extras/tsconfig.json @@ -1,8 +1,8 @@ { - "include": [ "src" ], + "include": ["src"], "ts-node": { - "require": [ "typescript-transform-paths/register" ] + "require": ["typescript-transform-paths/register"] }, "compilerOptions": { @@ -17,7 +17,7 @@ "baseUrl": "./src", "paths": { - "#identifier": [ "./id.ts" ] + "#identifier": ["./id.ts"] }, "plugins": [ diff --git a/test/projects/general/tsconfig.json b/test/projects/general/tsconfig.json index 459a6d57..b47ad94b 100644 --- a/test/projects/general/tsconfig.json +++ b/test/projects/general/tsconfig.json @@ -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/*"], "*": ["*"] }, diff --git a/test/projects/nx/packages/library1/project.json b/test/projects/nx/packages/library1/project.json index cddfb00a..5c23404c 100644 --- a/test/projects/nx/packages/library1/project.json +++ b/test/projects/nx/packages/library1/project.json @@ -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", @@ -23,9 +21,8 @@ } ] }, - "dependsOn": [ - ] + "dependsOn": [] } }, - "tags": [ ] + "tags": [] } diff --git a/test/projects/nx/packages/library2/project.json b/test/projects/nx/packages/library2/project.json index 0adf5b7a..7ef4c0d7 100644 --- a/test/projects/nx/packages/library2/project.json +++ b/test/projects/nx/packages/library2/project.json @@ -2,7 +2,6 @@ "root": "packages/library2", "sourceRoot": "packages/library2/src", "projectType": "library", - "targets": { - }, + "targets": {}, "tags": [] } diff --git a/test/projects/project-ref/a/tsconfig.json b/test/projects/project-ref/a/tsconfig.json index e969bde3..7253501c 100755 --- a/test/projects/project-ref/a/tsconfig.json +++ b/test/projects/project-ref/a/tsconfig.json @@ -1,5 +1,5 @@ { - "files": [ "index.ts" ], + "files": ["index.ts"], "extends": "../tsconfig.base.json", "compilerOptions": { "outDir": "../lib/a", diff --git a/test/projects/project-ref/b/tsconfig.json b/test/projects/project-ref/b/tsconfig.json index 8d73f8ce..9a61c6d6 100755 --- a/test/projects/project-ref/b/tsconfig.json +++ b/test/projects/project-ref/b/tsconfig.json @@ -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": [ { diff --git a/test/projects/project-ref/tsconfig.base.json b/test/projects/project-ref/tsconfig.base.json index 487e7787..413d2f9e 100755 --- a/test/projects/project-ref/tsconfig.base.json +++ b/test/projects/project-ref/tsconfig.base.json @@ -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", diff --git a/test/projects/specific/tsconfig.json b/test/projects/specific/tsconfig.json index 4ea47cbb..7b43a93a 100755 --- a/test/projects/specific/tsconfig.json +++ b/test/projects/specific/tsconfig.json @@ -1,5 +1,5 @@ { - "include": [ "src", "generated" ], + "include": ["src", "generated"], "compilerOptions": { "noEmit": true, @@ -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 } diff --git a/test/tests/transformer/specific.test.ts b/test/tests/transformer/specific.test.ts index 320f045e..368d0fd2 100755 --- a/test/tests/transformer/specific.test.ts +++ b/test/tests/transformer/specific.test.ts @@ -187,27 +187,27 @@ describe(`Specific Tests`, () => { expect(typeElisionIndex).transformedMatches( /import { type TypeAndConst, ConstB as __ } from "\.\/a";\s*export { TypeAndConst, __ };/, - {kind: ["dts"]} + { kind: ["dts"] } ); /* Export Import type-only keyword on import specifier */ expect(typeElisionIndex).transformedMatches( /import { TypeAndConst as TypeAndConst2, ConstB as ___ } from "\.\/a";\s*export { type TypeAndConst2, ___ };/, - {kind: ["dts"]} + { kind: ["dts"] } ); expect(typeElisionIndex).transformedMatches( /import { TypeAndConst as TypeAndConst2, ConstB as ___ } from "\.\/a";\s*export { ___ };/, - {kind: ["js"]} + { kind: ["js"] } ); /* Unreferenced w/ type-only keyword on import specifier */ expect(typeElisionIndex).not.transformedMatches( /import { ConstB as ____, type TypeAndConst as TypeAndConst3 } from "\.\/a";\s/, - {kind: ["dts"]} + { kind: ["dts"] } ); - expect(typeElisionIndex).not.transformedMatches(/import { ConstB as ____ } from "\.\/a";\s/, {kind: ["js"]}); + expect(typeElisionIndex).not.transformedMatches(/import { ConstB as ____ } from "\.\/a";\s/, { kind: ["js"] }); } }); diff --git a/test/tsconfig.json b/test/tsconfig.json index 3453ff74..e40bf64a 100755 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,6 +1,6 @@ { "extends": "../tsconfig.base", - "include": [ "tests", "utils" ], + "include": ["tests", "utils"], "compilerOptions": { "noEmit": true, diff --git a/tsconfig.base.json b/tsconfig.base.json index be73040f..ce2a6a00 100755 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -4,7 +4,7 @@ "incremental": true, "declaration": false, - "lib": [ "esnext" ], + "lib": ["esnext"], "target": "es2015", "module": "CommonJS", "moduleResolution": "node", diff --git a/tsconfig.json b/tsconfig.json index 7fa99cd8..b38c8920 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.base", - "include": [ "src" ], - "exclude": [ "src/declarations" ], + "include": ["src"], + "exclude": ["src/declarations"], "compilerOptions": { "rootDir": "src", diff --git a/types/index.d.ts b/types/index.d.ts index 8a02ef01..86d8e786 100755 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,5 @@ -import TSNode from 'ts-node' -import ts from 'typescript' +import TSNode from "ts-node"; +import ts from "typescript"; /* ****************************************************************************************************************** * * Hardcoded declarations file for npm package @@ -10,7 +10,7 @@ export interface TsTransformPathsConfig { readonly exclude?: string[]; readonly afterDeclarations?: boolean; readonly tsConfig?: string; - readonly transform?: string + readonly transform?: string; } export interface TransformerExtras { @@ -20,7 +20,7 @@ export interface TransformerExtras { ts: typeof ts; } -export function register(): TSNode.RegisterOptions | undefined +export function register(): TSNode.RegisterOptions | undefined; export default function transformer( program?: ts.Program, @@ -33,8 +33,7 @@ export default function transformer( compilerOptions?: ts.CompilerOptions; fileNames?: string[]; } -): ts.CustomTransformerFactory - +): ts.CustomTransformerFactory; /* ****************************************************************************************************************** * * NX @@ -50,4 +49,4 @@ export interface NxTransformerPlugin { afterDeclarations: NxTransformerFactory; } -export const nxTransformerPlugin: NxTransformerPlugin +export const nxTransformerPlugin: NxTransformerPlugin;