Skip to content

Commit c4bc672

Browse files
committed
Add initial browserify transform.
No test runner yet, but "npm test" output in test/browserify is expected to match expected.out.
1 parent 39b34a3 commit c4bc672

File tree

11 files changed

+120
-0
lines changed

11 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
node_modules

browserify.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var transformTools = require('browserify-transform-tools');
2+
3+
var CLASSNAME_SEARCH = /\b([a-z][a-zA-Z_-]*)/g;
4+
5+
// @see https://github.com/benbria/browserify-transform-tools#creating-a-falafel-transform
6+
module.exports = transformTools.makeFalafelTransform('component-css-ns', {}, function(node, transformOptions, done) {
7+
if (node.type === 'Property' && node.key.name === 'className') {
8+
var componentNs = transformOptions.file.replace(/.*\/(\w+).*/, '$1');
9+
node.value.update(node.value.source() + '.replace(' + CLASSNAME_SEARCH.toString() + ', "' + componentNs + '-$1")');
10+
}
11+
done();
12+
});

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "component-css-ns",
3+
"version": "0.0.1",
4+
"description": "",
5+
"dependencies": {
6+
"browserify-transform-tools": "1.3.3"
7+
},
8+
"devDependencies": {
9+
"browserify": "9.0.8",
10+
"classnames": "1.2.1",
11+
"react": "0.13.2",
12+
"reactify": "1.1.0"
13+
},
14+
"scripts": {
15+
"test": "echo \"Error: no test specified\" && exit 1"
16+
},
17+
"author": "Jarno Rantanen <jarno@jrw.fi>",
18+
"license": "MIT"
19+
}

test/browserify/MyComponent.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var React = require('react');
2+
var cx = require('classnames');
3+
var SubComponent = require('./SubComponent.jsx');
4+
5+
module.exports = React.createClass({
6+
7+
render: function() {
8+
9+
return (
10+
<div className="foo bar-x baz_y lib-herp">
11+
<span className={cx({ derp: true })} />
12+
<SubComponent />
13+
</div>
14+
);
15+
16+
}
17+
18+
});

test/browserify/MyComponent.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.foo {
2+
.bar-x {
3+
.baz_y {
4+
color: red;
5+
}
6+
}
7+
}
8+
9+
@import 'SubComponent';

test/browserify/SubComponent.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var React = require('react');
2+
3+
module.exports = React.createClass({
4+
5+
render: function() {
6+
7+
return (
8+
<div className="something" />
9+
);
10+
11+
}
12+
13+
});

test/browserify/SubComponent.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.something {
2+
color: blue;
3+
}

test/browserify/expected.out

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2+
var React = require('react');
3+
var cx = require('classnames');
4+
var SubComponent = require('./SubComponent.jsx');
5+
6+
module.exports = React.createClass({displayName: "exports",
7+
8+
render: function() {
9+
10+
return (
11+
React.createElement("div", {className: "foo bar-x baz_y lib-herp".replace(/\b([a-z][a-zA-Z_-]*)/g, "MyComponent-$1")},
12+
React.createElement("span", {className: cx({ derp: true }).replace(/\b([a-z][a-zA-Z_-]*)/g, "MyComponent-$1")}),
13+
React.createElement(SubComponent, null)
14+
)
15+
);
16+
17+
}
18+
19+
});
20+
21+
22+
},{"./SubComponent.jsx":2,"classnames":undefined,"react":undefined}],2:[function(require,module,exports){
23+
var React = require('react');
24+
25+
module.exports = React.createClass({displayName: "exports",
26+
27+
render: function() {
28+
29+
return (
30+
React.createElement("div", {className: "something".replace(/\b([a-z][a-zA-Z_-]*)/g, "SubComponent-$1")})
31+
);
32+
33+
}
34+
35+
});
36+
37+
38+
},{"react":undefined}]},{},[1]);

test/browserify/node_modules/.bin

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browserify/node_modules/component-css-ns

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browserify/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"test": "browserify --no-bundle-external -t [ reactify --es6 ] -t component-css-ns/browserify MyComponent.jsx"
4+
}
5+
}

0 commit comments

Comments
 (0)