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

simple-import-replacement

v1.0.14

Published

A script to replace relative imports with absolute imports in TypeScript and JavaScript files.

Downloads

17

Readme

Simple Import Replacement

A script to replace relative imports with absolute imports in TypeScript, JavaScript, and CSS files.

Description

This script processes files in your current directory (or a specified one) and its subdirectories, converting relative import paths to absolute import paths. It supports ts, tsx, js, jsx, css, scss, less, and sass file extensions.

Installation

Local Installation

You can install the package locally as a development dependency:

npm install --save-dev simple-import-replacement

or using yarn:

yarn add --dev simple-import-replacement

Global Installation

You can install the package globally:

npm install -g simple-import-replacement

or using yarn:

yarn global add simple-import-replacement

Using npx

You can run the script directly using npx without installing it:

npx simple-import-replacement

Usage

Local Installation

Add a script to your package.json:

{
  "scripts": {
    "replace-imports": "simple-import-replacement"
  }
}

Run the script:

npm run replace-imports

or using yarn:

yarn replace-imports

Global Installation

Run the script directly:

replace-imports

Using npx

Run the script using npx:

npx simple-import-replacement

Additional Examples

Default behavior

Scan for **/*.{ts,tsx,js,jsx,css,scss,less,sass} files in the current directory and its subdirectories:

npx simple-import-replacement

Specify a root directory

npx simple-import-replacement --root-dir=/path/to/your/project/src

or

npx simple-import-replacement /path/to/your/project/src

Specify a root directory and custom file patterns

npx simple-import-replacement /path/to/your/project/src **/*.ts **/*.js **/*.css

Example

Suppose you have a project structure as follows:

/path/to/your/project/src/
├── com/
│   ├── test/
│   │   ├── foo/
│   │   │   ├── bar/
│   │   │   │   └── example.ts
│   │   │   └── another/
│   │   │       └── anotherThing.ts
│   └── components/
│       └── MyComponent.ts
│       └── styles.css

And your example.ts file contains the following imports:

import something from './something';
import anotherThing from '../another/anotherThing';
import React from 'react';
export { MyComponent } from '../../components/MyComponent';

And your styles.css file contains:

@import './variables.css';
@import '../components/common.css';

Running the script:

npx simple-import-replacement

will update example.ts to:

import something from 'com/test/foo/bar/something';
import anotherThing from 'com/test/foo/another/anotherThing';
import React from 'com/test/foo/bar/something';
export { MyComponent } from 'com/components/MyComponent';

and styles.css to:

@import 'com/test/foo/bar/variables.css';
@import 'com/components/common.css';

Supported File Types

The script now supports the following file types:

  • TypeScript (.ts, .tsx)
  • JavaScript (.js, .jsx)
  • CSS (.css)
  • SCSS (.scss)
  • Less (.less)
  • Sass (.sass)

It will process both import statements in TypeScript/JavaScript files and @import rules in CSS and other stylesheet files.

Contributing

Feel free to open issues or submit pull requests if you find bugs or have suggestions for improvements.

License

This project is licensed under the MIT License.