detect-environment
Version:
Detect environments in JavaScript similar to the way Laravel does
46 lines (34 loc) • 1.3 kB
Markdown
in within a project that is using [gulp](http://gulpjs.com/)
follow the example `gulpfile.js` below.
```js
// Pull in `gulp` and its plugins.
var gulp = require('gulp');
var gif = require('gulp-if');
var uglify = require('gulp-uglify');
// Pull in the `detect-environment` plugin.
var de = require('detect-environment');
// Determine our environment name and fetch its data.
var env = de();
// Here we utilise the `gulp-if` plugin to conditionaly run the `gulp-uglify`
// plugin. The condition that we are tracking is whether or not we are in the
// `production` environment.
gulp.task('js', function () {
return gulp.src('src/**/*.js')
.pipe(gif(env.name === 'production', uglify()))
.pipe(gulp.dest('dist');
});
```
To action this `gulpfile` simply run the following command in your terminal.
```sh
$ gulp js
```
When no environment is specified, the detect environment package will return a
default `env.name` of `local`. When coupled with the `gulp-if` plugin the
`uglify()` plugin will not be run.
However, if an environment is specified.
```sh
$ gulp js --env=production
```
Then the `uglify()` task will be executed and the resuling JavaScript files
minified. This is because `env.name` will now be equal to `production`.
To use detect environment