From 58e13d91594e525e21e31713448797cec001db15 Mon Sep 17 00:00:00 2001 From: unennhexium <50716292+unennhexium@users.noreply.github> Date: Sat, 24 Feb 2024 21:19:42 +0300 Subject: [PATCH 1/4] Support tsconfig without baseUrl property Related issue: https://github.com/simonhaenisch/rollup-plugin-typescript-paths/issues/18 --- .gitignore | 1 + index.ts | 21 ++++++++++++++------- package.json | 1 + test/index.js | 32 ++++++++++++++++++++++---------- test/tsconfig-wo-baseurl.json | 13 +++++++++++++ tsconfig.json | 1 + 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 test/tsconfig-wo-baseurl.json diff --git a/.gitignore b/.gitignore index fa3783f..e8a7a3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/settings.json +.idea node_modules package-lock.json dist diff --git a/index.ts b/index.ts index 7e47118..379b5e1 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import { join } from 'path'; +import { join, relative } from 'path'; import { Plugin } from 'rollup'; import { CompilerOptions, @@ -65,12 +65,19 @@ export const typescriptPaths = ({ return null; } - const targetFileName = join( - outDir, - preserveExtensions - ? resolvedFileName - : resolvedFileName.replace(/\.tsx?$/i, '.js'), - ); + const processedFileName = preserveExtensions + ? resolvedFileName + : resolvedFileName.replace(/\.tsx?$/i, '.js'); + + /* Do not use: + * - path.dirname(tsConfigPath) -> using abs. path to /test/ + * - __dirname -> using abs. path to compiled plugin files /dist + * - process.env.PWD -> non cross-platform + * instead of process.cwd() -> using abs. path to /, that is correct + */ + const targetFileName = typeof compilerOptions.baseUrl === 'undefined' + ? join(outDir, relative(process.cwd(), processedFileName)) + : join(outDir, processedFileName); const resolved = absolute ? sys.resolvePath(targetFileName) diff --git a/package.json b/package.json index 24962e9..7eba1e7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dist" ], "scripts": { + "test:wo-baseurl": "WO_BASEURL=true npm run test", "test": "npm run prepare && node test", "preversion": "npm test", "prepare": "rm -rf dist && tsc" diff --git a/test/index.js b/test/index.js index 95f7c86..a7de5b5 100644 --- a/test/index.js +++ b/test/index.js @@ -6,27 +6,31 @@ const typescriptPaths = require('../dist').default; const transform = (path) => path.replace(/\.js$/i, '.cjs.js'); +const tsConfigPath = process.env.WO_BASEURL + ? 'tsconfig-wo-baseurl.json' + : 'tsconfig.json' + const plugin = typescriptPaths({ - tsConfigPath: resolve(__dirname, 'tsconfig.json'), + tsConfigPath: resolve(__dirname, tsConfigPath), }); const pluginNonAbs = typescriptPaths({ - tsConfigPath: resolve(__dirname, 'tsconfig.json'), + tsConfigPath: resolve(__dirname, tsConfigPath), absolute: false, }); const pluginNonRelative = typescriptPaths({ - tsConfigPath: resolve(__dirname, 'tsconfig.json'), + tsConfigPath: resolve(__dirname, tsConfigPath), nonRelative: true, }); const pluginTransform = typescriptPaths({ - tsConfigPath: resolve(__dirname, 'tsconfig.json'), + tsConfigPath: resolve(__dirname, tsConfigPath), transform, }); const pluginPreserveExtensions = typescriptPaths({ - tsConfigPath: resolve(__dirname, 'tsconfig.json'), + tsConfigPath: resolve(__dirname, tsConfigPath), preserveExtensions: true, }); @@ -86,11 +90,19 @@ try { // skips non-relative paths unless enabled strictEqual(plugin.resolveId('foo/bar', ''), null); - // resolves non-relative from baseUrl even if no path is matched - strictEqual( - pluginNonRelative.resolveId('foo/bar', ''), - join(__dirname, 'foo', 'bar.js'), - ); + // resolves non-relative from baseUrl even if no path is matched, baseUrl is necessary in config + if (!process.env.WO_BASEURL) { + strictEqual( + pluginNonRelative.resolveId('foo/bar', ''), + join(__dirname, 'foo', 'bar.js'), + ); + } else { + console.log( + 'SKIP test forced nonRelative paths:\n', + '- Irrelevant since baseUrl is not provided in this case\n', + '- See WO_BASEURL environment variable in the test code' + ) + } // resolves as a relative path with option `absolute: false` strictEqual( diff --git a/test/tsconfig-wo-baseurl.json b/test/tsconfig-wo-baseurl.json new file mode 100644 index 0000000..3b1e2c2 --- /dev/null +++ b/test/tsconfig-wo-baseurl.json @@ -0,0 +1,13 @@ +{ + // comments should work + "compilerOptions": { + "paths": { + "@foobar": ["test/foo/bar"], + "@foobar-react": ["test/foo/bar-react"], + "@bar/*": ["test/bar/*"], + "bar/*": ["test/bar/*"], + "@js": ["test/js"], + "$/*": ["test/*"] + } + } +} diff --git a/tsconfig.json b/tsconfig.json index 01f619d..9a445e3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "lib": ["esnext"], "declaration": true, "outDir": "dist", + "sourceMap": true, "strict": true, "noUnusedLocals": true, From d415e7d33bec837fba91d696e8dba934feaead1a Mon Sep 17 00:00:00 2001 From: unennhexium <50716292+unennhexium@users.noreply.github> Date: Sat, 24 Feb 2024 21:30:24 +0300 Subject: [PATCH 2/4] Fix nonRealtive option description --- readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 782e88b..2137d6c 100644 --- a/readme.md +++ b/readme.md @@ -53,7 +53,8 @@ export default { ## Options - **`absolute`:** Whether to resolve to absolute paths; defaults to `true`. -- **`nonRelative`:** Whether to resolve [non-relative paths](https://www.typescriptlang.org/docs/handbook/module-resolution.html#relative-vs-non-relative-module-imports) based on tsconfig's `baseUrl`, even if none of the `paths` are matched; defaults to `false`. +- **`nonRelative`:** Whether to resolve [non-relative paths](https://www.typescriptlang.org/docs/handbook/module-resolution.html#relative-vs-non-relative-module-imports) based on tsconfig's `baseUrl`, even if none of the `paths` are matched; defaults to `false` + - The `baseUrl` parameter must be specified in _tsconfig.json_ - **`preserveExtensions`:** Whether to preserve `.ts` and `.tsx` file extensions instead of having them changed to `.js`; defaults to `false`. - **`tsConfigPath`:** Custom path to your `tsconfig.json`. Use this if the plugin can't seem to find the correct one by itself. - **`transform`:** If the plugin successfully resolves a path, this function allows you to hook into the process and transform that path before it is returned. From 67e0eb52a775fd3261efbf629ad4b20e6447ed0c Mon Sep 17 00:00:00 2001 From: unennhexium <50716292+unennhexium@users.noreply.github> Date: Sun, 25 Feb 2024 01:26:27 +0300 Subject: [PATCH 3/4] Add leading ./ to elimitate error TS5090: Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'? (cherry picked from commit 1c9a970b8d859cebdc599b29df79d8dd1eda7b92) --- test/tsconfig-wo-baseurl.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/tsconfig-wo-baseurl.json b/test/tsconfig-wo-baseurl.json index 3b1e2c2..1318daf 100644 --- a/test/tsconfig-wo-baseurl.json +++ b/test/tsconfig-wo-baseurl.json @@ -2,12 +2,12 @@ // comments should work "compilerOptions": { "paths": { - "@foobar": ["test/foo/bar"], - "@foobar-react": ["test/foo/bar-react"], - "@bar/*": ["test/bar/*"], - "bar/*": ["test/bar/*"], - "@js": ["test/js"], - "$/*": ["test/*"] + "@foobar": ["./test/foo/bar"], + "@foobar-react": ["./test/foo/bar-react"], + "@bar/*": ["./test/bar/*"], + "bar/*": ["./test/bar/*"], + "@js": ["./test/js"], + "$/*": ["./test/*"] } } } From b4e79cd0bcb7e7a2dce53b8712e15afe1f85e38c Mon Sep 17 00:00:00 2001 From: unennhexium <50716292+unennhexium@users.noreply.github> Date: Sun, 25 Feb 2024 02:03:09 +0300 Subject: [PATCH 4/4] Fix spaces in comment --- index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 379b5e1..07a7926 100644 --- a/index.ts +++ b/index.ts @@ -69,11 +69,12 @@ export const typescriptPaths = ({ ? resolvedFileName : resolvedFileName.replace(/\.tsx?$/i, '.js'); - /* Do not use: + /* + * Do not use: * - path.dirname(tsConfigPath) -> using abs. path to /test/ - * - __dirname -> using abs. path to compiled plugin files /dist - * - process.env.PWD -> non cross-platform - * instead of process.cwd() -> using abs. path to /, that is correct + * - __dirname -> using abs. path to compiled plugin files /dist + * - process.env.PWD -> non cross-platform + * instead of process.cwd() -> using abs. path to /, that is correct */ const targetFileName = typeof compilerOptions.baseUrl === 'undefined' ? join(outDir, relative(process.cwd(), processedFileName))