Skip to content

Composing from a node_module #25

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

Merged
merged 3 commits into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules/*
!node_modules/cool-styles
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Normally you need to use a strict naming convention like BEM to ensure that one

Read Mark Dalgleish's excellent ["End of Global CSS"](https://medium.com/seek-ui-engineering/the-end-of-global-css-90d2a4a06284) and check out [css-modules](https://github.com/css-modules/css-modules) for more context.

## Usage
## Getting started

First install the package: `npm install --save css-modulesify`

Expand All @@ -31,7 +31,27 @@ var div = `<div class="${styles.inner}">...</div>`;

The generated css will contain locally-scoped versions of any css you have `require`'d, and will be written out to the file you specify in the `--output` or `-o` option.

### PostCSS Plugins
## API Usage

```js
var b = require('browserify')();

b.add('./main.js');
b.plugin(require('css-modulesify'), {
rootDir: __dirname,
output: './path/to/my.css'
});

b.bundle();
```

### Options:

- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames.
- `output`: path to write the generated css
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins)

## PostCSS Plugins

The following PostCSS plugins are enabled by default:

Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var cssExt = /\.css$/;
module.exports = function (browserify, options) {
options = options || {};

var rootDir = options.rootDir || options.d || '/';

var cssOutFilename = options.output || options.o;
if (!cssOutFilename) {
throw new Error('css-modulesify needs the --output / -o option (path to output css file)');
Expand Down Expand Up @@ -84,13 +86,12 @@ module.exports = function (browserify, options) {

return through(function noop () {}, function end () {
var self = this;

var loader = new FileSystemLoader(path.dirname(filename), plugins);
var loader = new FileSystemLoader(rootDir, plugins);

// pre-populate the loader's tokensByFile
loader.tokensByFile = tokensByFile;

loader.fetch(path.basename(filename), '/').then(function (tokens) {
loader.fetch(path.relative(rootDir, filename), '/').then(function (tokens) {
var output = "module.exports = " + JSON.stringify(tokens);

assign(tokensByFile, loader.tokensByFile);
Expand All @@ -101,7 +102,7 @@ module.exports = function (browserify, options) {
self.queue(output);
self.queue(null);
}, function (err) {
console.error(err);
console.error('loader err', err);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A browserify transform to load CSS Modules",
"main": "index.js",
"dependencies": {
"css-modules-loader-core": "0.0.11",
"css-modules-loader-core": "0.0.12",
"object-assign": "^3.0.0",
"string-hash": "^1.1.0",
"through": "^2.3.7"
Expand Down
1 change: 0 additions & 1 deletion tests/.gitignore

This file was deleted.

6 changes: 6 additions & 0 deletions tests/cases/compose-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
._cool_styles_styles__foo {
color: #F00;
}
._styles__foo {
background: black;
}
1 change: 1 addition & 0 deletions tests/cases/compose-node-module/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var styles = require('./styles.css');
4 changes: 4 additions & 0 deletions tests/cases/compose-node-module/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.foo {
composes: foo from "cool-styles/styles.css";
background: black;
}
2 changes: 1 addition & 1 deletion tests/cases/import-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
._styles__foo {
._node_modules_cool_styles_styles__foo {
color: #F00;
}
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function runTestCase (dir) {
var b = browserify();
b.add(path.join(casesDir, dir, 'main.js'));
b.plugin(cssModulesify, {
rootDir: path.join(casesDir, dir),
output: cssOutFilename
});

Expand Down