Excluding the 'src' folder from output during typescript compilation?
Asked Answered
S

1

6

When running tsc how do we exclude the src folder (Root containing folder) from output? I have the outDir set to target and instead of target/src/file.ts I would like target/file.ts?

Summit answered 12/7, 2018 at 22:34 Comment(13)
Do you have all your code in the src folder?Shandishandie
I'm assuming this question is related to your last one?Darmstadt
@PatrickRoberts it's related in the sense that I'm pursuing creating a clean dist or target directory to publish from...Would be a lot easier if there was a base package.json property though ... :)Summit
@SimeonSmith yes currently the code is in the src folder. I'm planning on building it all into a target folder and then copying package.json into that folder so that I can run npm publish from the target folder and have all the typescript modules be importable from there.Summit
So in other words I would like to avoid compiling and then having to do additional moving of resources in order to create a clean dist folder to run npm publish from.Summit
You should just be able to use tsc src to select everything in that folder and not output the src folder. What does your compile command look like right now?Shandishandie
It could be tsc src/*Shandishandie
Getting error TS6053: on the different attempts ...Summit
I though perhaps I could use the rootDir options in tsconfig.json, but when I use that typescript complains that it does not contain the test directory ...Summit
This is the target repository: github.com/fireflysemantics/validatorSummit
How about using the "include" option in your tsconfig? So something like {"compilerOptions": { ... }, "include": [ "./src/" ]}?Englut
You're going to want a different tsc command to compile your tests. You can't do both at the same time and have the src not compile to the same folder. You can chain the commands though.Shandishandie
Yeah I'm using ts-node to both run nyc code coverage and mocha tests at the same time. I have a follow up question about running the build using a separate tsconfig file here: #51317231Summit
S
5

Your probably looking for the rootDir tsconfig option. Specifically, in your case: rootDir: 'src/', supposing your keeping all source files under ./src/ and your tsconfig.json at the level above src/.

Note however, tsc may silently ignore this setting if your actually importing files that aren't under src/, say for example you import your package.json from top level.

Screw answered 19/2, 2023 at 9:23 Comment(3)
Do have any way to do exclude src if I want to import file outside rootDir except use config devtool: source-map in webpack.config.js.Unchaste
Yeah you can generally do it via a build tool like webpack, vite, rollup. I don't use webpack so can't help further than that. Another alternative is to get your build tool to inject a global variable instead on import if you just need something like the version from package.json.Screw
I really doesn't want to use build tool. It make build time more slow.Unchaste

© 2022 - 2024 — McMap. All rights reserved.