Skip to content

Commit

Permalink
chore(): drop deprecated methods, upgrade tsconfig paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 26, 2022
1 parent 3ed9d05 commit 01c5acd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 66 deletions.
4 changes: 3 additions & 1 deletion lib/compiler/compiler.ts
Expand Up @@ -58,7 +58,9 @@ export class Compiler {
undefined,
undefined,
{
before: before.concat(tsconfigPathsPlugin),
before: tsconfigPathsPlugin
? before.concat(tsconfigPathsPlugin)
: before,
after,
afterDeclarations,
},
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/defaults/webpack-defaults.ts
Expand Up @@ -53,7 +53,7 @@ export const webpackDefaultsFactory = (
plugins: [
new TsconfigPathsPlugin({
configFile: tsConfigFile,
}) as any,
}),
],
},
mode: 'none',
Expand Down
37 changes: 30 additions & 7 deletions lib/compiler/hooks/tsconfig-paths.hook.ts
Expand Up @@ -11,6 +11,10 @@ export function tsconfigPathsBeforeHookFactory(
const { paths = {}, baseUrl = './' } = compilerOptions;
const matcher = tsPaths.createMatchPath(baseUrl!, paths, ['main']);

if (Object.keys(paths).length === 0) {
return undefined;
}

return (ctx: ts.TransformationContext): ts.Transformer<any> => {
return (sf: ts.SourceFile) => {
const visitNode = (node: ts.Node): ts.Node => {
Expand All @@ -19,27 +23,46 @@ export function tsconfigPathsBeforeHookFactory(
(tsBinary.isExportDeclaration(node) && node.moduleSpecifier)
) {
try {
const newNode = tsBinary.getMutableClone(node);
const importPathWithQuotes =
node.moduleSpecifier && node.moduleSpecifier.getText();

if (!importPathWithQuotes) {
return node;
}
const text = importPathWithQuotes.substr(
const text = importPathWithQuotes.substring(
1,
importPathWithQuotes.length - 2,
importPathWithQuotes.length - 1,
);
const result = getNotAliasedPath(sf, matcher, text);
if (!result) {
return node;
}
(newNode as any).moduleSpecifier = tsBinary.createLiteral(result);
(newNode as any).moduleSpecifier.parent = (
const moduleSpecifier =
tsBinary.factory.createStringLiteral(result);
(moduleSpecifier as any).parent = (
node as any
).moduleSpecifier.parent;
(newNode as any).flags = node.flags;
return newNode;

if (tsBinary.isImportDeclaration(node)) {
return tsBinary.factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
moduleSpecifier,
node.assertClause,
);
} else {
return tsBinary.factory.updateExportDeclaration(
node,
node.decorators,
node.modifiers,
node.isTypeOnly,
node.exportClause,
moduleSpecifier,
node.assertClause,
);
}
} catch {
return node;
}
Expand Down
91 changes: 37 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 01c5acd

Please sign in to comment.