Skip to content

Commit e0c6aeb

Browse files
ship patched envify, fixes #18 (#22)
* Include patched envify * Add test * Update minify-stream.
1 parent fdd5e5a commit e0c6aeb

File tree

13 files changed

+578
-5
lines changed

13 files changed

+578
-5
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var packFlatStream = require('browser-pack-flat')
44
var commonShake = require('common-shakeify')
55
var unassertify = require('unassertify')
66
var uglify = require('minify-stream')
7-
var envify = require('envify/custom')
7+
var envify = require('./private_modules/envify/custom')
88
var uglifyify = require('uglifyify')
99

1010
function makeUglifyOptions (debug) {

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77
"url": "https://github.com/browserify/tinyify/issues"
88
},
99
"dependencies": {
10+
"acorn-node": "^1.8.2",
1011
"browser-pack-flat": "^3.0.9",
1112
"bundle-collapser": "^1.3.0",
1213
"common-shakeify": "^0.6.0",
13-
"envify": "^4.1.0",
14-
"minify-stream": "^1.1.0",
14+
"dash-ast": "^1.0.0",
15+
"minify-stream": "^1.2.1",
16+
"multisplice": "^1.0.0",
17+
"through2": "^3.0.1",
1518
"uglifyify": "^5.0.0",
1619
"unassertify": "^2.1.1"
1720
},
1821
"devDependencies": {
19-
"standard": "^14.3.1"
22+
"browserify": "^16.5.0",
23+
"from2-string": "^1.1.0",
24+
"standard": "^14.3.1",
25+
"tape": "^4.11.0"
2026
},
2127
"homepage": "https://github.com/browserify/tinyify",
2228
"keywords": [
@@ -33,6 +39,11 @@
3339
"url": "https://github.com/browserify/tinyify.git"
3440
},
3541
"scripts": {
36-
"test": "standard"
42+
"test": "standard && node test"
43+
},
44+
"standard": {
45+
"ignore": [
46+
"private_modules/**"
47+
]
3748
}
3849
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*]
2+
indent_size = 2

private_modules/envify/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

private_modules/envify/.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test.js
2+
.travis.yml

private_modules/envify/.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
- '0.12'
5+
- '4'
6+
- '6'
7+
- '8'
8+
- '10'
9+
- '11'
10+
- '12'

