I am splitting typescript project into subprojects. And referencing them by file path. And it works fine. But when I try running mocha tests it fails. I use ts-node/require. The root of the problem is that project uses amd. When everything was under one project test script was overriding compiler options to commonjs. But now subproject is already compiled into js.
Asked
Active
Viewed 32 times
0
-
You might want to look into turning your sub-projects into npm packages, or otherwise look into npm workspaces. – Evert May 09 '21 at 06:25
-
Thanks @Evert, subprojects are npm packages. But because it is compiled, dist folder has js files in AMD format. And I dont know how to tell ts-node to use referenced npm packages ts files instead of js files. I will look at workspaces. – tsadigov May 09 '21 at 09:33
-
If everything is typescript and compiled, don't worry about using the `ts` files. You _want_ to use the js files. Your npm packages should have the definition files (.d.ts) alongside them. – Evert May 09 '21 at 16:52
-
I want to use same code for web and nodejs/testing. For the root project I dynamically change tsconfig parameter to make it generate js that is compatible with the target environment. But that does not change subprojecs(packages) js. – tsadigov May 10 '21 at 09:37
-
I don't fully understand the point of your last statement. Your root tsconfig.json should have no influences on npm packages. If it does (or if you want to) something is wrong. – Evert May 10 '21 at 19:08
-
I want to. Would not say that something is wrong. I want a way to compile/run everything in nodejs compatible mode for unit testing. I want everything to compile in browser compatible mode for deployment. I can compare this to selecting target platform in older IDEs. You can select x86, x64 or ARM as build target. x64 build of your application does not try to load ARM build of the library it depends on. – tsadigov May 12 '21 at 03:18
-
The normal workflow for this is that the top-level project has a tool like webpack that does this. Webpack typically also operates on the output .js files of dependencies, and you can control the target. – Evert May 12 '21 at 03:22
-
Not saying there's absolutely no way you can achieve this, but things tend to get easier when you follow common patterns. Dependencies, packages, transpilers, buildtools are are all a PITA to configure, but friction can go down if you do things the normal way. – Evert May 12 '21 at 03:35
-
Agree with you, going the way everybody else is using most frictionless. I need to change the project though. For now I decided the easiest would be to make grunt rebuild dependencies passing additional parameter to tsc to override the module format. npm run build -- -m AMD – tsadigov May 14 '21 at 01:47