Skip to content

Commit

Permalink
Add related-file comment annotations
Browse files Browse the repository at this point in the history
This allows tools like relateify, https://github.com/wbyoung/relateify,
to understand what the original CSS file was.
  • Loading branch information
wbyoung committed Jan 14, 2017
1 parent 8abb69f commit 0e655ff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,10 @@ this transform. We recommend:
- [`postcss-cli`][postcss-cli] (v3 or later)
- [`gulp-postcsssrc`][gulp-postcssrc]

Finally, it's worth noting that this transform also adds a comment to the
generated code indicating the related CSS file so that it can be processed by
other tools, i.e. [`relateify`][relateify].

### PostCSS Load Config Context

#### `extractModules(_: any, modules: object)`
Expand All @@ -90,6 +94,12 @@ The function accepts two arguments. The transform uses only the
second value passed to the function. That value is the object value that
replaces the `import`/`require`.

## Using with Browserify & Watchify

This will work well with the [`babelify`][babelify] transform, but if you're
using [`watchify`][watchify], you will want to add the [`relateify`][relateify]
transform in order to ensure that changes to CSS files rebuild the appropriate
JS files.

## Prior Art

Expand All @@ -113,6 +123,9 @@ This project is distributed under the MIT license.
[css-modules-transform]: https://github.com/michalkvasnicak/babel-plugin-css-modules-transform
[css-modules-require-hook]: https://github.com/css-modules/css-modules-require-hook
[gulp-postcssrc]: https://github.com/michael-ciniawsky/gulp-postcssrc
[babelify]: https://github.com/babel/babelify
[watchify]: https://github.com/substack/watchify
[relateify]: https://github.com/wbyoung/relateify

[travis-image]: http://img.shields.io/travis/wbyoung/babel-plugin-transform-postcss.svg?style=flat
[travis-url]: http://travis-ci.org/wbyoung/babel-plugin-transform-postcss
Expand Down
17 changes: 13 additions & 4 deletions src/plugin.js
Expand Up @@ -80,11 +80,20 @@ export default function transformPostCSS({ types: t }: any): any {
}).toString('utf8');
const tokens = JSON.parse(result || '{}');

path.replaceWith(new t.ObjectExpression(
const expression = path.findParent((test) => (
test.isVariableDeclaration() ||
test.isExpressionStatement()
));

expression.addComment(
'trailing', ` @related-file ${stylesheetPath}`, true
);

path.replaceWith(t.objectExpression(
Object.keys(tokens).map(
(token) => new t.ObjectProperty(
new t.StringLiteral(token),
new t.StringLiteral(tokens[token])
(token) => t.objectProperty(
t.stringLiteral(token),
t.stringLiteral(tokens[token])
)
)
));
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/import.expected.js
Expand Up @@ -2,7 +2,7 @@

var _simple = {
"simple": "_simple_jvai8_1"
};
}; // @related-file ./simple.css

var _simple2 = _interopRequireDefault(_simple);

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/require.expected.empty.js
@@ -1,3 +1,3 @@
'use strict';

var styles = {};
var styles = {}; // @related-file ./simple.css
2 changes: 1 addition & 1 deletion test/fixtures/require.expected.js
Expand Up @@ -2,4 +2,4 @@

var styles = {
'simple': '_simple_jvai8_1'
};
}; // @related-file ./simple.css

0 comments on commit 0e655ff

Please sign in to comment.