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

envarconst

v0.0.1

Published

Replace const declarations with custom values, so an minifier like uglifyjs can remove dead code

Downloads

1

Readme

envarconst

Make JavaScript const variable.

Build Status

The Idea

UglifyJS and probably other minifiers remove dead code, when a const has a value, which is used in an if statement elsewhere. For example:

const ES5 = true;
if (ES5){
	console.log("I have ES5 features");
} else {
	console.log("I'm an old browser");
}

minifies to simply:

console.log("I have ES5 features");

Now we can use this, to make, say, a mobile version, with mobile specific code, or special code for development, with DEV code.

However, eventually, you want to deploy to production, or to another environment target, so the const values have to change, from const DEV = true, to const DEV = false, so all if (DEV){ ... } code is removed.

cat es5.js | envarconst -d ES5=false | uglifyjs -m -c

results in:

console.log("I'm an old browser");

envarconst helps you to do this quickly!

How To Use It

installation

npm install envarconst -g

cli

envarconst comes with an cli tool which you can use. It either reads from an file, or from stdin. When using stdin you can use Unix pipes in your build process.

envarconst -d DEV=false -d MOBILE=true ./file.js

Using Unix pipes with wrapup and UglifyJS:

wrup -r ./mymodule | envarconst -d DEV=false | uglifyjs -m -c -o mymodule.min.js

JavaScript

The JS API is very simple, simply require the module, and call the function with the JS code and the new constants.

var envarconst = require('envarconst');
envarconst("const FOO = false", {
	FOO: "true"
}); // "const FOO = true"

Why Not UglifyJS --define

UglifyJS doesn't replace variables or constants. So those variables defined by --define are only later specified. This causes problems when you just want to use if (DEV){ ... }, instead you have to do an extra typeof DEV != 'undefined' check, which is ugly and too verbose.

But older browsers (IE) don't support const

If you use wrapup first, use AMD, or some other pattern where your code is always in some (function(){ ... code here ... })(); function scope, UglifyJS will remove all const declarations!

echo "(function(){ const FOO = true; console.log(FOO ? 'hey' : 'boo'); })();" | uglifyjs -m -c

results in:

(function(){console.log("hey")})();

which is totally safe!

License

MIT