@@ -18,264 +18,146 @@ Pack CSS into common shared bundles.
18
18
which make ` b.bundle() ` output a stream manipulatable by [ ` gulp ` ] plugins.
19
19
20
20
## Example
21
- Suppose we want to pack css in ` /path/to/src ` (not including those in its subdirectories) into ` /path/to/build/bundle.css ` .
21
+ Check the [ example ] ( example/reduce/ ) .
22
22
23
- There are already ` blue.css ` and ` red.css ` in ` /path/to/src ` , and they both depend upon ` /path/to/src/node_modules/reset/index.js ` .
24
-
25
- ** Input**
26
-
27
- ` blue.css ` :
28
- ``` css
29
- @external "reset";
30
- @import " color" ;
31
- .blue {
32
- color : $blue ;
23
+ ``` js
24
+ var reduce = require (' reduce-css' )
25
+ var del = require (' del' )
26
+ var path = require (' path' )
27
+ var Transform = require (' stream' ).Transform
28
+
29
+ var basedir = path .join (__dirname , ' src' )
30
+
31
+ bundle (createBundler ())
32
+
33
+ function createBundler (watch ) {
34
+ var basedir = path .join (__dirname , ' src' )
35
+ var b = reduce .create (
36
+ /* glob for entries */
37
+ ' *.css' ,
38
+
39
+ /* options for depsify */
40
+ {
41
+ basedir,
42
+ cache: {},
43
+ packageCache: {},
44
+ },
45
+
46
+ /* options for common-bundle */
47
+ // single bundle
48
+ // 'bundle.css',
49
+ // multiple bundles
50
+ {
51
+ groups: ' *.css' ,
52
+ common: ' common.css' ,
53
+ },
54
+
55
+ /* options for watchify2 */
56
+ watch && { entryGlob: ' *.css' }
57
+ )
58
+ return b
33
59
}
34
60
35
- ```
36
-
37
- ` red.css ` :
38
- ``` css
39
- @external "reset";
40
- @external "./button";
41
- @import " color " ;
42
- .red {
43
- color : $ red ;
61
+ function bundle ( b ) {
62
+ var build = path . join ( __dirname , ' build ' )
63
+ del . sync (build)
64
+ return b . bundle (). on ( ' error ' , log)
65
+ . pipe ( b . dest (build, {
66
+ maxSize : 0 ,
67
+ name : ' [name].[hash] ' ,
68
+ assetOutFolder : path . join (build, ' assets ' ),
69
+ }))
44
70
}
45
71
46
- ```
47
-
48
- ` reset ` contains styles to be shared.
49
- We use ` @external ` to declare that
50
- it should come before ` a.css ` and ` b.css ` in the final ` bundle.css ` .
51
- ``` css
52
- html , body {
53
- margin : 0 ;
54
- padding : 0 ;
72
+ function log () {
73
+ console .log .apply (console , [].map .call (arguments , function (msg ) {
74
+ if (typeof msg === ' string' ) {
75
+ return msg
76
+ }
77
+ return JSON .stringify (msg, null , 2 )
78
+ }))
55
79
}
56
80
57
- ```
58
-
59
- The ` color ` module is installed in ` node_modules ` ,
60
- and will be consumed by [ ` postcss ` ] when ` @import ` ed in css.
61
- ``` css
62
- $red: #FF0000;
63
- $green: #00FF00 ;
64
- $blue: #0000FF ;
65
81
66
82
```
67
83
68
- `/path/to/src/button` is a button component,
69
- shipped with a background image (`/path/to/src/button/button .png`),
70
- as well as some styles (`/path/to/src/button/index.css`):
71
- ```css
72
- @import " color" ;
73
- .button {
74
- background-color : $red ;
75
- background-image : url (button.png );
76
- }
77
-
78
- ```
79
- The image will be inlined or copied to the build directory
80
- after bundling, and the url in css will also be transformed to
81
- reference to it correctly.
82
-
83
- ** Building script**
84
+ To watch file changes:
84
85
85
86
``` js
86
- ' use strict'
87
-
88
- const reduce = require (' reduce-css' )
89
-
90
- const build = __dirname + ' /build'
91
- const basedir = __dirname + ' /src'
92
- const b = reduce .create ({ basedir })
93
- reduce .src (' *.css' , { cwd: basedir })
94
- .pipe (reduce .bundle (b, ' bundle.css' ))
95
- .pipe (reduce .dest (build, null , {
96
- maxSize: 0 ,
97
- name: ' [name].[hash]' ,
98
- assetOutFolder: build + ' /assets' ,
99
- }))
100
-
101
- ```
102
-
103
- ** Output**
104
-
105
- ` /path/to/build/bundle.css ` :
106
- ``` css
107
- html , body {
108
- margin : 0 ;
109
- padding : 0 ;
110
- }
111
-
112
- .blue {
113
- color : #0000FF ;
114
- }
115
-
116
- .button {
117
- background-color : #FF0000 ;
118
- background-image : url (assets/button.161fff2.png );
119
- }
120
- .red {
121
- color : #FF0000 ;
122
- }
87
+ var b = createBundler (true )
88
+ b .on (' update' , function update () {
89
+ bundle (b)
90
+ return update
91
+ }())
123
92
124
93
```
125
94
126
- The background image has been renamed and copied to ` /path/to/build/assets/button.161fff2.png ` .
127
-
128
- ** Watch**
129
-
130
- To watch file changes:
95
+ To work with gulp:
131
96
132
97
``` js
133
- ' use strict'
134
-
135
- const reduce = require (' reduce-css' )
136
-
137
- const build = __dirname + ' /build'
138
- const basedir = __dirname + ' /src'
139
- const b = reduce .create ({
140
- basedir,
141
- cache: {},
142
- packageCache: {},
98
+ var gulp = require (' gulp' )
99
+ gulp .task (' build' , function () {
100
+ return bundle (createBundler ())
143
101
})
144
102
145
- reduce .src (' *.css' , { cwd: basedir })
146
- .pipe (reduce .watch (b, ' bundle.css' , { entryGlob: ' *.css' }))
147
- .on (' bundle' , function (bundleStream ) {
148
- bundleStream .pipe (reduce .dest (build, null , {
149
- maxSize: 0 ,
150
- name: ' [name].[hash]' ,
151
- assetOutFolder: build + ' /assets' ,
152
- }))
153
- .on (' data' , file => console .log (' bundle:' , file .relative ))
154
- .on (' end' , () => console .log (' -' .repeat (40 )))
155
- })
156
-
103
+ gulp .task (' watch' , function (cb ) {
104
+ var b = createBundler (true )
105
+ b .on (' update' , function update () {
106
+ bundle (b)
107
+ return update
108
+ }())
109
+ b .on (' close' , cb)
110
+ })
157
111
158
112
```
159
113
160
- ** Common shared bundles**
161
-
162
- Check this [ example] ( example/without-gulp/multi.js ) .
163
-
164
- ## Work with Gulp
165
- Check this [ gulpfile] ( example/gulp/multi/gulpfile.js ) .
166
-
167
114
## API
168
115
169
- ``` javascript
170
- const reduce = require (' reduce-css' )
116
+ ``` js
117
+ var reduce = require (' reduce-css' )
118
+ var b = reduce .create (entries, depsifyOptions, bundleOptions, watchifyOptions)
171
119
172
120
```
173
121
174
- ### reduce.create(opts )
122
+ ### reduce.create(entries, depsifyOptions, bundleOptions, watchifyOptions )
175
123
Return a [ ` depsify ` ] instance.
176
124
177
- ` opts ` is passed to the [ ` depsify ` ] constructor .
178
-
179
- If ` opts .postcss` is not ` false ` ,
125
+ * ` entries ` : patterns to locate input files. Check [ ` globby ` ] for more details .
126
+ * ` depsifyOptions ` : options for [ ` depsify ` ] .
127
+ If ` depsifyOptions .postcss` is not ` false ` ,
180
128
the plugin [ ` reduce-css-postcss ` ] for [ ` depsify ` ]
181
129
is applied, which use [ ` postcss ` ] to preprocess css.
130
+ * ` bundleOptions ` : options for [ ` common-bundle ` ] .
131
+ * ` watchifyOptions ` : options for [ ` watchify2 ` ] .
132
+ If specified, file changes are watched.
182
133
183
- ### reduce.bundle(b, opts)
184
- Return a transform:
185
- * input: [ ` vinyl-fs#src ` ]
186
- * output: ` b.bundle() `
187
-
188
- ** b**
189
-
190
- [ ` depsify ` ] instance.
191
-
192
- ** opts**
193
-
194
- Options passed to ` reduce.bundler ` .
195
-
196
- ### reduce.watch(b, opts, watchOpts)
197
- Return a transform:
198
- * input: [ ` vinyl-fs#src ` ] .
199
- * output: actually no data flows out,
200
- but you can listen to the ` bundle ` event (triggered on the returned transform)
201
- to process the result of ` b.bundle() ` .
202
-
203
- ` b ` and ` opts ` are the same with ` reduce.bundle(b, opts) `
204
-
205
- ** watchOpts**
206
-
207
- Options passed to [ ` watchify2 ` ] .
208
-
209
- To detect new entries,
210
- provide a glob to detect entries as ` watchOpts.entryGlob ` .
211
-
212
- ### reduce.src(patterns, opts)
213
- Same with [ ` vinyl-fs#src ` ] , except that ` opts.read ` defaults to ` false ` .
134
+ ### b.bundle()
135
+ Return a [ ` vinyl ` ] stream,
136
+ which can be processed by gulp plugins.
214
137
215
- ### reduce.dest(outFolder, opts, urlOpts)
216
- ` outFolder ` and ` opts ` are passed to [ ` vinyl-fs#dest ` ] directly.
217
-
218
- [ ` postcss-custom-url ` ] is used to transform ` url() ` expressions in css (paths transformed, assets copied or inlined).
219
-
220
- The actual processor is constructed as:
221
138
``` js
222
- const url = require (' postcss-custom-url' )
223
- const postcss = require (' postcss' )
224
- const urlProcessor = postcss (url ([
225
- [ url .util .inline , urlOpts ],
226
- [ url .util .copy , urlOpts ],
227
- ]))
139
+ b .bundle ().pipe (require (' gulp-uglifycss' )()).pipe (b .dest (' build' ))
228
140
229
141
```
230
142
231
- ### reduce.bundler(b, opts)
232
- Plugin for creating common shared bundles.
233
-
234
- ** opts**
235
-
236
- Default: ` {} `
143
+ ### b.dest(outFolder, urlTransformOptions)
144
+ Works almost the same with [ ` gulp.dest ` ] ,
145
+ except that file contents are transformed using [ ` postcss-custom-url ` ]
146
+ before being written to disk.
237
147
238
- * ` Function ` or ` Array ` : ` b.plugin(opts) ` will be executed.
239
- Used to replace the default bundler [ ` common-bundle ` ] .
240
- * ` String ` : all modules are packed into a single bundle, with ` opts ` the file path.
241
- * otherwise: ` opts ` is passed to [ ` common-bundle ` ] directly .
148
+ ` urlTransformOptions ` is passed to both
149
+ the [ inline ] ( https://github.com/reducejs/postcss-custom-url#inline )
150
+ and [ copy ] ( https://github.com/reducejs/postcss-custom-url#copy )
151
+ transformers for [ ` postcss-custom-url ` ] .
242
152
153
+ The actual processor:
243
154
``` js
244
- const reduce = require (' reduce-css' )
245
- const path = require (' path' )
246
-
247
- const b = reduce .create ({
248
- entries: [' a.css' , ' b.css' ],
249
- basedir: ' /path/to/src' ,
250
- })
251
- b .plugin (reduce .bundler , ' bundle.css' )
252
- b .bundle ().pipe (reduce .dest (' build' ))
253
-
254
- ```
255
-
256
- ### reduce.watcher(b, opts)
257
- Plugin for watching file changes, addition and deletion.
258
-
259
- ` opts ` is passed to [ ` watchify2 ` ] directly.
260
-
261
- A ` bundle-stream ` event is triggered whenever ` b.bundle() ` is provoked.
262
-
263
- ``` js
264
- const reduce = require (' reduce-css' )
265
- const path = require (' path' )
266
- const b = reduce .create ({
267
- entries: [' a.css' , ' b.css' ],
268
- basedir: ' /path/to/src' ,
269
- cache: {},
270
- packageCache: {},
271
- })
272
- b .plugin (reduce .bundler , ' bundle.css' )
273
- b .plugin (reduce .watcher , { entryGlob: ' *.css' })
274
- b .on (' bundle-stream' , function (bundleStream ) {
275
- // bundleStream is the result of `b.bundle()`
276
- bundleStream .pipe (reduce .dest (' build' ))
277
- })
278
- b .start ()
155
+ var url = require (' postcss-custom-url' )
156
+ var postcss = require (' postcss' )
157
+ var urlProcessor = postcss (url ([
158
+ [ url .util .inline , urlTransformOptions ],
159
+ [ url .util .copy , urlTransformOptions ],
160
+ ]))
279
161
280
162
```
281
163
@@ -293,6 +175,7 @@ b.start()
293
175
[ `gulp` ] : https://www.npmjs.com/package/gulp
294
176
[ `watchify2` ] : https://github.com/reducejs/watchify2
295
177
[ `postcss-custom-url` ] : https://github.com/reducejs/postcss-custom-url
178
+ [ `vinyl` ] : https://github.com/gulpjs/vinyl
296
179
[ `vinyl-fs#src` ] : https://github.com/gulpjs/vinyl-fs#srcglobs-options
297
- [ `vinyl-fs# dest` ] : https://github.com/gulpjs/vinyl-fs#destfolder-options
298
- [ `factor-bundle ` ] : https://www.npmjs. com/package/factor-bundle
180
+ [ `gulp. dest` ] : https://github.com/gulpjs/vinyl-fs#destfolder-options
181
+ [ `globby ` ] : https://github. com/sindresorhus/globby
0 commit comments