Skip to content

Commit

Permalink
Merge pull request redfin#611 from doug-wade/fix-586
Browse files Browse the repository at this point in the history
Prepend configurable prefix to module tag
  • Loading branch information
doug-wade authored Aug 26, 2016
2 parents be11530 + bb8720c commit 6a531ff
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/babel-plugin-react-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ module.exports = function() {
const {name} = node;

const trim = state.opts.trim;
const prefix = state.opts.prefix;
const parent = path.resolve(path.join(process.cwd(), '..')) + path.sep;
const filePath = this.file.opts.filename.replace(parent, '');

//TODO: Support labels
const moduleTag = loggerSpec({ filePath, trim });
const moduleTag = loggerSpec({ filePath, trim, prefix });

let tokens;
if (state.opts.tokens) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
["../../../src", { "prefix": "nicolas-lodeiro." }]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var logger = require('react-server').logging.getLogger(__LOGGER__);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var logger = require('react-server').logging.getLogger({"name":"nicolas-lodeiro.babel-plugin-react-server.test.fixtures.prefix.actual","color":{"server":207,"client":"rgb(212,42,212)"}});
4 changes: 2 additions & 2 deletions packages/react-server-gulp-module-tagger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ Given a [`getLogger`](http://redfin.github.io/react-server/annotated-src/logging
adds the correct arguments to keep the server and the browser in sync.

For example, given a module in `src/components/my-feature/foo.js`, and using the options
`{ trim: 'src.' }`
`{ trim: 'src.', prefix: 'react-server.' }`

```javascript
let logger = require("react-server").logging.getLogger(__LOGGER__);
```

returns a logger instance that will have consistent coloring on the server and
the client, and that has a human-friendly, readable name that easily maps to
the file tree (in this example `components.my-feature.foo`).
the file tree (in this example `react-server.components.my-feature.foo`).

If you need more than one logger in your module, you can distinguish them
with labels
Expand Down
4 changes: 3 additions & 1 deletion packages/react-server-gulp-module-tagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ module.exports = function(config) {
return loggerSpec({
filePath: file.path,
basePath: config.basePath,
trim: config.trim, optString,
trim: config.trim,
prefix: config.prefix,
optString,
});
}));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var logger = require('react-server').logging.getLogger(__LOGGER__);
var fooLogger = logging.getLogger(__LOGGER__({ label: "foo" }));
var barLogger = logging.getLogger(__LOGGER__({ label: "bar" }));

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const gulp = require('gulp');
const tagger = require('../../..');

gulp.task('default', () => {
gulp.src('actual.js')
.pipe(tagger({ prefix: 'christian-roldan.' }))
.pipe(gulp.dest('build'));
});
12 changes: 9 additions & 3 deletions packages/react-server-module-tagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ var isWindows = ('win32' === process.platform);
* @param {String} optString The label to add to the module tag, in the form '({label:"$label"})'
* @param {String} trim The prefix to remove from the logger name
* @param {String} basePath The path to the root of the project
* @param {String} prefix A prefix to prepend to the logger name
* @return {String} A json object containing a module identifier
*/
module.exports = function(arg){
var optString = arg.optString
, fn = arg.filePath
, trim = arg.trim || ''
, basePath = arg.basePath || ''
, prefix = arg.prefix
, opts = {}

if (fn.indexOf(basePath) !== 0) {
Expand All @@ -28,7 +30,7 @@ module.exports = function(arg){
opts = new Function("return "+optString.replace(/^\/\//mg,''))(); // eslint-disable-line no-new-func
}

opts.name = getName (fn, opts, trim, basePath);
opts.name = getName (fn, opts, trim, basePath, prefix);
opts.color = getColor (opts);

return JSON.stringify(opts);
Expand All @@ -45,11 +47,12 @@ module.exports = function(arg){
* ) // returns "components.my-component"
* @param {String} fn filename
* @param {Object} opts { label: 'Optional logger label' }
* @param {String} trim The leading portion of the name to remove.
* @param {String} trim The leading portion of the name to remove
* @param {String} basePath The path to the file
* @param {String} prefix A prefix to prepend to the name
* @return {String} The logger name, e.g. my-project.components.my-component
*/
var getName = function(fn, opts, trim, basePath){
var getName = function(fn, opts, trim, basePath, prefix){
var slashPattern = isWindows
?/\\/g
:/\//g
Expand All @@ -59,6 +62,9 @@ var getName = function(fn, opts, trim, basePath){
if (opts.label) {
name += '.'+opts.label
}
if (prefix) {
name = prefix + name
}
return name;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/react-server-module-tagger/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ test('trims prefix from module tag name', t => {
t.is(expected, actual);
});

test('adds prefix to module tag name', t => {
const expected = '{\"name\":\"foo.bar.baz\",\"color\":{\"server\":131,\"client\":\"rgb(127,42,42)\"}}';

const filePath = 'bar/baz';
const prefix = 'foo.';
const actual = loggerSpec({filePath, prefix});

t.is(expected, actual);
});

test('adds labels', t => {
const expected = '{\"label\":\"foo\",\"name\":\"has.label.foo\",\"color\":{\"server\":131,\"client\":\"rgb(127,42,42)\"}}';

Expand Down

0 comments on commit 6a531ff

Please sign in to comment.