Giter Site home page Giter Site logo

tscpaths's Introduction

tscpaths

Replace absolute paths to relative paths after typescript compilation (tsc) during compile-time.

npm version Dependency Status License

Comparison to tsconfig-paths

+ Compile time (no runtime dependencies)

Getting Started

First, install tscpaths as devDependency using npm or yarn.

npm install --save-dev tscpaths
# or
yarn add -D tscpaths

Add it to your build scripts in package.json

"scripts": {
  "build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s ./src -o ./out",
}

Options

flag description
-p --project project configuration file (tsconfig.json)
-s --src source code root directory
-o --out output directory of transpiled code (tsc --outDir)

You need to provide -s (--src) and -o (--out), because it's hard to predict source and output paths based on tsconfig.json.

I've tried a little and failed. :(

tsc does some magic to determine source and output paths and I haven't dived too deep to mimic it.

For now, it's simpler to provide the paths manually.

If you know how, Pull Requests are welcome!

Disclaimer !!!!!

This is not a mature project yet.

It works for my setup so far.

It may not work correctly if your setup is too complicated, so please do some testing before pushing it to production!!!

tscpaths's People

Contributors

adam187 avatar joonhocho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tscpaths's Issues

Is the project abandoned?

Hi @joonhocho, cc: @jonkwheeler, @adam187,

I’m seeing that there are issues opened since 2018 and pull requests from 2019. Any chance that you have decided to no longer maintain this project anymore?

In that case, it would be good to state it in the README, I use this package in all my libraries and I would like to address some of the issues that it has. I would be also open to being a maintainer of the project or taking it over if you are no longer interested.

Regards

Improvement of the program

Hello! I cloned your job and I arranged it. You can visit it at https://github.com/justkey007/tsc-alias.
Currently, just take the typed configuration and without the other options. Excuse me I speak French which I used the machine translation but my message was poorly translated and I noticed too late.

It doesn't work when using the alias as the root of the import

require("alias/file") works fine, but if you have an index file under the alias and reference only require("alias"), it doesn't work. Imports to the root of an alias never gets replaced.

From what I see, this project has been abandoned, if anyone goes through the same problem, you need to replace the line 105 in this file for:

if (modulePath.startsWith(prefix) || modulePath === prefix.replace(/\/$/, '')) {

Breaks if tsconfig.json extends from a config defined in node_modules

My tsconfig.json contains an extends property, which targets a config that is a node_module dependency:

{
  "extends": "@org/ts-config",
  ...
}

however this breaks tscpaths:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '<redacted>/@org/ts-config'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.exports.loadConfig (<redacted>/node_modules/tscpaths/cjs/util.js:16:12)
    at Object.exports.loadConfig (<redacted>/node_modules/tscpaths/cjs/util.js:28:38)
    at Object.<anonymous> (<redacted>/node_modules/tscpaths/cjs/index.js:37:43)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)

it seems loadConfig() in utils is not able to correctly resolve non-relative/non-absolute extends clauses.

Not able to replace image assets

I've been running this over some image assets that are aliased and getting back "could not replace". When this line executes with a .ts file, it looks like the path is getting combined with the file name to get the full location. But when it executes with a .png file, only the image file name comes back.

const moduleSrc = resolve(apath, modulePathRel);

I wonder if something as simple as path + filename would suffice here, rather than using the resolve function.

moderate security severity on globby dependency

https://www.npmjs.com/advisories/1751

node_modules/glob-parent
fast-glob <=2.2.7
Depends on vulnerable versions of glob-parent
node_modules/tscpaths/node_modules/fast-glob
globby 8.0.0 - 9.2.0
Depends on vulnerable versions of fast-glob
node_modules/tscpaths/node_modules/globby
tscpaths *
Depends on vulnerable versions of globby
node_modules/tscpaths

please update the globby package to fix this issue, thanks!

replaces paths with .ts instead of .js

Not sure why this would be happening. It does it regardless of if I chain the tsc and tscpaths commands or not.

Output:
require("../shared/index.ts");

Expected:
require("../shared/index.js");

Input / Output source paths

I don't mind providing the input and output directory.

But I think the output should be directly reference compilerOptions.outDir found in the tsconfig.json description.

windows path replacement error

on the Windows operating system, path alias will be compiled to backslash, but it is good on the Mac.
below is my screenshots

  • windows:
    windows

  • Mac:
    mac

and my useage is:
rimraf dist && tsc --project tsconfig.json && tscpaths -p tsconfig.json -s ./src -o ./dist

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.