Skip to content

Commit 11a54cb

Browse files
committed
CSS in js
1 parent 5b13a6b commit 11a54cb

28 files changed

+433
-83
lines changed

.eslintrc

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
ecmaFeatures:
3+
modules: true
4+
5+
env:
6+
browser: true
7+
node: true
8+
es6: true
9+
10+
rules:
11+
comma-dangle: [2, "always-multiline"]
12+
no-dupe-args: 2
13+
no-dupe-keys: 2
14+
no-duplicate-case: 2
15+
no-empty-character-class: 2
16+
no-ex-assign: 2
17+
no-extra-boolean-cast: 2
18+
no-extra-parens: 2
19+
no-extra-semi: 2
20+
no-func-assign: 2
21+
no-inner-declarations: 2
22+
no-invalid-regexp: 2
23+
no-irregular-whitespace: 2
24+
no-negated-in-lhs: 2
25+
no-obj-calls: 2
26+
no-regex-spaces: 2
27+
no-sparse-arrays: 2
28+
no-unreachable: 2
29+
use-isnan: 2
30+
valid-typeof: 2
31+
no-unexpected-multiline: 2
32+
no-constant-condition: 2
33+
no-control-regex: 2
34+
no-debugger: 2
35+
# code style
36+
consistent-return: 0
37+
curly: [2, "multi-line"]
38+
default-case: 2
39+
dot-notation: 2
40+
dot-location: [2, "property"]
41+
eqeqeq: [2, "allow-null"]
42+
no-else-return: 2
43+
no-lone-blocks: 2
44+
no-loop-func: 2
45+
no-multi-spaces: 2
46+
no-multi-str: 2
47+
no-proto: 2
48+
no-redeclare: 2
49+
no-return-assign: 2
50+
no-sequences: 2
51+
no-throw-literal: 2
52+
no-unused-expressions: 2
53+
no-void: 2
54+
no-warning-comments: [2, { "terms": ["todo", "fixme", "xxx"], "location": "start" }]
55+
no-with: 2
56+
radix: 2
57+
no-delete-var: 2
58+
no-shadow-restricted-names: 2
59+
no-shadow: 2
60+
no-undef: 2
61+
no-unused-vars: 2
62+
brace-style: [2, "1tbs", { "allowSingleLine": true }]
63+
comma-spacing: 2
64+
comma-style: 2
65+
indent: [2, 2]
66+
key-spacing: 2
67+
max-nested-callbacks: [2, 5]
68+
no-lonely-if: 2
69+
no-mixed-spaces-and-tabs: 2
70+
no-nested-ternary: 2
71+
no-spaced-func: 2
72+
no-trailing-spaces: 2
73+
one-var: [2, "never"]
74+
operator-linebreak: 2
75+
quote-props: [2, "as-needed"]
76+
quotes: [2, "single", "avoid-escape"]
77+
semi: [2, "never"]
78+
keyword-spacing: 2
79+
space-before-blocks: 2
80+
space-infix-ops: 2

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
/node_modules/
2+
.DS_Store
3+
npm-debug.log
4+
build/
5+
coverage/
6+
.nyc_output/

.npmignore

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
.gitignore
2-
/test/
3-
/example/
1+
npm-debug.log
2+
changelog.md
3+
coverage/
4+
example/
5+
test/
6+
.*

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "stable"
4+
- "4"
5+
- "0.12"

README.md

+68-20
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,86 @@
11
# browserify-postcss
2-
transform contents using postcss
2+
A [browserify] transform to work with [postcss].
33

4-
## Usage
4+
## Example
55

6-
example/var.js:
6+
The build script:
77

