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

@kadeluxe/ts-transform-paths

v1.7.19

Published

Ts transform paths

Downloads

46

Readme

Typescript transform paths plugin

Note: this is a fork with fixed compatibility for TypeScript 4.5+.

Heavily tested and most complete import/require path rewriter for typescript.

tsconfig baseUrl + paths alias rewriting in bundles and declaration files. You can use absolute paths in libraries. All them will be rewritted to relative in transpiled js and in d.ts files.

Works everywhere, no more tspath, rollup-plugin-alias or webpack resolve alias and other workarounds.

Why? Problem described here: d.ts files not working, if absolute paths used in npm-packaged library.

Similar libraries

ts-transform-import-path-rewrite does't support compilerOptions.paths config, doesn't support require function, weak tested.

ts-transformer-imports doesn't support dynamic import and require function. It doesn't support d.ts: "transform": "ts-transformer-imports", "afterDeclarations" true, "before": "true"} rewrites paths only in d.ts, "transform": "ts-transformer-imports", "afterDeclarations" true, "after": "true"} rewrites paths only in js.

External packages

Since 1.7.4 plugin rewrites paths only in internal project files, not for packages in node_modules.

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src"
    "plugins": [{ "transform": "@zerollup/ts-transform-paths" }],
    "paths": { "*": ["*"] },
  }
}

// main.ts
import { app } from "electron";  // a module in node_modules
import { setMenu } from "main/menu";  // a module in the current dir

// main.js
const electron_1 = require("electron");
const menu_1 = require("./main/menu");

Setup For ttypescript

ttypescript is a wrapper around typescript with transformer plugin support in tsconfig.json.

my-lib/tsconfig.json:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "my-lib/*": ["src/*"]
        },
        "plugins": [
            {
                "transform": "@zerollup/ts-transform-paths",
                "exclude": ["*"]
            }
        ]
    }
}

my-lib/src/index.ts

export * from 'my-lib/some'

my-lib/src/some.ts

export const some = '123'

Transpiled my-lib/dist/index.js

export * from './some'

Typings my-lib/dist/index.d.ts

export * from './some';

For more examples see zerollup demo lib.

Setup For rollup-plugin-typescript2

install:

$ npm i -D @zerollup/ts-transform-paths ttypescript

add to configure file rollup.config.js

import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'

export default {
    input: 'src/lib.ts',
    output: [{ file : 'dist/lib.js', name : 'mylib', format : 'iife', sourcemap : true }],
    plugins: [
        tsPlugin({
            typescript: ttypescript
        })
    ]
}

And setup tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "my-lib/*": ["src/*"]
        },
        "plugins": [
            {
                "transform": "@zerollup/ts-transform-paths",
                "exclude": ["*"]
            }
        ]
    }
}

Setup For webpack ts-loader

const tsTransformPaths = require('@zerollup/ts-transform-paths');

module.exports = {
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        loader: 'ts-loader',
        options: {
          getCustomTransformers: (program) => {
            const transformer = tsTransformPaths(program);
 
            return {
              before: [transformer.before], // for updating paths in generated code
              afterDeclarations: [transformer.afterDeclarations] // for updating paths in declaration files
            };
          }
        }
      }
    ]
  }
};

Plugin options

interface Config {
    /**
        Disable plugin path resolving for given paths keys
     */
    exclude?: string[] | void
}