How to exclude files from exports in package.json in Node? - node.js

My util lib has the following exports in the package.json file
"exports":{
"./*": "./src/*.ts"
},
This works fine, however it exports all my test files. How can I exclude them?
The test files are next to the regular files, and have the extension .test.ts, e.g.:
/foo.ts
/foo.test.ts
https://nodejs.org/api/packages.html#package-entry-points

Related

transpile __dirname as a value

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.

Publish a TypeScript Project to NPM without source

Problem: I have built a library that have many .ts files in structured folders. Now I want to publish my this library, but do not want to share Source (typescript files).
Attempt: When I run tsc command it generates a .js file corresponding to each .ts file. When I keep declaration: true in my tsconfig.json file then tsc also generates .d.ts file for each .ts file as well.
Challenge: All of my .js files are generated structurally as expected, but .d.ts also are generating in parallel location of .js file. Now challenge is, by this way how any consumer of this library will import types as all of them are scattered in different folders, as there is no single .d.ts file generated by tsc
I see many module on NPM (like https://github.com/winstonjs/winston) which publishes single index.d.ts file, and consumer import them to use and avail Typescript advantage.
Currently my project structure looks like as below:
- my-project-root-directory
- src
- demo
- main.ts
- sample.ts
-lib
- constants
- some_constant_file1.ts
- models
- some_model_file1.ts
- some_model_file2.ts
- util
- some_util_file.ts
- dist
- demo
- main.js
- main.d.ts
- sample.js
- sample.d.ts
-lib
- constants
- some_constant_file1.js
- some_constant_file1.d.ts
- models
- some_model_file1.js
- some_model_file1.d.ts
- some_model_file2.js
- some_model_file2.d.ts
- util
- some_util_file.js
- some_util_file.d.ts
- logs
- package.json
- tsconfig.json
There is obviously no index.d.ts file, how can I get single file? Is single .d.ts file approach correct way? Or I am moving into wrong direction?
I've already seen many answers on stackoverflow & tutorials on youtube, all of them showing a simple example which have only single index.ts file and index.d.ts file corresponding to it. But I can not find a perfect way that solve my purpose.
Thanks for reading my question, any help is welcome.
Here's how I'd go along:
Create an index.ts file in each folder that exports types that are meaningful for library consumers. For example: src/lib/constants/index.ts, src/lib/models/index.ts, ... Each of the index.ts files exports all TypeScript types that exist in its folder, plus all index.ts files from its sub-folders.
Create a central src/index.ts file that exports everything from all src/... subfolders.
In your package.json, add a types: "dist/index.d.ts" entry.
In your .npmignore file, add a a src/ entry.
With this approach, you have all your imports referenced in one central dist/index.d.ts file, so consumers of your library do not need to import types from different locations inside your package. Also, you do not distribute TypeScript sources at all.
Here's how your directory structure could end up looking like. I excluded the dist folder, but an index.js and index.d.ts file will be generated in each folder, as well:
- my-project-root-directory
- src
- index.ts
- demo
- main.ts
- sample.ts
-lib
- constants
- some_constant_file1.ts
- index.ts
- models
- some_model_file1.ts
- some_model_file2.ts
- index.ts
- util
- some_util_file.ts
- index.ts
- logs
- package.json
- tsconfig.json
Your src/lib/constants/index.ts file could, for example, look like this:
export * from './some_constant_file1.ts'
Your src/lib/index.ts file:
export * from './constants';
export * from './models';
export * from './util';
Your src/index.ts file:
export * from './lib';

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