Typescript: updateImportDeclaration

Created on 17 May 2019  路  8Comments  路  Source: microsoft/TypeScript

TypeScript Version: 3.5.0-dev.20190517

Search Terms: updateImportDeclaration, createImportDeclaration

Code

// transformer.ts
function visit(node: ts.Node): ts.Node {
  if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
    const file = findFileInPaths(node.moduleSpecifier.text);  // transform specifier to filename according to baseUrl and paths compiller options.
    if (file) {
      return ts.updateImportDeclaration(
        node,
        node.decorators,
        node.modifiers,
        node.importClause,
        ts.createLiteral(file)
      );
    }
  }
}
//  tsconfig.json
{
  "compillerOptions": {
    "baseUrl": "./",
    "paths": { "@/*": [ "src/*" ] }
  }
}
// a.ts
import { B } from '@/b';
const b: B = 'blah';

// b.ts
export type B = 'blah' | 'vah';

Expected behavior: All imports to be updated accordingly to baseUrl and handled after. Especially when importClause elements used only as types, and not as values, import will be deleted from resulting js file.

// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var b = 'blah';

Actual behavior: All updated import declaration are go to resulting js file.

// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var b_1 = require("./b");
var b = 'blah';
Needs Investigation

Most helpful comment

By the way if I do this things start seems working correctly:

node.moduleSpecifier = (ts as any).updateNode(
  ts.createLiteral(file), node.moduleSpecifier
);
return node;

All 8 comments

Same for re-export declarations which exports only types.

export * from '@/b.ts';

This is just how typescript transpiled imports and exports that contain only types. That's not affected by the transform.

Such imports/exports are stripped from the generated .js file. They are only present in .d.ts files if you transpile with the declaration option.

Depending on how you pass the transform to the compiler those imports might already be stripped away when it's executed.

@ajafff It is affected by transform as it turns out, that's why I've created this issue. They should be stripped, but it looks like updateImportDeclaration invalidates some modules cache or something and this mechanism stops working.

Or there is somewhere link to the original node and it is not updated to the new one created by updateImportDeclaration.

By the way if I do this things start seems working correctly:

node.moduleSpecifier = (ts as any).updateNode(
  ts.createLiteral(file), node.moduleSpecifier
);
return node;

Did found piece of code responsible for this (probably). So it look like, I have to reproduce this behavior by my self.

Why this closed?

Agreed. Can we re-open this? While @anion155's workaround may be helpful, this behaviour doesn't seem like it should be standard.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

blendsdk picture blendsdk  路  3Comments

siddjain picture siddjain  路  3Comments