transpile __dirname as a value - node.js

I want to get the directory of the source file, so I want to use __dirname in my .ts file as a value, not a variable.
in other words, I want this code to output the same value whatever the location of the output.
/src/index.ts
console.log(__dirname); // -> /src
when transpiling this file via tsc into a different location, it should log the new location.
but I want to log the same value each time
/dist/index.js
console.log(__dirname); // -> /dist
what I want is that when I open dist/index.js to see cosole.log("/src") instead of console.log(__dirname)
why I do that?
because sometimes you don't want to respect tsconfig.json, and output to a different path.
for instance using ts-node src/index.ts acts as if it outputs the compiled file into the same directory of the spurce file, so __dirname here gives the same path as the source file
another example using jest you need to transpile .ts files on the fly rather than compiling it into the dist folder then running the test.
you then need to refer to a path relatively to a fixe
d point, whatever the output file lives.

Related

Why does require() not require an absolute path but an express.static() does?

I am trying to run my index.js script from outside the project directory. My project structure is as follows:
app
- config
- config.js
- public
- index.html
- src
- index.js
Now when I run my src/index.js from outside my app folder, require() is able to resolve the relative paths
const config = require(`../config/config`);
On the other hand express.static is not able to resolve such relative paths.
e.g. app.use(express.static("../public"));
Why do I need to use path.join and get the absolute path?
require() works off __dirname which is independent of what the current directory was when your app was loaded. It's always the directory where the module is located in, so it is consistent.
express.static() when used with relative paths uses the directory that the main app was launched form, so if you use relative paths, its behavior varies depending upon how you launch the app.
From the express doc for serving static files:
However, the path that you provide to the express.static function is
relative to the directory from where you launch your node process. If
you run the express app from another directory, it’s safer to use the
absolute path of the directory that you want to serve
So, if you want the directory to be module relative, you have to manually combine your path with __dirname to make a full path, as you have discovered.

Typescript using `__dirname` to reference local file

My file structure looks as follows:
root
src
test.ts
template.xlsx
dist
// compiled js source ...
In test.ts, I try to reference the spreadsheet template as follows:
path.join(__dirname, "template.xlsx");
But it is searching in path/to/root/dist instead of looking in path/to/root/src. What's the best way to access my file?
If you want to access to /src from /dist you could navigate there like this:
path.join(__dirname + "../src", "template.xlsx");

Jest test to see if *.spec.tsx files exist

I have a TypeScript project that contains a bunch of files in different directories all located under src/.
Is it possible to write a Jest test so that it only returns success if each *.tsx file found under the parent directory (src/) has a corresponding *.spec.tsx test file?
In other words if I have the following 3 files:
src/index.tsx
src/foo.tsx
src/folder/component.tsx
The Jest test would fail until such time that the following files are created:
src/index.spec.tsx
src/foo.spec.tsx
src/folder/component.spec.tsx
I ended up using
import fs from 'fs';
and then using fs.readdir and fs.statSync to determine if a file is a file or a directory so that I can recursively look in every location from my src/ path.

Is there a way to use the folder name as default resolver (instead index.js) filename when import or require?

As in the node documentation:
If there is no package.json file present in the directory, then
Node.js will attempt to load an index.js or index.node file out of
that directory. For example, if there was no package.json file in the
above example, then require('./some-library') would attempt to load:
./some-library/index.js
./some-library/index.node
So when we give the directory without the filename it looks automatically to index.js and index.node. Is there a way to look first for the name of the folder for the file? For instance:
I have a module in "Afolder/" directory, with the name Afolder.js and I use:
import module from 'Afolder';
Here what I want is that node automatically looks first for Afolder.js and then for index.js and index.node .

Require.js r.js: Compile directory to single file

Is there any way to configure RequireJS to compile an entire directory to a single file? I don't mean the normal use case of the 'out' setting. I'll try to explain by example. If I have the following project structure:
- app
- main.js
- menu.js
- module
- file-a.js
- file-b.js
Then let's say I want to compile the 'app' directory to a single file. I don't care about it's dependencies - even if it requires 'module' or either of its files, they won't be included. Even if main.js doesn't require menu.js, it'll be included anyway. The resultant output file would define 'app/main' and 'app/menu' modules.
Likewise, if I wanted to compile the 'module' directory, the file would define 'module/file-a' and 'module/file-b' regardless of what other dependencies were defined.
I hope this is clear enough.
You can use the dir parameter in build file of require instead of just name parameter.
You can read more about building whole directory on requirejs documentation - Optimize Whole Project
If you write build file something like app-build.js-
({
appDir: ".",
baseUrl: "app",
dir: "../app-build",
})
and if you run r.js -o app.build.js then it will create
app-build
main.js
menu.js
Here menu.js will not be include in main.js unless it is required somewhere in main.js source.

Resources