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

@philidem/rewrite-relative-paths

v1.2.3

Published

Tool for making import paths relative to project root and making require paths relative

Downloads

1,196

Readme

Rewrite project paths

TLDR: This package helps you write code using imports with ~/src/some/directory/someFile instead of something like ../../../../src/some/directory/someFile.

Motivation

If you're a JavaScript developer then you have probably seen code similar to the following:

// inside src/some/directory/file1.ts
import { doSomething } from '../../../util';
// inside src/some/other/directory/file2.ts
import { doSomething } from '../../../../util';

Relative paths with .. are difficult to read and interpret and it causes inconsistency in import statement when importing the same file from different source files.

The typescript compiler can be configured to support paths that are relative to the project root.

For example, the examples above would become:

// inside src/some/directory/file1.ts
import { doSomething } from '~/src/util';
// inside src/some/other/directory/file2.ts
import { doSomething } from '~/src/util';

The tsconfig.json should be modified to have a paths property similar to the following:

{
  "compilerOptions": {
    "rootDir": ".",
    "baseUrl": ".",
    "outDir": "dist",
    "paths": {
      "~/*": ["./*"]
    }
  }
}

However, even when you configure typescript to understand these paths, the ~ will still appear in the output *.js files. To fix the output *.js files you will need to rewrite the require(...) statements so that the paths become relative again.

This project exposes two commands:

  • rewrite-imports: Rewrites source *.ts files so that paths with import statements using ../ are rewritten to use ~/. You can run this command in directory that contains *.ts so that ugly ../ paths will be rewritten and then can be committed to your source code repository.

  • rewrite-requires: Rewrites compiled *.js files so that paths in require statements are rewritten to always use relative paths. You can run this command after compiling typescript files to a target directory.

Installation

npm install -D @philidem/rewrite-relative-paths
yarn add --dev @philidem/rewrite-relative-paths

Usage

Rewrite import statements in source files to use paths relative to project root:

# Using yarn
yarn rewrite-imports --dir .

# Using npm
npm run rewrite-imports --dir .

Rewrite require statements in compiled *.js files to use relative paths:

# Using yarn
yarn rewrite-requires --dir ./dist

# Using npm
npm run rewrite-requires --dir ./dist