justkey007 / tsc-alias

Replace alias paths with relative paths after typescript compilation
MIT License
902 stars 62 forks source link

Having a nested folder with the same name as the project root results in invalid paths #154

Closed patrickswijgman closed 1 year ago

patrickswijgman commented 1 year ago

Description

I am not entirely sure this is a bug but it is reproducible. It seems that when you have a nested folder with the same name as the project root it resolves to invalid paths.

FIle structure:

* components/ (project root)
    * src/
        * components/
            * atoms/
                * Button.tsx
        * types/
        * utils/
        * index.ts (contains the export of Button.tsx) 

tsconfig.build.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "declaration": true,
    "emitDeclarationOnly": true,
    "outDir": "dist"
  },
  "include": ["src/"],
  "exclude": ["src/stories/"]
}

tsconfig.json:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@e2e/*": ["e2e/*"],
      "@monorepo/*": ["../../*"]
    }
  },
  "include": ["src/", "tests/", "e2e/", "scripts/"]
}

Debug

Running tsc-alias -p tsconfig.build.json --debug give this output:

Output ``` tsc-alias debug: Loading config file: '/home/patrick/Projects/micro-frontends/packages/components/tsconfig.build.json' tsc-alias debug: configDir '/home/patrick/Projects/micro-frontends/packages/components' tsc-alias debug: loaded config (from file): { outDir: '/home/patrick/Projects/micro-frontends/packages/components/dist' } tsc-alias debug: Loading config file: '/home/patrick/Projects/micro-frontends/packages/components/tsconfig.json' tsc-alias debug: configDir '/home/patrick/Projects/micro-frontends/packages/components' tsc-alias debug: loaded config (from file): { baseUrl: '.', paths: { '@/*': [ 'src/*', [length]: 1 ], '@e2e/*': [ 'e2e/*', [length]: 1 ], '@monorepo/*': [ '../../*', [length]: 1 ] } } tsc-alias debug: Loading config file: '/home/patrick/Projects/micro-frontends/tsconfig.base.json' tsc-alias debug: configDir '/home/patrick/Projects/micro-frontends' tsc-alias debug: loaded config (from file): {} tsc-alias debug: loaded project config: { configFile: '/home/patrick/Projects/micro-frontends/packages/components/tsconfig.build.json', baseUrl: '.', outDir: '/home/patrick/Projects/micro-frontends/packages/components/dist', configDir: '/home/patrick/Projects/micro-frontends/packages/components', outPath: '/home/patrick/Projects/micro-frontends/packages/components/dist', confDirParentFolderName: 'components', hasExtraModule: false, configDirInOutPath: null, relConfDirPathInOutPath: null, pathCache: PathCache { fileExtensions: [ 'js', 'json', 'jsx', 'cjs', 'mjs', 'd.ts', 'd.tsx', 'd.cts', 'd.mts', [length]: 9 ], useCache: true, existsCache: Map(0) {}, absoluteCache: Map(0) {} }, inputGlob: '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}' } tsc-alias debug: loaded full config: { configFile: '/home/patrick/Projects/micro-frontends/packages/components/tsconfig.build.json', baseUrl: '.', outDir: '/home/patrick/Projects/micro-frontends/packages/components/dist', configDir: '/home/patrick/Projects/micro-frontends/packages/components', outPath: '/home/patrick/Projects/micro-frontends/packages/components/dist', confDirParentFolderName: 'components', hasExtraModule: false, configDirInOutPath: null, relConfDirPathInOutPath: null, pathCache: PathCache { fileExtensions: [ 'js', 'json', 'jsx', 'cjs', 'mjs', 'd.ts', 'd.tsx', 'd.cts', 'd.mts', [length]: 9 ], useCache: true, existsCache: Map(1) { '/home/patrick/Projects/micro-frontends/packages/components' => true }, absoluteCache: Map(0) {} }, inputGlob: '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}', output: Output { verb: false, debug: [Function (anonymous)] { [length]: 2, [name]: '' }, [verbose]: [Setter] }, aliasTrie: TrieNode { children: Map(1) { '@' => TrieNode { children: Map(3) { '/' => TrieNode { children: Map(0) {}, data: { shouldPrefixMatchWildly: true, prefix: '@/', paths: [ { path: 'src/', isExtra: false, basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' }, [length]: 1 ] } }, 'e' => TrieNode { children: Map(1) { '2' => TrieNode { children: Map(1) { 'e' => TrieNode { children: Map(1) { '/' => TrieNode { children: Map(0) {}, data: { shouldPrefixMatchWildly: true, prefix: '@e2e/', paths: [ { path: 'e2e/', isExtra: false, basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' }, [length]: 1 ] } } }, data: null } }, data: null } }, data: null }, 'm' => TrieNode { children: Map(1) { 'o' => TrieNode { children: Map(1) { 'n' => TrieNode { children: Map(1) { 'o' => TrieNode { children: Map(1) { 'r' => TrieNode { children: Map(1) { 'e' => TrieNode { children: Map(1) { 'p' => TrieNode { children: Map(1) { 'o' => TrieNode { children: Map(1) { '/' => TrieNode { children: Map(0) {}, data: { shouldPrefixMatchWildly: true, prefix: '@monorepo/', paths: [ { path: '../../', isExtra: false, basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' }, [length]: 1 ] } } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null } }, data: null }, replacers: [ [length]: 0 ] } tsc-alias debug: Started loading replacers tsc-alias debug: Found node_modules: [ '../../node_modules', [length]: 1 ] tsc-alias debug: Added replacers to list from command-line filepaths: tsc-alias debug: Reading replacers config tsc-alias debug: Loading default replacer: [ 'default', { enabled: true }, [length]: 2 ] tsc-alias debug: Replacer has no file: [ 'default', { enabled: true }, [length]: 2 ] tsc-alias debug: Loading default replacer: [ 'base-url', { enabled: true }, [length]: 2 ] tsc-alias debug: Replacer has no file: [ 'base-url', { enabled: true }, [length]: 2 ] tsc-alias debug: Loaded replacers: [ [Function: replaceImportStatement] { [length]: 1, [name]: 'replaceImportStatement', [prototype]: { [constructor]: [Circular *1] } }, [Function: replaceBaseUrlImport] { [length]: 1, [name]: 'replaceBaseUrlImport', [prototype]: { [constructor]: [Circular *2] } }, [length]: 2 ] tsc-alias debug: Search pattern: [ '/home/patrick/Projects/micro-frontends/packages/components/dist/**/*.{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}', '!/home/patrick/Projects/micro-frontends/packages/components/dist/**/node_modules', [length]: 2 ] tsc-alias debug: Found files: [ '/home/patrick/Projects/micro-frontends/packages/components/dist/index.d.ts', '/home/patrick/Projects/micro-frontends/packages/components/dist/index.js', '/home/patrick/Projects/micro-frontends/packages/components/dist/types/Window.d.ts', '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getClasses.d.ts', '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getComponentClasses.d.ts', '/home/patrick/Projects/micro-frontends/packages/components/dist/components/atoms/Button.d.ts', [length]: 6 ] tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/index.d.ts' tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/index.js' tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/types/Window.d.ts' tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getClasses.d.ts' tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getComponentClasses.d.ts' tsc-alias debug: Starting to replace file: '/home/patrick/Projects/micro-frontends/packages/components/dist/components/atoms/Button.d.ts' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/types/Window.d.ts' tsc-alias debug: default replacer - requiredModule: '@/types/Window' tsc-alias debug: default replacer - alias: { shouldPrefixMatchWildly: true, prefix: '@/', paths: [ { path: 'src/', isExtra: false, basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' }, [length]: 1 ] } tsc-alias debug: default replacer - absoluteAliasPath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' tsc-alias debug: default replacer - Invalid path tsc-alias debug: default replacer - requiredModule: '@/components/atoms/Button' tsc-alias debug: default replacer - alias: { shouldPrefixMatchWildly: true, prefix: '@/', paths: [ { path: 'src/', isExtra: false, basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' }, [length]: 1 ] } tsc-alias debug: default replacer - absoluteAliasPath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components' tsc-alias debug: default replacer - Invalid path tsc-alias debug: base-url replacer - requiredModule: '@/types/Window' tsc-alias debug: base-url replacer - requiredModule: '@/components/atoms/Button' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/index.d.ts' tsc-alias debug: default replacer - requiredModule: 'classnames' tsc-alias debug: default replacer - alias: tsc-alias debug: base-url replacer - requiredModule: 'classnames' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getClasses.d.ts' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/utils/getComponentClasses.d.ts' tsc-alias debug: default replacer - requiredModule: 'preact' tsc-alias debug: default replacer - alias: tsc-alias debug: default replacer - requiredModule: 'classnames' tsc-alias debug: default replacer - alias: tsc-alias debug: default replacer - requiredModule: 'tailwind-merge' tsc-alias debug: default replacer - alias: tsc-alias debug: default replacer - requiredModule: 'tailwind-tokens/dist/tailwind-components.json' tsc-alias debug: default replacer - alias: tsc-alias debug: base-url replacer - requiredModule: 'preact' tsc-alias debug: base-url replacer - requiredModule: 'classnames' tsc-alias debug: base-url replacer - requiredModule: 'tailwind-merge' tsc-alias debug: base-url replacer - requiredModule: 'tailwind-tokens/dist/tailwind-components.json' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/index.js' tsc-alias debug: default replacer - requiredModule: 'preact' tsc-alias debug: default replacer - alias: tsc-alias debug: base-url replacer - requiredModule: 'preact' tsc-alias debug: replaced file without changes: '/home/patrick/Projects/micro-frontends/packages/components/dist/components/atoms/Button.d.ts' ```

Findings

The basePath that the default replacer for the @/* alias makes is:

basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist/components'

while it should've been:

basePath: '/home/patrick/Projects/micro-frontends/packages/components/dist'

justkey007 commented 1 year ago

@patrickswijgman I would like you to provide me with a minimal repo that reproduces the problem. Thanks

Jokero commented 1 year ago

@justkey007 I encountered a similar bug, check please https://github.com/Jokero/tsc-alias-bug-demo. Update: I've created a separate issue https://github.com/justkey007/tsc-alias/issues/169

alvis commented 1 year ago

@justkey007 I have the same issue as @patrickswijgman I've a feeling it's related to path matching. But have to confirm it later