Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/constantify
Transform your source to inline const values
https://github.com/hughsk/constantify
Last synced: 8 days ago
JSON representation
Transform your source to inline const values
- Host: GitHub
- URL: https://github.com/hughsk/constantify
- Owner: hughsk
- License: mit
- Created: 2013-10-03T13:17:42.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-14T11:49:21.000Z (about 9 years ago)
- Last Synced: 2024-10-17T16:41:14.609Z (22 days ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# constantify [![experimental](http://hughsk.github.io/stability-badges/dist/experimental.svg)](http://github.com/hughsk/stability-badges) #
A [browserify](http://browserify.org) transform (and standalone function)
that inlines `const` values into your code. Like
[envify](http://github.com/hughsk/envify), this comes in handy for things
such as conditional compilation and as such works well with
[uglifyify](http://github.com/hughsk/uglifyify).## Usage ##
[![constantify](https://nodei.co/npm/constantify.png?mini=true)](https://nodei.co/npm/constantify)
constantify will pick up `const` definitions at the top of your module, i.e.
outside of any closures, and replace their references with their actual values.
Note that this only works with strings and numbers.For example, the following code:
``` javascript
const generate = require('./generate')
const SIZE = 64
const SIZE_SQUARED = 64*64var array = new Float32Array(SIZE_SQUARED)
var n = 0for (var x = 0; x < SIZE; x += 1)
for (var y = 0; y < SIZE; y += 1) {
array[n++] = generate(x, y)
}
```Will be transformed to become this:
``` javascript
const generate = require('./generate')var array = new Float32Array(4096)
var n = 0for (var x = 0; x < 64; x += 1)
for (var y = 0; y < 64; y += 1) {
array[n++] = generate(x, y)
}
```You can use constantify the same you would as any other browserify transform:
``` bash
browserify -t constantify
```And to use it from another module, just pass it your source as a string to the
`fromString` method, like so:``` javascript
var constantify = require('constantify')
var fs = require('fs')var file = fs.readFileSync(__dirname + '/index.js', 'utf8')
var transformed = constantify.fromString(file)console.log(transformed)
```You can also use it as a standalone command-line tool if installed globally:
``` bash
constantify index.js > bundled.js
cat index.js | constantify > bundled.js
```## License ##
MIT. See [LICENSE.md](http://github.com/hughsk/constantify/blob/master/LICENSE.md) for details.