Skip to content

All solutions have one problem: you must use relative imports #7

@Danielku15

Description

@Danielku15

I stumbled over this RFC researching solutions for the problem described in this repo: how to best use source-sharing within a mono repo to avoid a build step for cross references.

While most solutions sound promising there is a key problem to all of them: You must use relative path or imports for all imports within a package. If you use absolute paths or package specific ts-paths things fall apart.

Here a simplified example of what I mean:

packages
    lib1 
        src
            fun2.ts 
                // 1. import {fun1} from '@src/shared'; 
                // 2. import {fun1} from 'src/shared';
                // 3. import {fun1} from './shared';

            fun1.ts
                // 1. import { shared } from '@src/shared'
                // 2. import { shared } from 'src/shared'
                // 3. import { shared } from './shared'
            shared.ts
    lib2
        src
            file1.ts
                // import {fun2} from '@brand/lib1/fun2';

Depending on what paths you use for imports, the imports will fail at some point:

  • 1/1, 1/2, 1/3: import of '@brand/lib1/fun2' already fails because @src/ points to the lib2 sources
  • 2/1, 2/2, 2/3: import of '@brand/lib1/fun2' already fails because src/ points to the lib2 sources
  • 3/1: import of '@brand/lib1/fun2' works, but import of '@src/shared' fails because @src/ points to the lib2 sources.
  • 3/2: import of '@brand/lib1/fun2' works, but import of 'src/shared' fails because src/ points to the lib2 sources.
  • 3/3 import of '@brand/lib1/fun2' works and import of './shared'

I didn't test this through but I also expect things get tricky regarding automatic imports by the IDE.

I guess you either need to ensure relative paths or you use the full @brand/package syntax across the whole codebase.

Happy to hear any inputs how this challenge can be solved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions