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..07a7926 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,20 @@ 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/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. 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..1318daf --- /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,