justkey007 / tsc-alias

Replace alias paths with relative paths after typescript compilation
MIT License
876 stars 63 forks source link

Tsc-alias is resolving imports inside literal strings and not only import statements; #195

Closed Mauro-Domingues closed 8 months ago

Mauro-Domingues commented 1 year ago

I have a cli which helps to build fast apis, and inside of it some typescript templates, but is resolving those imports.

Steps to Reproduce:

  1. this is inside my tsconfig.json
    "tsc-alias": {
    "resolveFullPaths": true,
    "fileExtensions": {
      "inputGlob": "js",
      "outputCheck": ["js"]
    }
    }
  2. Create a ts file with an inport inside a string
    
    export class CreateDataSource {
    public execute(): string {
    return `import { DataSource } from 'typeorm';
    import 'dotenv/config';
    import 'reflect-metadata';

export const AppDataSource = new DataSource({ type: 'mysql', host: process.env.DB_HOST, port: Number(process.env.DB_PORT), username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.NODE_ENV === 'test' ? 'database_test' : process.env.DB_DATABASE, // synchronize: true, // logging: true, entities: [`\${dirname}/../../modules/*/entities/.{js,ts}`], // subscribers: [], migrations: [`\${dirname}/migrations/*.{js,ts}`], }); `; } }

3. run `tsc && tsc-alias`

4. You can also install the cli `npm install cross-api --save-dev` and check the files generated after the build.

Results:
```javascript
export class CreateDataSource {
    execute() {
        return `import { DataSource } from 'typeorm.js';
import 'dotenv/config';
import 'reflect-metadata';

export const AppDataSource = new DataSource({
  type: 'mysql',
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database:
    process.env.NODE_ENV === 'test' ? 'database_test' : process.env.DB_DATABASE,
  // synchronize: true,
  // logging: true,
  entities: [\`\${__dirname}/../../modules/**/entities/*.{js,ts}\`],
  // subscribers: [],
  migrations: [\`\${__dirname}/migrations/*.{js,ts}\`],
});
`;
    }
}
jearle commented 4 months ago

@Mauro-Domingues I'm still encountering this issue on version 1.8.8, where is the fix for this?

Mauro-Domingues commented 4 months ago

@Mauro-Domingues I'm still encountering this issue on version 1.8.8, where is the fix for this?

I closed my issue due to inactivity

You can avoid this bug by using ${'from'} if you're using templates like me

export class CreateDataSource {
  public execute(): string {
    return `import { DataSource } ${'from'} 'typeorm';
`;
  }
}

If it's node packages, I solved it by putting the node prefix node:

import { something } from 'crypto';

to:

import { something } from 'node:crypto';

As for the rest, unfortunately there is still no solution.

jearle commented 4 months ago

@Mauro-Domingues really appreciate the reply and this is a great workaround, can't believe I didn't think of it!