Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |