Skip to content

Commit

Permalink
chore(deps): update dev dependencies
Browse files Browse the repository at this point in the history
also re-format with print width 80
  • Loading branch information
simonhaenisch committed Jun 21, 2023
1 parent a65dedd commit bd4937a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 28 deletions.
33 changes: 27 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { join } from 'path';
import { Plugin } from 'rollup';
import { CompilerOptions, findConfigFile, nodeModuleNameResolver, parseConfigFileTextToJson, sys } from 'typescript';
import {
CompilerOptions,
findConfigFile,
nodeModuleNameResolver,
parseConfigFileTextToJson,
sys,
} from 'typescript';

export const typescriptPaths = ({
absolute = true,
Expand All @@ -14,9 +20,15 @@ export const typescriptPaths = ({
return {
name: 'resolve-typescript-paths',
resolveId: (importee: string, importer?: string) => {
const enabled = Boolean(compilerOptions.paths || (compilerOptions.baseUrl && nonRelative));
const enabled = Boolean(
compilerOptions.paths || (compilerOptions.baseUrl && nonRelative),
);

if (typeof importer === 'undefined' || importee.startsWith('\0') || !enabled) {
if (
typeof importer === 'undefined' ||
importee.startsWith('\0') ||
!enabled
) {
return null;
}

Expand All @@ -34,7 +46,12 @@ export const typescriptPaths = ({
return null; // never resolve relative modules, only non-relative
}

const { resolvedModule } = nodeModuleNameResolver(importee, importer, compilerOptions, sys);
const { resolvedModule } = nodeModuleNameResolver(
importee,
importer,
compilerOptions,
sys,
);

if (!resolvedModule) {
return null;
Expand All @@ -48,10 +65,14 @@ export const typescriptPaths = ({

const targetFileName = join(
outDir,
preserveExtensions ? resolvedFileName : resolvedFileName.replace(/\.tsx?$/i, '.js'),
preserveExtensions
? resolvedFileName
: resolvedFileName.replace(/\.tsx?$/i, '.js'),
);

const resolved = absolute ? sys.resolvePath(targetFileName) : targetFileName;
const resolved = absolute
? sys.resolvePath(targetFileName)
: targetFileName;

return transform ? transform(resolved) : resolved;
},
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"test": "npm run prepare && node test",
"preversion": "npm test",
"prepare": "tsc"
"prepare": "rm -rf dist && tsc"
},
"author": "Simon Haenisch (https://github.com/simonhaenisch)",
"repository": "simonhaenisch/rollup-plugin-typescript-paths",
Expand All @@ -24,17 +24,17 @@
},
"homepage": "https://github.com/simonhaenisch/rollup-plugin-typescript-paths#readme",
"devDependencies": {
"@types/node": "18.6.5",
"prettier": "2.7.1",
"prettier-plugin-organize-imports": "3.0.3",
"rollup": "2.77.2",
"typescript": "4.7.4"
"@types/node": "18.16.18",
"prettier": "2.8.8",
"prettier-plugin-organize-imports": "3.2.2",
"rollup": "2.79.1",
"typescript": "5.1.3"
},
"peerDependencies": {
"typescript": ">=3.4"
},
"prettier": {
"printWidth": 120,
"printWidth": 80,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
Expand Down
74 changes: 59 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ const typescriptPaths = require('../dist').default;

const transform = (path) => path.replace(/\.js$/i, '.cjs.js');

const plugin = typescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json') });
const plugin = typescriptPaths({
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
});

const pluginNonAbs = typescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json'), absolute: false });
const pluginNonAbs = typescriptPaths({
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
absolute: false,
});

const pluginNonRelative = typescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json'), nonRelative: true });
const pluginNonRelative = typescriptPaths({
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
nonRelative: true,
});

const pluginTransform = typescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json'), transform });
const pluginTransform = typescriptPaths({
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
transform,
});

const pluginPreserveExtensions = typescriptPaths({
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
Expand All @@ -30,39 +41,72 @@ try {
strictEqual(plugin.resolveId('\0@foobar', ''), null);

// resolves with non-wildcard paths
strictEqual(plugin.resolveId('@foobar', ''), join(__dirname, 'foo', 'bar.js'));
strictEqual(plugin.resolveId('@foobar-react', ''), join(__dirname, 'foo', 'bar-react.js'));
strictEqual(
plugin.resolveId('@foobar', ''),
join(__dirname, 'foo', 'bar.js'),
);
strictEqual(
plugin.resolveId('@foobar-react', ''),
join(__dirname, 'foo', 'bar-react.js'),
);

// resolves with wildcard paths
strictEqual(plugin.resolveId('@bar/foo', ''), join(__dirname, 'bar', 'foo.js'));
strictEqual(
plugin.resolveId('@bar/foo', ''),
join(__dirname, 'bar', 'foo.js'),
);

// resolves from a directory with index file
strictEqual(plugin.resolveId('@js', ''), join(__dirname, 'js', 'index.js'));

// resolves without an `@` prefix
strictEqual(plugin.resolveId('bar/foo', ''), join(__dirname, 'bar', 'foo.js'));
strictEqual(
plugin.resolveId('bar/foo', ''),
join(__dirname, 'bar', 'foo.js'),
);

// resolves with a different importer
strictEqual(plugin.resolveId('bar/foo', join(__dirname, 'foo', 'bar.ts')), join(__dirname, 'bar', 'foo.js'));
strictEqual(
plugin.resolveId('bar/foo', join(__dirname, 'foo', 'bar.ts')),
join(__dirname, 'bar', 'foo.js'),
);

// doesn't accidentally resolve relative paths that also have an alias
strictEqual(plugin.resolveId('../bar/foo', join(__dirname, 'foo', 'bar.ts')), null);
strictEqual(
plugin.resolveId('../bar/foo', join(__dirname, 'foo', 'bar.ts')),
null,
);

// 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'));
strictEqual(
pluginNonRelative.resolveId('foo/bar', ''),
join(__dirname, 'foo', 'bar.js'),
);

// resolves as a relative path with option `absolute: false`
strictEqual(pluginNonAbs.resolveId('@foobar', ''), join('test', 'foo', 'bar.js'));
strictEqual(
pluginNonAbs.resolveId('@foobar', ''),
join('test', 'foo', 'bar.js'),
);

// applies function from `transform` option
strictEqual(pluginTransform.resolveId('@foobar', ''), join(__dirname, 'foo', 'bar.cjs.js'));
strictEqual(
pluginTransform.resolveId('@foobar', ''),
join(__dirname, 'foo', 'bar.cjs.js'),
);

// resolves including the file extension with option `preserveExtensions: true`
strictEqual(pluginPreserveExtensions.resolveId('@foobar', ''), join(__dirname, 'foo', 'bar.ts'));
strictEqual(pluginPreserveExtensions.resolveId('@foobar-react', ''), join(__dirname, 'foo', 'bar-react.tsx'));
strictEqual(
pluginPreserveExtensions.resolveId('@foobar', ''),
join(__dirname, 'foo', 'bar.ts'),
);
strictEqual(
pluginPreserveExtensions.resolveId('@foobar-react', ''),
join(__dirname, 'foo', 'bar-react.tsx'),
);

console.log('PASSED');
} catch (error) {
Expand Down

0 comments on commit bd4937a

Please sign in to comment.