Skip to content

Add support for tsconfig without baseUrl parameter #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode/settings.json
.idea
node_modules
package-lock.json
dist
22 changes: 15 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path';
import { join, relative } from 'path';
import { Plugin } from 'rollup';
import {
CompilerOptions,
Expand Down Expand Up @@ -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 <proj_root>/test/<tsconfig>
* - __dirname -> using abs. path to compiled plugin files <proj_root>/dist
* - process.env.PWD -> non cross-platform
* instead of process.cwd() -> using abs. path to <proj_root>/<tsconfig>, that is correct
*/
const targetFileName = typeof compilerOptions.baseUrl === 'undefined'
? join(outDir, relative(process.cwd(), processedFileName))
: join(outDir, processedFileName);

const resolved = absolute
? sys.resolvePath(targetFileName)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 22 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions test/tsconfig-wo-baseurl.json
Original file line number Diff line number Diff line change
@@ -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/*"]
}
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"lib": ["esnext"],
"declaration": true,
"outDir": "dist",
"sourceMap": true,

"strict": true,
"noUnusedLocals": true,
Expand Down