private_modules/envify/README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# envify [![Build Status](https://secure.travis-ci.org/hughsk/envify.png?branch=master)](http://travis-ci.org/hughsk/envify) [![stable](http://hughsk.github.io/stability-badges/dist/stable.svg)](http://github.com/hughsk/stability-badges) #
2+
3+
Selectively replace Node-style environment variables with plain strings.
4+
Available as a standalone CLI tool and a
5+
[Browserify](http://browserify.org) v2 transform.
6+
7+
Works best in combination with [uglifyify](http://github.com/hughsk/uglifyify).
8+
9+
## Installation ##
10+
11+
If you're using the module with Browserify:
12+
13+
``` bash
14+
npm install envify browserify
15+
```
16+
17+
Or, for the CLI:
18+
19+
``` bash
20+
sudo npm install -g envify
21+
```
22+
23+
## Usage ##
24+
25+
envify will replace your environment variable checks with ordinary strings -
26+
only the variables you use will be included, so you don't have to worry about,
27+
say, `AWS_SECRET_KEY` leaking through either. Take this example script:
28+
29+
``` javascript
30+
if (process.env.NODE_ENV === "development") {
31+
console.log('development only')
32+
}
33+
```
34+
35+
After running it through envify with `NODE_ENV` set to `production`, you'll
36+
get this:
37+
38+
``` javascript
39+
if ("production" === "development") {
40+
console.log('development only')
41+
}
42+
```
43+
44+
By running this through a good minifier (e.g.
45+
[UglifyJS2](https://github.com/mishoo/UglifyJS)), the above code would be
46+
stripped out completely.
47+
48+
However, if you bundled the same script with `NODE_ENV` set to `development`:
49+
50+
``` javascript
51+
if ("development" === "development") {
52+
console.log('development only')
53+
}
54+
```
55+
56+
The `if` statement will evaluate to `true`, so the code won't be removed.
57+
58+
## CLI Usage ##
59+
60+
With browserify:
61+
62+
``` bash
63+
browserify index.js -t envify > bundle.js
64+
```
65+
66+
Or standalone:
67+
68+
``` bash
69+
envify index.js > bundle.js
70+
```
71+
72+
You can also specify additional custom environment variables using
73+
browserify's [subarg](http://github.com/substack/subarg) syntax, which is
74+
available in versions 3.25.0 and above:
75+
76+
``` bash
77+
browserify index.js -t [ envify --NODE_ENV development ] > bundle.js
78+
browserify index.js -t [ envify --NODE_ENV production ] > bundle.js
79+
```
80+
81+
## Module Usage ##
82+
83+
**require('envify')**
84+
85+
Returns a transform stream that updates based on the Node process'
86+
`process.env` object.
87+
88+
**require('envify/custom')([environment])**
89+
90+
If you want to stay away from your environment variables, you can supply
91+
your own object to use in its place:
92+
93+
``` javascript
94+
var browserify = require('browserify')
95+
, envify = require('envify/custom')
96+
, fs = require('fs')
97+
98+
var b = browserify('main.js')
99+
, output = fs.createWriteStream('bundle.js')
100+
101+
b.transform(envify({
102+
NODE_ENV: 'development'
103+
}))
104+
b.bundle().pipe(output)
105+
```
106+
107+
## Purging `process.env` ##
108+
109+
By default, environment variables that are not defined will be left untouched.
110+
This is because in some cases, you might want to run an envify transform over
111+
your source more than once, and removing these values would make that
112+
impossible.
113+
114+
However, if any references to `process.env` are remaining after transforming
115+
your source with envify, browserify will automatically insert its shim for
116+
Node's process object, which will increase the size of your bundle. This weighs
117+
in at around 2KB, so if you're trying to be conservative with your bundle size
118+
you can "purge" these remaining variables such that any missing ones are simply
119+
replaced with undefined.
120+
121+
To do so through the command-line, simply use the subarg syntax and include
122+
`purge` after `envify`, e.g.:
123+
124+
``` bash
125+
browserify index.js -t [ envify purge --NODE_ENV development ]
126+
```
127+
128+
Or if you're using the module API, you can pass `_: "purge"` into your
129+
arguments like so:
130+
131+
``` javascript
132+
b.transform(envify({
133+
_: 'purge'
134+
, NODE_ENV: 'development'
135+
}))
136+
```
137+
138+
## Contributors ##
139+
140+
* [hughsk](http://github.com/hughsk)
141+
* [benjamn](http://github.com/benjamn)
142+
* [zag2art](http://github.com/zag2art)
143+
* [bjoerge](http://github.com/bjoerge)
144+
* [andreypopp](http://github.com/andreypopp)
145+
* [jupl](http://github.com/jupl)

private_modules/envify/bin/envify

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
3+
var envify = require('../')
4+
, fs = require('fs')
5+
6+
if (process.argv[2]) {
7+
fs.createReadStream(process.argv[2], { encoding: 'utf8' })
8+
.pipe(envify(process.argv[2]))
9+
.pipe(process.stdout)
10+
} else {
11+
process.stdin.resume()
12+
process.stdin
13+
.pipe(envify(__filename))
14+
.pipe(process.stdout)
15+
}
16+
17+

private_modules/envify/custom.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
var acorn = require('acorn-node')
2+
, dash = require('dash-ast')
3+
, msplice = require('multisplice')
4+
, through = require('through2')
5+
6+
var processEnvPattern = /\bprocess\.env\b/
7+
8+
function equalNodes(a, b) {
9+
if (a.type !== b.type) return false
10+
switch (a.type) {
11+
case 'Literal': return a.value === b.value
12+
case 'Identifier': return a.name === b.name
13+
case 'MemberExpression': return a.computed === b.computed && equalNodes(a.object, b.object) && equalNodes(a.property, b.property)
14+
}
15+
return false
16+
}
17+
18+
module.exports = function(rootEnv) {
19+
rootEnv = rootEnv || process.env || {}
20+
21+
return function envify(file, argv) {
22+
if (/\.json$/.test(file)) return through()
23+
24+
var buffer = []
25+
argv = argv || {}
26+
27+
return through(write, flush)
28+
29+
function write(data, enc, cb) {
30+
buffer.push(data)
31+
cb()
32+
}
33+
34+
function transform(source, envs) {
35+
var args = [].concat(envs[0]._ || []).concat(envs[1]._ || [])
36+
var purge = args.indexOf('purge') !== -1
37+
var replacements = []
38+
39+
function match(node) {
40+
return (
41+
node.type === 'MemberExpression'
42+
&& node.object.type === 'MemberExpression'
43+
&& node.object.computed === false
44+
&& node.object.object.type === 'Identifier'
45+
&& node.object.object.name === 'process'
46+
&& node.object.property.type === 'Identifier'
47+
&& node.object.property.name === 'env'
48+
&& (node.computed ? node.property.type === 'Literal' : node.property.type === 'Identifier')
49+
)
50+
}
51+
52+
var ast = acorn.parse(source)
53+
54+
dash(ast, { leave: function(node) {
55+
if (match(node)) {
56+
var key = node.property.name || node.property.value
57+
for (var i = 0; i < envs.length; i++) {
58+
var value = envs[i][key]
59+
if (value !== undefined) {
60+
replacements.push({ node: node, value: JSON.stringify(value) })
61+
return
62+
}
63+
}
64+
if (purge) {
65+
replacements.push({ node: node, value: 'undefined' })
66+
}
67+
} else if (node.type === 'AssignmentExpression') {
68+
for (var i = 0; i < replacements.length; ++i) {
69+
if (equalNodes(replacements[i].node, node.left)) {
70+
replacements.splice(i, 1)
71+
}
72+
}
73+
}
74+
} })
75+
76+
var splicer = msplice(source)
77+
if (replacements.length > 0) {
78+
replacements.sort(function (a, b) {
79+
return b.start - a.start
80+
})
81+
for (var i = 0; i < replacements.length; i++) {
82+
var r = replacements[i]
83+
splicer.splice(r.node.start, r.node.end, r.value)
84+
}
85+
}
86+
87+
return splicer.toString()
88+
}
89+
90+
function flush(cb) {
91+
var source = buffer.join('')
92+
93+
if (processEnvPattern.test(source)) {
94+
try {
95+
source = transform(source, [argv, rootEnv])
96+
} catch(err) {
97+
return this.emit('error', err)
98+
}
99+
}
100+
101+
this.push(source)
102+
this.push(null)
103+
cb()
104+
}
105+
}
106+
}

private_modules/envify/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./custom')(process.env)

0 commit comments

Comments
 (0)