-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add babel module tagging #301
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "stage-0"] | ||
} |
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,3 @@ | ||
node_modules | ||
*.log | ||
lib |
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,3 @@ | ||
node_modules | ||
*.log | ||
src |
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,90 @@ | ||
# babel-plugin-react-server | ||
|
||
React Server transpilation | ||
|
||
## Example | ||
|
||
**In** | ||
|
||
```js | ||
var logger = require('react-server').logging.getLogger(__LOGGER__); | ||
``` | ||
|
||
**Out** | ||
|
||
```js | ||
"use strict"; | ||
|
||
var logger = require('react-server').logging.getLogger({ name: 'module.name', color: {} }); | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install babel-plugin-react-server | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["react-server"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins react-server script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["react-server"] | ||
}); | ||
``` | ||
|
||
|
||
## Configuration | ||
|
||
A fully configured babel plugin in your babelrc would look be | ||
|
||
```json | ||
{ | ||
"plugins": [ | ||
["react-server", { | ||
"trim": "my-project.components.", | ||
"token": "__LOGGER__" | ||
}] | ||
] | ||
} | ||
``` | ||
|
||
|
||
### Trim | ||
|
||
A substring to trim off the front of the module name | ||
|
||
```javascript | ||
{ | ||
trim: "my-project.pages." | ||
} | ||
``` | ||
|
||
### Token | ||
|
||
The token to replace in the source code with the module tag. By default, uses | ||
the default logger token `__LOGGER__`, and two future reserved tokens, | ||
`__CHANNEL__` and | ||
|
||
```javascript | ||
{ | ||
token: "__LOGGER__" | ||
} | ||
``` |
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,31 @@ | ||
{ | ||
"name": "babel-plugin-react-server", | ||
"version": "0.0.1", | ||
"description": "Babel plugin for React Server transpilation", | ||
"repository": "redfin/babel-plugin-react-server", | ||
"author": "Doug Wade <doug.wade@redfin.com>", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"react-server-module-tagger": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.3.17", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"mocha": "^2.2.5" | ||
}, | ||
"scripts": { | ||
"clean": "rm -rf lib npm-debug.log*", | ||
"build": "babel src -d lib", | ||
"test": "mocha --compilers js:babel-register", | ||
"test:watch": "npm run test -- --watch", | ||
"prepublish": "npm run clean && npm run build" | ||
}, | ||
"keywords": [ | ||
"react", | ||
"server", | ||
"babel", | ||
"plugin", | ||
"babel-plugin" | ||
] | ||
} |
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,34 @@ | ||
import loggerSpec from 'react-server-module-tagger'; | ||
import path from 'path'; | ||
|
||
module.exports = function({types: t }) { | ||
return { | ||
visitor: { | ||
Identifier(p, state) { | ||
const {node} = p; | ||
const {name, type} = node; | ||
|
||
const config = { trim: state.opts.trim }; | ||
const parent = path.resolve(path.join(process.cwd(), '..')) + path.sep; | ||
const fp = this.file.opts.filename.replace(parent, ''); | ||
const file = { path: fp }; | ||
//TODO: Support labels | ||
const moduleTag = loggerSpec.bind({ file, config })(fp); | ||
|
||
let tokens; | ||
if (state.opts.tokens) { | ||
tokens = new Set(state.opts.tokens); | ||
} else { | ||
tokens = new Set(["__LOGGER__", "__CHANNEL__", "__CACHE__"]); | ||
} | ||
|
||
if (tokens.has(name)) { | ||
// this strikes me as a dirty, nasty hack. I think it would be better | ||
// to parse the object as json and coerce it to an array of | ||
// ObjectProperties to construct an ObjectExpression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
p.node.name = moduleTag; | ||
} | ||
} | ||
} | ||
}; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-react-server/test/fixtures/configurable-token/.babelrc
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,5 @@ | ||
{ | ||
"plugins": [ | ||
["../../../src", { "tokens": ["BAR"] }] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/configurable-token/actual.js
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 @@ | ||
var logger = require('react-server').logging.getLogger(BAR); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/configurable-token/expected.js
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 @@ | ||
var logger = require('react-server').logging.getLogger({"name":"babel-plugin-react-server.test.fixtures.configurable-token.actual","color":{"server":133,"client":"rgb(127,42,127)"}}); |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-react-server/test/fixtures/example/.babelrc
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,5 @@ | ||
{ | ||
"plugins": [ | ||
["../../../src"] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/example/actual.js
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 @@ | ||
var logger = require('react-server').logging.getLogger(__LOGGER__); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/example/expected.js
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 @@ | ||
var logger = require('react-server').logging.getLogger({"name":"babel-plugin-react-server.test.fixtures.example.actual","color":{"server":131,"client":"rgb(127,42,42)"}}); |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-react-server/test/fixtures/reserved-future-tokens/.babelrc
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,5 @@ | ||
{ | ||
"plugins": [ | ||
["../../../src"] | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/babel-plugin-react-server/test/fixtures/reserved-future-tokens/actual.js
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,2 @@ | ||
__CHANNEL__ | ||
__CACHE__ |
2 changes: 2 additions & 0 deletions
2
packages/babel-plugin-react-server/test/fixtures/reserved-future-tokens/expected.js
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,2 @@ | ||
{"name":"babel-plugin-react-server.test.fixtures.reserved-future-tokens.actual","color":{"server":159,"client":"rgb(127,212,212)"}}; | ||
{"name":"babel-plugin-react-server.test.fixtures.reserved-future-tokens.actual","color":{"server":159,"client":"rgb(127,212,212)"}}; |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-react-server/test/fixtures/trim/.babelrc
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,5 @@ | ||
{ | ||
"plugins": [ | ||
["../../../src", { "trim": "babel-plugin-react-server.test.fixtures." }] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/trim/actual.js
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 @@ | ||
var logger = require('react-server').logging.getLogger(__LOGGER__); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-react-server/test/fixtures/trim/expected.js
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 @@ | ||
var logger = require('react-server').logging.getLogger({"name":"trim.actual","color":{"server":207,"client":"rgb(212,42,212)"}}); |
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,26 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import assert from 'assert'; | ||
import { transformFileSync } from 'babel-core'; | ||
import plugin from '../src'; | ||
|
||
function trim(str) { | ||
return str.replace(/^\s+|\s+$/, ''); | ||
} | ||
|
||
describe('React Server transpilation', () => { | ||
const fixturesDir = path.join(__dirname, 'fixtures'); | ||
fs.readdirSync(fixturesDir).map((caseName) => { | ||
it(`should ${caseName.split('-').join(' ')}`, () => { | ||
const fixtureDir = path.join(fixturesDir, caseName); | ||
const actualPath = path.join(fixtureDir, 'actual.js'); | ||
const actual = transformFileSync(actualPath).code; | ||
|
||
const expected = fs.readFileSync( | ||
path.join(fixtureDir, 'expected.js') | ||
).toString(); | ||
|
||
assert.equal(trim(actual), trim(expected)); | ||
}); | ||
}); | ||
}); |
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,19 +1,11 @@ | ||
module.exports = { | ||
plugins: [ | ||
require('babel-plugin-transform-es2015-arrow-functions'), | ||
require('babel-plugin-transform-es2015-block-scoping'), | ||
require('babel-plugin-transform-es2015-classes'), | ||
require('babel-plugin-transform-es2015-computed-properties'), | ||
require('babel-plugin-transform-es2015-constants'), | ||
require('babel-plugin-transform-es2015-destructuring'), | ||
require('babel-plugin-transform-es2015-modules-commonjs'), | ||
require('babel-plugin-transform-es2015-parameters'), | ||
require('babel-plugin-transform-es2015-shorthand-properties'), | ||
require('babel-plugin-transform-es2015-spread'), | ||
require('babel-plugin-transform-es2015-template-literals'), | ||
require('babel-plugin-transform-object-rest-spread'), | ||
require('babel-plugin-react-server'), | ||
require('babel-plugin-transform-runtime'), | ||
], | ||
presets: [ | ||
require('babel-preset-es2015'), | ||
require('babel-preset-react'), | ||
require('babel-preset-stage-0'), | ||
], | ||
} |
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
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,37 @@ | ||
import test from 'ava'; | ||
import fs from 'fs'; | ||
import { transform } from 'babel-core'; | ||
|
||
test('module can be required', t => { | ||
try { | ||
require('.'); | ||
t.pass(); | ||
} catch (e) { | ||
console.error(e); | ||
t.fail(); | ||
} | ||
}); | ||
|
||
test('transpiles as expected', async t => { | ||
const source = await readFile('tst/source.js'); | ||
const actual = transform(source, { presets: [require('.')] }).code; | ||
const expected = await readFile('tst/expected.js'); | ||
t.is(trim(actual), trim(expected)); | ||
}); | ||
|
||
function readFile(filename) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
} else { | ||
resolve(data.toString()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function trim(str) { | ||
return str.replace(/^\s+|\s+$/, ''); | ||
} |
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,7 @@ | ||
'use strict'; | ||
|
||
var _reactServer = require('react-server'); | ||
|
||
var label = 'foo'; | ||
var logger = _reactServer.logging.getLogger({"name":"unknown","color":{"server":207,"client":"rgb(212,42,212)"}}); | ||
var fooLogger = _reactServer.logging.getLogger({"name":"unknown","color":{"server":207,"client":"rgb(212,42,212)"}}({ label: label })); |
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,5 @@ | ||
import {logging} from 'react-server'; | ||
|
||
const label = 'foo'; | ||
const logger = logging.getLogger(__LOGGER__); | ||
const fooLogger = logging.getLogger(__LOGGER__({ label })); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, a nicer interface for the module tagger would eliminate the need for this nonsense.