Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinbate committed Jan 25, 2016
1 parent e40e9f5 commit f9531d7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
@@ -1,2 +1,21 @@
# environment-brunch
Replaces a defined token with the current environment in Brunch.

Replaces a defined token with the current environment in your js/ts files in Brunch.

The environment defaults to `development` if not set.

It will replace the string `BRUNCH_ENVIRONMENT` by default, but this can be overridden by setting the `token` config.

```js
module.exports.config = {
plugins: {
environment: {
token: /\$!ENV!\$/g
}
}
}
```

You can also provide a string value and that will work as well. If you provide a regular expression, make sure that you set the `g` flag to replace all occurences, not just the first one.

The replacement is not wrapped in anything so make sure you account for that in your files.
36 changes: 36 additions & 0 deletions index.js
@@ -0,0 +1,36 @@
'use strict';
// Documentation for Brunch plugins:
// https://github.com/brunch/brunch/blob/master/docs/plugins.md
const DEFAULT_TOKEN = /BRUNCH_ENVIRONMENT/g;
class BrunchPlugin {
constructor(config) {
this.config = config && config.plugins && config.plugins.environment || {};
this.token = this.config.token || DEFAULT_TOKEN;
if (this.token instanceof RegExp) {
this.search = this.token;
} else {
this.search = new RegExp(this.token, 'g');
}
if (config.env && config.env.length) {
this.env = config.env[0];
} else {
this.env = 'development';
}
}

compile(file) {
file.data = file.data.replace(this.search, this.env);
return Promise.resolve(file);
}
}

BrunchPlugin.prototype.brunchPlugin = true;
BrunchPlugin.prototype.type = 'javascript';
BrunchPlugin.prototype.pattern = /\.[jt]sx?$/;

// Indicates which environment a plugin should be applied to.
// The default value is '*' for usual plugins and
// 'production' for optimizers.
// BrunchPlugin.prototype.defaultEnv = 'production';

module.exports = BrunchPlugin;
27 changes: 27 additions & 0 deletions package.json
@@ -0,0 +1,27 @@
{
"name": "environment-brunch",
"version": "1.0.0",
"description": "Replaces a defined token with the current brunch environment.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/colinbate/environment-brunch.git"
},
"keywords": [
"brunch",
"environment",
"replace"
],
"author": "Colin Bate <colin@colinbate.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/colinbate/environment-brunch/issues"
},
"homepage": "https://github.com/colinbate/environment-brunch#readme",
"peerDependencies": {
"brunch": "^2.0.0"
}
}

0 comments on commit f9531d7

Please sign in to comment.