Auto-publish mini-version for npm packages


(Aori Nevo) #1

Introduction

The idea is to have npm auto-publish a barebones npm package that includes the minified file of the corresponding npm package. Note that the idea is not to have npm do the minification - that would be provided by the project.

This idea grew out of the following issue https://github.com/reduxjs/redux/issues/3040 and corresponding solution https://github.com/reduxjs/redux/pull/3043.

Benefits

  • Quicker installation
  • Maintain consistency between full package semver and minified package semver

Notes

  • may want to publish the minified packages under a different scope (i.e. @npm/minified/{{package-name}})
  • have this work on an opt-in basis. For example, have a key in the package.json that specifies the location of the minified file. If it is there, auto-publish; otherwise, do nothing.

(Joshua) #2

Just FYI, that first link has a semicolon at the end, which leads to a Page not Found error. There could also be direct registry support for this, in some form or another. This would also be great fo npm getting better at front-end package management.


(Aori Nevo) #3

Thanks @legodude17! Updated the link.

I think it would be great if this was made available through npm. It would help standardize and ease the publishing process for projects that are already publishing, or plan on publishing, different versions to npm.

In the meantime, I’ve published weepub. It’s an npm module that helps publish the different versions of a library.


(Ben Gubler) #4

I think that would be a great idea. In some of my NPM Packages, I have files that are only used for developing my package.

Projects relying on my package really only need 1 JavaScript file.


(Joel Wallis Jucá) #5

I’m currently doing this exact kind of hybrid publishing on my scoped package only by using the pre-publish hook. I generate a global build (from the CommonJS source code) and minified version before publishing to npm. You might want to check if pre-publish can help you on it.


(Aori Nevo) #6

@Joel, can you share a link to your npm package? I’d like to take a look.


(Joel Wallis Jucá) #7

@aorinevo the package mentioned is here:

I have three build scripts in package.json: build, build:min and build:all. The first one, build, is the normal one that’s tailored to be used in browsers. The second one, build:min, builds a minified version of it. build:all is just a loop that runs all build scripts in sequence. Then, my prepublishOnly runs test and, if tests are passing, runs build:all.

If test fails the package does not get published, because the exit code is different than zero (since I ran npm test && npm build:all, the && operator sends the non-zero exit code of the first command, blocking the publication of the package.

This little hacky setup serves me well, allowing me to publish a CommonJS-compatible version of my package alongside a two browser targetted versions - a normal and a minified version, while uses test to ensure all tests are passing before publishing to npm.


(Aori Nevo) #8

So you’re still only publishing one package with two bundle types: commonjs (only.js) and minified (only.min.js).

The idea is to publish a separate package for each bundle type (min, umd, commonjs, etc…). In your case, I would like to see something like:

  • @joewallis/minified/only
  • @joewallis/only

where the first package contains a barebones npm package (i.e. package.json and main minified file) and the other contains the full package; both would have the same semver.

weepub already does this, but it would be great if this was provided by npm core.