npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-path-mapping

v0.1.4

Published

Adds the path aliases declared in tsconfig.json using module-alias package.

Downloads

97

Readme

ts-path-mapping

A npm package to automate path mapping in typescript modules, using only the tsconfig.json file. This package implements module-alias and comment-json for the tsconfig.json parsing, and now has ts-node support! (thanks for this file).

Disclaimer

This package is designed only for developing projects for the end user (such as websites, applications, etc). Using this package for resolve paths inside npm packages may cause serious problems. See this section for more details.

Changelog

  • --require ts-path-mapping/register added.
  • Dependencies updated.

Roadmap

  • [ ] Add a verbose mode to check in details the paths added.

How it works?

When you import (for side-effects) this library, this tries to read the tsconfig.json file for extracts the paths declared them. Later iterates all paths and register that one by one using module-alias.

Usage

First install the package:

npm i --save ts-path-mapping

Later, configure your tsconfig like this format (remember use "/*" at the end of every alias key and value):

{
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src",
    "baseUrl": "./src",
    "paths": {
        "@key-01/*": [ "folder-01/*" ],
        "@key-02/*": [ "folder-02/*" ],
        "@key-03/*": [ "folder-03/*" ],
    }
  }
}

Finally, when you start your project, simply execute it adding this library as a required dependency for node.js:

node --require ts-path-mapping/register ./dist/index.js

..or if do you want to use ts-node:

npx ts-node --require ts-path-mapping/register ./src/index.ts

How to detect

When this package is launched, the process object is altered adding some property with global symbols as key. The descriptions of that symbol keys are:

  • ts-path-mapping.registered: is true when this library was called using --require argument.
  • ts-path-mapping.required: is true when this library has been called.

If do you want to check the value of any key, you can write, for example:

const value = Object
    .getOwnPropertySymbols(process)
    .some(x => x.description === 'ts-path-mapping.registered');

What's the purpose to adding ts-path-mapping/register functionality?

In previous versions, before the ts-path-mapping/register implementation, you was must to import this library (in side-only effects mode) in the root file of your project. For execute your project wasn't a problem:

// THIS IS REQUIRED only in the root file
import 'ts-path-mapping';

// Import your resources...
import { jajaja } from '@key-01/jajaja';
import { gegege } from '@key-02/gegege';
import { jojojo } from '@key-03/jojojo';

// ...all use it as your needs
jajaja();
gegege();
jojojo();

...but if you want to make unit testing, that's is a problem because the unit testings doesn't starts necessarily from the root file. For example:

class-a.test.ts:

// A dependency imported using the path aliasing
import { memeParser } from '@tool/meme-parser';

describe('Testing "ClassA"', () => {
  // bla bla bla bla bla
});

class-b.test.ts:

// The same dependency imported before
import { memeParser } from '@tool/meme-parser';

describe('Testing "ClassB"', () => {
  // bla bla bla bla bla
});

When you execute Mocha (or another unit testing library), the execution will throw a module not found error, because this library wasn't imported in the first testing file loaded by the testing library. The temporally awful solution is import this library in every testing file (because we don't know what test will be executed first):

class-a.test.ts:

import 'ts-path-mapping';   // THIS WIILL BE IN ALL TESTING
import { memeParser } from '@tool/meme-parser';

describe('Testing "ClassA"', () => {
  // bla bla bla bla bla
});

class-b.test.ts:

import 'ts-path-mapping';   // THIS WIILL BE IN ALL TESTING
import { memeParser } from '@tool/meme-parser';

describe('Testing "ClassB"', () => {
  // bla bla bla bla bla
});

The solution of that is execute this library before of your project. For that reason is this library now has a ts-path-mapping/register functionality. If you still need the previous method (import for side-effects only), that option stills working.