Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@zerollup/ts-transform-paths

zerkalica162kMIT1.7.18TypeScript support: included

Ts transform paths

typescript, transform, path, alias

readme

Typescript transform paths plugin

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
}

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.7.18 (2020-06-08)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.17 (2020-04-04)

Bug Fixes

  • ts-transform-paths: disable path rewrite in d.ts option: disableForDeclarations (e6d3de3)

1.7.16 (2020-04-04)

Bug Fixes

  • ts-transform-paths: use fileExists, remove getSourceFile #30 (0c97e90)

1.7.15 (2020-03-26)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.14 (2020-03-26)

Bug Fixes

  • ts-transform-paths: setup rollup with ttypescript (767576b)

1.7.13 (2020-03-23)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.12 (2020-03-01)

Bug Fixes

  • ts-transform-paths: check if import file exists without substitution, closes #25 (2c19d3a)

1.7.11 (2020-01-12)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.10 (2020-01-11)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.9 (2019-12-22)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.8 (2019-12-02)

Bug Fixes

  • ts-transform-paths: import with extension #21 (46f4933)

1.7.7 (2019-11-16)

Bug Fixes

  • ts-transform-paths: improve own import detector #18 (29eb2f7)

1.7.6 (2019-11-13)

Bug Fixes

1.7.5 (2019-11-10)

Bug Fixes

  • ts-transform-paths: readme, code optimisations (392f93b)
  • ts-transform-paths: restore resolver, closes #18 (679311f)

1.7.4 (2019-11-07)

Bug Fixes

1.7.3 (2019-05-31)

Bug Fixes

  • ts-transform-paths: moduleSpecifier null check #13 (50c033c)

1.7.2 (2019-05-31)

Note: Version bump only for package @zerollup/ts-transform-paths

1.7.1 (2019-04-09)

Bug Fixes

1.7.0 (2019-02-09)

Features

  • ts-transform-paths: added exclude and for options to config, #10 (5da42e1)

1.6.6 (2019-02-06)

Bug Fixes

  • ts-transform-paths: tests for resolve relative paths, #10 (a41585e)

1.6.5 (2018-12-01)

Note: Version bump only for package @zerollup/ts-transform-paths

1.5.1 (2018-08-29)

Note: Version bump only for package @zerollup/ts-transform-paths

1.5.0 (2018-08-29)

Note: Version bump only for package @zerollup/ts-transform-paths

1.4.5 (2018-06-26)

Bug Fixes

  • ts-transform-paths: processed map removed (7bf7d11)

1.4.4 (2018-06-26)

Bug Fixes

  • ts-transform-paths: d.ts generated dynamic imports (4a8410b)

1.4.3 (2018-06-22)

Bug Fixes

  • ts-transform-paths: regexp matching for paths suggestions, closes #3 (723dc77)
  • ts-transform-paths: removed compat with old ts (ddecdc5)

1.2.6 (2018-05-31)

Note: Version bump only for package @zerollup/ts-transform-paths

1.2.5 (2018-05-30)

Note: Version bump only for package @zerollup/ts-transform-paths

1.2.4 (2018-05-29)

Bug Fixes

  • ts-transform-paths: bad import/export in output js if importing/exporting pure interfaces (ea509a5)

1.2.3 (2018-05-29)

Bug Fixes

  • ts-transform-paths: interface import non-removal (31d294e)

1.2.2 (2018-05-26)

Bug Fixes

  • ts-transform-paths: node cloning (5ffaf95)

1.2.1 (2018-05-26)

Bug Fixes

  • ts-transform-plugin: commonjs module fix #1 (f5aee9a)

1.2.0 (2018-05-22)

Features

  • ts-transform-plugin: initial require support (1aa3762)

1.1.4 (2018-05-18)

Bug Fixes

  • ts-transform-paths: afterDeclarations name changed (f1f5e6a)

1.1.3 (2018-05-15)

Note: Version bump only for package @zerollup/ts-transform-paths