88
```javascript
9-
var plugin = require("browserify-postcss")
9+
var browserify = require('browserify')
10+
var fs = require('fs')
1011

11-
var tr = plugin("fake.css", {
12-
plugin: "postcss-simple-vars",
13-
basedir: __dirname
12+
var to = __dirname + '/static/bundle.js'
13+
var b = browserify(__dirname + '/src/entry.js')
14+
b.transform(require('browserify-postcss', {
15+
// a list of postcss plugins
16+
plugin: [
17+
'postcss-import',
18+
'postcss-advanced-variables',
19+
['postcss-custom-url', [
20+
['inline', { maxSize: 10 }],
21+
['copy', { assetOutFolder: __dirname + '/static/assets' }],
22+
]],
23+
],
24+
// basedir where to search plugins
25+
basedir: __dirname + '/src',
26+
// options for processing.
27+
postCssOptions: { to: to },
28+
// insert a style element to apply the styles
29+
inject: true,
1430
})
15-
tr.end("$color: red; .fake { color: $color; }")
16-
tr.pipe(process.stdout)
31+
b.bundle().pipe(
32+
fs.createWriteStream(to)
33+
)
1734

1835
```
1936
20-
output:
37+
entry.js:
2138
22-
```
23-
⌘ node example/vars.js
24-
.fake { color: red; }
25-
```
39+
```js
40+
require('./entry.css')
2641

27-
### tr = plugin(file, opts)
42+
console.log('styles from entry.css are applied')
43+
44+
```
2845
29-
### Options
46+
## Options
3047
31-
#### plugin
48+
### plugin
49+
Specify a list of [postcss] plugins to apply.
3250
33-
Type: `String|Array`
51+
Type: `String`, `Array`
3452
Default: `null`
3553
36-
postcss plugins used to transform the content
54+
### basedir
55+
Specify where to look for plugins.
56+
57+
### postCssOptions
58+
Specify the options for the [postcss] processor.
59+
60+
The `from` and `to` fields will be set to the css file path by default.
61+
62+
### inject
63+
Specify how to use the styles:
64+
65+
If `true`, styles are applied immediately.
66+
If `false`, `require('style.css')` will return the string representation of the styles.
67+
68+
### extensions
69+
A list of file extensions to identify styles.
70+
71+
Type: `Array`
72+
73+
Default: `['.css', '.scss', '.sass']`
74+
75+
## Watch
76+
Imported files will **NOT** be watched when used with [watchify].
77+
78+
## Related
79+
80+
* [reduce-css]: bundle css files without `require`ing them in js.
81+
3782
38-
If `Array`, each element can be `String`, `Function`, or `Array`.
83+
[browserify]: https://github.com/substack/node-browserify
84+
[watchify]: https://github.com/substack/watchify
85+
[postcss]: https://github.com/postcss/postcss
86+
[reduce-css]: https://github.com/reducejs/reduce-css

browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('cssify')

example/build.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var browserify = require('browserify')
2+
var fs = require('fs')
3+
4+
var del = require('del')
5+
6+
del.sync(__dirname + '/static/assets')
7+
del.sync(__dirname + '/static/bundle.js')
8+
9+
var to = __dirname + '/static/bundle.js'
10+
var b = browserify(__dirname + '/src/entry.js')
11+
b.transform(require('..'), {
12+
plugin: [
13+
'postcss-import',
14+
'postcss-advanced-variables',
15+
['postcss-custom-url', [
16+
['inline', { maxSize: 10 }],
17+
['copy', { assetOutFolder: __dirname + '/static/assets' }],
18+
]],
19+
],
20+
basedir: __dirname + '/src',
21+
postCssOptions: function (file) {
22+
return { from: file, to: to }
23+
},
24+
inject: true,
25+
})
26+
b.bundle().pipe(
27+
fs.createWriteStream(to)
28+
)

example/src/deps/index.css

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.inline {
2+
width: 258px;
3+
height: 192px;
4+
background-image: url(octocat_setup.png);
5+
}
6+
7+
.copy {
8+
width: 356px;
9+
height: 192px;
10+
background-image: url(octocat_fork.png);
11+
}
12+

example/src/deps/lazyloading.png

98 Bytes
Loading

example/src/deps/octocat_fork.png

24.8 KB
Loading

example/src/deps/octocat_setup.png

7.74 KB
Loading

example/src/entry.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import "colors";
2+
@import "./deps";
3+
4+
.entry {
5+
color: $red;
6+
}
7+

example/src/entry.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('./entry.css')
2+
3+
console.log('styles from entry.css are applied')
4+

example/src/node_modules/browserify-postcss

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/node_modules/colors/index.css

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
24.8 KB
Loading

example/static/bundle.js

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/static/index.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
5+
<title>browserify-postcss</title>
6+
<script src="bundle.js"></script>
7+
</head>
8+
<body>
9+
<h2>Styles from entry</h2>
10+
<p class="entry">Text should be <strong>RED</strong></p>
11+
<h2>Styles from deps</h2>
12+
<ul>
13+
<li>inlined image:
14+
<div class="inline"></div>
15+
</li>
16+
<li>copied image:
17+
<div class="copy" style></div>
18+
</li>
19+
</ul>
20+
</body>
21+
</html>
22+

example/vars.js

-8
This file was deleted.

0 commit comments

Comments
 (0)