Typescript, paths aliases with folder outside my project root
Asked Answered
P

0

12

I'm sorry for my bad english XD I hope to explain myself good.

I'm building a full-stack typescript project like this:

./
    package.json
    src/
        _shared/
            extensions/
                date-extension.ts
                object-extension.ts
                string-extension.ts
        restapi/
            dist/
                [...]
            src/
                [...]
            tsconfig.json
            package.json
            package-lock.json
        weapp/
            dist/
                [...]
            src/
                [...]
            tsconfig.json
            package.json
            package-lock.json

in this moment the folder ./src/_shared/extensions is present in both ./src/webapp (frontend) and ./src/restapi (backend), both projects are separate projects (so they have their own node_modules folder and package.json file) but both have the same extension methods for string, object and Date types.

Actually in restapi project my tsconfig looks like this:

{
    "compilerOptions": {
        "experimentalDecorators": false,
        "target": "es6",
        "module": "commonjs",
        "sourceMap": true,
        "outDir": "./dist",
        "strict": true,
        "esModuleInterop": false,
        "forceConsistentCasingInFileNames": true,
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,
        "baseUrl": "src",
        "paths": {
            "@src/*": [
                "./*"
            ],
            "@shared/*": [
                "../../_shared/*"
            ]
        }
    },
    "include": [
        "./src/**/*",
        "../../_shared/**/*"
    ],
    "exclude": [
        "./node_modules",
        "**/*.spec.ts"
    ]
}

[EDIT] but the compiler create a ./dist folder like this:

./dist
    _shared/
        extensions/
            [...]
    restapi/
        src/
            [...]

If I move ./src/_shared/extensions into ./src/restapi/src folder, the compiler create a ./dist folder like this:

./dist
   [...]

How can I reference ./src/_shared folder into my projects keeping the _shared folder outside my project root?

Thanks to all as always! :)

Pinkeye answered 17/7, 2020 at 9:32 Comment(5)
did you find a solution? I have exactly the same architectureSandon
Me too, any luck? @SandonObscenity
No luck. I have a similar question: #75928194Sandon
It is not a simple configuration, there's a lot to it, and the answer includes configuring non-TypeScript tools (i.e., bundler, test suite, etc.), which is, of course, different for each tool. Here's my attempt: github.com/parzhitsky/try-monorepo. It is a template of a client/server monorepo. Take a look at the (many) TypeScript config files, Vite and Vitest configs, and the logic of creating a Docker compose stack.Branks
at the end, I've removed path aliases and I used the classical paths.Pinkeye

© 2022 - 2024 — McMap. All rights reserved.