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

publish-flatten

v1.1.0

Published

Publish npm packages with flattened source directory

Downloads

220

Readme

NPM publish flatten

Publish npm packages with flattened source directory

Description

If you ever been annoyed by imports like the one below then this is the tool for you.

import styles from "project/dist/styles"

It does the following:

  1. Runs npm pack --dry-run to get a list of all files to publish
  2. Copies all files to temporary directory .npmPublishFlatten with flattened paths
  3. Updates package.json to correspond to the flattened structure
  4. Strips fields from package.json (optional)
  5. Publishes the content of the temporary directory to npm (optional)
  6. Removes temporary directory (optional)

Installation

npm install publish-flatten --save

Usage

node node_modules/publish-flatten --flatten dist
In below examples I'm going to omit the node_modules/ path for cleaner code.

Default npm publish

Published directory structure
$ npm publish
.
├── package.json
├── README.md
└──  dist
    ├── index.js
    ├── index.js.map
    └── sub
        ├── util.js
        └── util.js.map
Import code
import project from "project/dist"
import util from "project/dist/sub/util"

publish-flatten

Multiple directories and sub directories can be flattened.

Published directory structure
$ node publish-flatten --flatten dist --flatten dist/sub
.
├── package.json
├── README.md
├── index.js
├── index.js.map
├── util.js
└── util.js.map  
Import code
import project from "project"
import util from "project/util"

Strip fields from package.json

Sometimes you have information in your package.json that you don't want to publish. These can be stripped using the --strip argument.

Removes scripts and devDependencies from package.json before publishing.
node publish-flatten --strip scripts --strip devDependencies

CLI arguments

 --flatten      Directory to be flattened. Can be repeated for additional directories.
 --strip        Field in package.json to strip/remove. Can be repeated for additional fields.
 --keepResult   Keep temporary directory with the published files.

Additional arguments

All additional arguments are passed on to the underlying npm publish process. This means that you can still pass arguments to npm publish.

Parameter --dry-run is passed on to npm publish.
node publish-flatten --flatten dist --dry-run

Create npm script

An easy way to always get your build with correct flattened dirs is to create an script in package.json.

{
    "scripts": {
        "pub": "node node_modules/publish-flatten --flatten dist --strip scripts"
    }
}

This can then be called by: npm run pub
Or with additional arguments: npm run pub -- --dry-run