How to add subdirectory module aliases in tsconfig? - node.js

I can set module aliases with no issue:
"baseUrl": "./app",
"paths": {
"assets/*": ["assets/*"],
"components/*": ["components/*"],
...
},
This works. However, I (actually my client, who wants to extract some parts of the project as a module in the future) want to create subdirectories as aliases to different paths (assume that all the directories as values exist, and I cannot change the actual directory structure):
"paths": {
"assets/*": ["assets/*"],
"components/*": ["components/*"],
"#myapp/api/*": ["services/myapp/*"],
"#myapp/state/*": ["state/*"],
},
Putting aside whether this is a good pattern/practice or not (as I'm asked to do it this way), is this technically possible without touching the physical directory structure (and without the use of 3rd party dependencies)? (I'm using Typescript 3.4.3 and Vscode 1.33.1)

"baseUrl": "./",
"paths": {
"#myapp/environment": [ "./src/environments/environment" ],
"#myapp/extensions": [ "./src/extensions" ],
"#myapp/testing": [ "./src/testing" ],
"#myapp/constants": [ "./src/app/shared/constants" ],
"#myapp/data": [ "./src/app/data" ],
"#myapp/shared": [ "./src/app/shared" ],
"#myapp/core/*": [ "./src/app/core/*" ],
"#myapp/*": [ "./src/app/*" ]
}
this works like a charm on my project

Related

package.json export multiple search paths

I'm building some code in src to dist. I want any top-level files (dist/*.mjs) and any second-level files (dist/*/index.mjs) to be exports:
"exports": {
".": {
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./*": {
"import": "./dist/*.mjs",
"types": "./dist/*.d.ts"
},
"./*": {
"import": "./dist/*/index.mjs",
"types": "./dist/*/index.d.ts"
},
}
The last item for obvious reasons (can't have two identical keys in JSON) does not work correctly. I can get one or the other working, but I'd like it to try one and fallback to the next.
In desperation I tried using an array there but it didn't work. Is there a strategy I can use here that will make this feasible? The build-tooling I am currently stuck with (tsup) doesn't seem to be able to build src/foo.ts into src/foo/index.mjs and I'd rather not write a post-processor to move them around, but I will if I have to.

Use of alias paths inside a referenced project

Since TypeScript 3, it is possible to use projects as references within a given project.
I was trying to use such feature on two personal projects I have but it was not working. So I decided to use a given example as a starting point: https://github.com/appzuka/project-references-example
When I launch npm run build, all projects compile successfully. I then decided to modify the tsconfig.json file of package animals to include the support of aliases. The file was modified as such:
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": ".",
"baseUrl": ".",
"paths": { "#animals/*": [ "*" ] }
},
"references": [
{ "path": "../core" }
]
}
(baseUrl and paths properties were added inside compilerOptions)
I then modified the first import of dog.tsx file this way:
import { Animal, Size } from #animals/animal;
When I try to build again the main project, it fails. The error is:
ERROR in C:\Users\User\Documents\project-references-example\packages\animals\dog.tsx
[tsl] ERROR in C:\Users\User\Documents\project-references-example\packages\animals\dog.tsx(1,30)
TS2307: Cannot find module '#animals/animal' or its corresponding type declarations.
To make it working, I need to add the alias also on the main project, like this (in tsconfig.json):
"paths": {
"#animals/*": [ "packages/animals/*" ]
}
Is there a possibility to use alias in a referenced project without having to specify such alias in the parent project?
Thank you!

Node C++ Module Shared Library

I am trying to create a Node C++ module for the purpose of interfacing with the Steam api. The library file is ./steam/lib/linux64/libsteam_api.so, and header files are in ./steam.
I have created a small regular C++ file for testing, which successfully uses the Steam api, imported using #include "steam_api.h". I have complied and imported the shared library like this: g++ -L./steam/lib/linux64 -Wl,-rpath=./steam/lib/linux64 -Isteam -lsteam_api main.cpp
binding.gyp:
{
"targets": [ {
"target_name": "steam",
"sources": [ "steam.cpp" ],
"include_dirs": [
"steam",
"<!#(node -p \"require('node-addon-api').include\")"
],
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"libraries": [ "./steam/lib/linux64/libsteam_api.so" ]
} ]
}
When I try to compile the Node module using node-gyp, I get g++: error: ./steam/lib/linux64/libsteam_api.so: No such file or directory
How do I correctly import the shared library?
After looking through some examples and a lot of trial and error, I was able to correct binding.gpy:
{
"targets": [ {
"target_name": "steam",
"sources": [ "steam.cpp" ],
"include_dirs": [
"steam",
"<!#(node -p \"require('node-addon-api').include\")"
],
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"libraries": [
"-lsteam_api",
"-L../steam/lib/linux64",
"-Wl,-rpath=./steam/lib/linux64"
]
} ]
}
The libraries section needed to include the arguments similar to how they were invoked with g++, except "-L" differed from "-Wl,-rpath=" and the g++ inputs in needing to start one folder level up for some unknown reason.
It looks like node-gyp is changing the current directory as it runs, which invalidates your relative path. Either use an absolute path instead, or do some experimentation to find the new current directory and then use a path relative to that.

Adding more than one path to logstash forwarder

I have added more than one path to logstash forwarder config file. But only the first one works. It is not sending logs for the second path.
My config file.
"network": {
"servers": [ "x.x.x.x:5000" ],
"ssl certificate": "./logstash-forwarder.crt",
"ssl ca": "/etc/pki/tls/certs/logstash-forwarder.crt",
"timeout": 15
},
"files": [
{
"paths": [
"/opt/app1/application.log"
],
"fields": { "type": "app1" }
},
{
"paths":[
"/opt/app2/application.log"
],
"fields":{"type": "app2"}
}
]
}
I know I can add them together in the first path block and it will work, but I want to add two different types, which I guess can't be done in one path block.
This is resolved. Actually the issue is that my second log file is not updated for more than a day. So, logstash-forwarder skipped it.
2015/08/07 04:55:14.029097 Skipping file (older than dead time of 24h0m0s):

grunt-contrib-coffee source map only points to joined file

I'm using Grunt to compile my project, and I'm very happy with it. However, when I attempt to create source maps for my CoffeeScript-files, grunt-contrib-coffee generates a map to the joined file (the concatenated result of all my CoffeeScript files), not to my actual CoffeeScript sources, and I have been unable to find a reason for this.
How do I make the compiler generate a map of my actual CoffeeScript sources, instead of the joined file?
After CoffeeScript has compiled, I use grunt-contrib-uglify to generate the actually-used JS file, and its sourceMapIn-option is working like a charm. It's the CoffeeScript compiler that generates a weird source map.
Here's my grunt-contrib-coffee-configuration. The app-files contains an array of 24 CoffeeScript files.
{
"source": {
"options": {
"bare": true,
"sourceMap": true
},
"expand": true,
"join": true,
"cwd": ".",
"ext": ".js",
"files": {
"<%= build_dir %>/public.js": [ "<%= app_files.coffee %>" ]
}
}
}
Here's the resulted source map.
{
"version": 3,
"file": "public.js",
"sourceRoot": "",
"sources": [
"public.src.coffee"
],
"names": [],
"mappings": [ ... ]
}
As you see, the only source it points to is public.src.coffee, which is the file CoffeeScript joined my files into before compiling into CS.
Thanks for your time.

Resources