Skip to content

Commit

Permalink
Merge pull request #1554 from pkerschbaum/fix/replace-aliased-imports…
Browse files Browse the repository at this point in the history
…-of-tsx-files

fix: replace aliased imports for tsx and jsx files
  • Loading branch information
kamilmysliwiec committed Mar 3, 2022
2 parents 4fd0432 + 83b5474 commit bf6e41c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/compiler/hooks/tsconfig-paths.hook.ts
Expand Up @@ -54,7 +54,12 @@ function getNotAliasedPath(
matcher: tsPaths.MatchPath,
text: string,
) {
let result = matcher(text, undefined, undefined, ['.ts', '.js']);
let result = matcher(text, undefined, undefined, [
'.ts',
'.tsx',
'.js',
'.jsx',
]);
if (!result) {
return;
}
Expand Down
Expand Up @@ -21,3 +21,48 @@ Object.defineProperty(exports, \\"__esModule\\", { value: true });
",
}
`;

exports[`tsconfig paths hooks should replace path of every import using a path alias by its relative path 1`] = `
Map {
"dist/foo.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Foo = void 0;
class Foo {
}
exports.Foo = Foo;
",
"dist/bar.jsx" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Bar = void 0;
class Bar {
}
exports.Bar = Bar;
",
"dist/baz.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Baz = void 0;
class Baz {
}
exports.Baz = Baz;
",
"dist/qux.jsx" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Qux = void 0;
class Qux {
}
exports.Qux = Qux;
",
"dist/main.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
const foo_1 = require(\\"./foo\\");
const bar_1 = require(\\"./bar\\");
const baz_1 = require(\\"./baz\\");
const qux_1 = require(\\"./qux\\");
// use the imports so they do not get eliminated
console.log(foo_1.Foo);
console.log(bar_1.Bar);
console.log(baz_1.Baz);
console.log(qux_1.Qux);
",
}
`;
@@ -0,0 +1 @@
export class Bar {}
@@ -0,0 +1 @@
export class Baz {}
@@ -0,0 +1 @@
export class Foo {}
10 changes: 10 additions & 0 deletions test/lib/compiler/hooks/fixtures/aliased-imports/src/main.ts
@@ -0,0 +1,10 @@
import { Foo } from '~/foo';
import { Bar } from '~/bar';
import { Baz } from '~/baz';
import { Qux } from '~/qux';

// use the imports so they do not get eliminated
console.log(Foo);
console.log(Bar);
console.log(Baz);
console.log(Qux);
@@ -0,0 +1 @@
export class Qux {}
17 changes: 16 additions & 1 deletion test/lib/compiler/hooks/tsconfig-paths.hook.spec.ts
@@ -1,13 +1,19 @@
import * as path from 'path';
import * as ts from 'typescript';
import { JsxEmit } from 'typescript';
import { tsconfigPathsBeforeHookFactory } from '../../../../lib/compiler/hooks/tsconfig-paths.hook';

function createSpec(baseUrl: string, fileNames: string[]) {
function createSpec(
baseUrl: string,
fileNames: string[],
compilerOptions?: ts.CompilerOptions,
) {
const options: ts.CompilerOptions = {
baseUrl,
outDir: path.join(baseUrl, 'dist'),
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.CommonJS,
...compilerOptions,
};

const program = ts.createProgram({
Expand Down Expand Up @@ -52,4 +58,13 @@ describe('tsconfig paths hooks', () => {
);
expect(output).toMatchSnapshot();
});

it('should replace path of every import using a path alias by its relative path', async () => {
const output = createSpec(
path.join(__dirname, './fixtures/aliased-imports'),
['src/main.ts', 'src/foo.ts', 'src/bar.ts'],
{ paths: { '~/*': ['./src/*'] }, jsx: JsxEmit.Preserve, allowJs: true },
);
expect(output).toMatchSnapshot();
});
});

0 comments on commit bf6e41c

Please sign in to comment.