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

source-trace

v6.0.0

Published

Trace all of your dependencies for any type of JavaScript (and supersets - TS / Flow) source files.

Downloads

44

Readme

source-trace

Trace all of your dependencies for any type of JavaScript (and supersets - TS / Flow) source files.

  • Supports standard ESM, JS, TypeScript and JSX variants.
  • Supports Webpack loader-style paths: !loader1!loader2?query!./path/to/file.
  • Supports query strings: ./path/to/file?query.

Install

npm install source-trace

Usage

You can use the source-trace command to return a list of dependencies.

$ source-trace ./path/to/entry/point.js

Or in JS:

const sourceTrace = require('source-trace');

// [{ path: '/absolute/path/to/entry/point.js' }]
sourceTrace('./path/to/entry/point.js').then(console.log);

Returned data

The returned data doesn't just return a path. It also returns other information about the path. For example, if you traced a source that used loaders or query strings in the file path. If a path contained stuff like !loader!another-loader?query!path?another-query, you'd get:

{
  loaders: [{ path: 'loader', query: 'another-loader' }],
  originalPath: '!loader!another-loader?query!some/file.js?another-query',
  path: 'some/file.js',
  query: 'another-query',
  resolvedFrom: '/path/to/cwd',
  resolvedPath: '/path/to/cwd/some/file.js'
}

The purpose of parsing this extra information is, firstly, that it doesn't error if you're using paths that are valid within the JS ecosystem. Secondly, you are able to do whatever you want with that information. You may want to report what loaders are being used in your app, or even manually invoke the loaders using Webpack's loader-runner.

Options

The following options are supported.

extensions

An array of extensions to search for.

ignore

An array or function that will tell the tracer whether or not it should ignore the current file and all of its dependencies. By default, it looks at the closest package.json and will ignore it if the dependency to be traced is a module and not in the list of dependencies.

Use cases

Measuring bundle size

This is great for measuring your theoretical bundle size:

const bytes = require('bytes');
const fs = require('fs');
const sourceTrace = require('source-trace');

(async function () {
  const deps = await sourceTrace('./index.js');
  const stats = deps.map(d => fs.statSync(d.path).size);
  const sum = stats.reduce((prev, curr) => prev + curr, 0);
  console.log(bytes(sum));
}());

This doesn't measure any transforms that happen to the code that may make it larger, such as those that happen from Babel or Webpack. It simply operates on your source.