2

I faced a problem :/ I want the shared codebase "shared" to be in both directories. Is this possible with typescript itself or do I need some extra build-tasks? Is there a better solution?

Important: The "shared" folder needs to be generated in srv and app, otherwise our core system would break.

I've got following src directory, where client contains a client.ts, server a server.ts and shared contains a index.ts. Now I want to use shared in client.ts and server.ts.

src
   - client
     - client.ts
     - tsconfig.json
   - server
     - server.ts
     - tsconfig.json
   - shared
     - index.ts
     - tsconfig.json

If I'm compiling my TypeScript, the following output should be generated.

app
   - client
     - client.js
   - shared
     - index.ts
srv
   - server
     - server.js
   - shared
     - index.ts

Here are my configs for each directory client/tsconfig.json

{
    "compileOnSave": true,
    "compilerOptions": {
        "target": "es2018",
        "module": "commonjs",
        "noImplicitAny": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outDir": "../../app",
        "sourceMap": true,
        "diagnostics": true,
    },
    "include": [
        "client.ts"
    ],
    "references": [
        { "path": "../shared"}
    ]
}

server/ts.config.json

{
    "compileOnSave": true,
    "compilerOptions": {
        "target": "es2018",
        "module": "commonjs",
        "noImplicitAny": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outDir": "../../srv",
        "sourceMap": true,
        "diagnostics": true,
    },
    "include": [
        "server.ts"
    ],
    "references": [
        {
            "path": "../shared"
        }
    ]
}

shared/tsconfig.json

{
    "compilerOptions": {
      "declaration": true, 
      "declarationMap": true,
      "composite": true,     
    },
    "include": [
      "*.*"
    ],
    "references": []
}

PS: If there is a better solution to solve this issue. Please let me know :)

Kind regards, Sebastian

2 Answers 2

1

You could try potentially turning the /shared folder into a separate npm package and then using npm link / npm install to join them together in each individual project? I did something similar with React and React Native using TypeScript.

Sign up to request clarification or add additional context in comments.

Comments

1

Typescript compilation process will not move or append files for you. It's just creating .js files for .ts files. With only tsc, there is no way to create bundles.

You should use a bundler like webpack Here is a link which might help: https://webpack.js.org/guides/typescript/


This being said, I think what you want to do is not correct. I assume your client is a single page application and server is a node server to serve those files? If so, you will run your server in node environment and serve client.js.

You can bundle client.js (with webpack or other cli tools) and serve it directly from server.js. Server.js won't need shared.js or client.js to be in the same directory unless you only copy server directory to somewhere.


EDIT: PS: There are monorepo approaches for such use cases in big scale projects. I'd suggest to take a look at 'yarn workspaces' and 'lerna'

4 Comments

My node server isnt serving client files, its seperated from each other. Thats why I need this "shared" directory in both directories.
I don't understand what you exactly mean by 'need directory'. If you have this structure and if you'd only run 'node-ts server/server.ts', it'll just work. Are you separating them when you are publishing/running?
Another HTTP Server is serving the client files and our server files will be run via nodejs.
Ok, clear. You need a bundler or symlinks for sure. A bundler would do the trick more cleaner and error proof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.