@@ -18,264 +18,143 @@ 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
+
28
+ bundle (createBundler ())
29
+
30
+ function createBundler (watch ) {
31
+ var basedir = path .join (__dirname , ' src' )
32
+ var b = reduce .create (
33
+ /* glob for entries */
34
+ ' *.css' ,
35
+
36
+ /* options for depsify */
37
+ {
38
+ basedir,
39
+ cache: {},
40
+ packageCache: {},
41
+ },
42
+
43
+ /* options for common-bundle */
44
+ // single bundle
45
+ // 'bundle.css',
46
+ // multiple bundles
47
+ {
48
+ groups: ' *.css' ,
49
+ common: ' common.css' ,
50
+ },
51
+
52
+ /* options for watchify2 */
53
+ watch && { entryGlob: ' *.css' }
54
+ )
55
+ return b
33
56
}
34
57
35
- ```
36
-
37
- ` red.css ` :
38
- ``` css
39
- @external "reset";
40
- @external "./button";
41
- @import " color " ;
42
- .red {
43
- color : $ red ;
58
+ function bundle ( b ) {
59
+ var build = path . join ( __dirname , ' build ' )
60
+ del . sync (build)
61
+ return b . bundle (). on ( ' error ' , log)
62
+ . pipe ( b . dest (build, {
63
+ maxSize : 0 ,
64
+ name : ' [name].[hash] ' ,
65
+ assetOutFolder : path . join (build, ' assets ' ),
66
+ }))
44
67
}
45
68
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 ;
69
+ function log () {
70
+ console .log .apply (console , [].map .call (arguments , function (msg ) {
71
+ if (typeof msg === ' string' ) {
72
+ return msg
73
+ }
74
+ return JSON .stringify (msg, null , 2 )
75
+ }))
55
76
}
56
77
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
78
66
79
```
67
80
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**
81
+ To watch file changes:
84
82
85
83
``` 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
- }
84
+ var b = createBundler (true )
85
+ b .on (' update' , function update () {
86
+ bundle (b)
87
+ return update
88
+ }())
123
89
124
90
```
125
91
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:
92
+ To work with gulp:
131
93
132
94
``` 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: {},
95
+ var gulp = require (' gulp' )
96
+ gulp .task (' build' , function () {
97
+ return bundle (createBundler ())
143
98
})
144
99
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
-
100
+ gulp .task (' watch' , function (cb ) {
101
+ var b = createBundler (true )
102
+ b .on (' update' , function update () {
103
+ bundle (b)
104
+ return update
105
+ }())
106
+ b .on (' close' , cb)
107
+ })
157
108
158
109
```
159
110
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
111
## API
168
112
169
- ``` javascript
170
- const reduce = require (' reduce-css' )
113
+ ``` js
114
+ var reduce = require (' reduce-css' )
115
+ var b = reduce .create (entries, depsifyOptions, bundleOptions, watchifyOptions)
171
116
172
117
```
173
118
174
- ### reduce.create(opts )
119
+ ### reduce.create(entries, depsifyOptions, bundleOptions, watchifyOptions )
175
120
Return a [ ` depsify ` ] instance.
176
121
177
- ` opts ` is passed to the [ ` depsify ` ] constructor .
178
-
179
- If ` opts .postcss` is not ` false ` ,
122
+ * ` entries ` : patterns to locate input files. Check [ ` globby ` ] for more details .
123
+ * ` depsifyOptions ` : options for [ ` depsify ` ] .
124
+ If ` depsifyOptions .postcss` is not ` false ` ,
180
125
the plugin [ ` reduce-css-postcss ` ] for [ ` depsify ` ]
181
126
is applied, which use [ ` postcss ` ] to preprocess css.
127
+ * ` bundleOptions ` : options for [ ` common-bundle ` ] .
128
+ * ` watchifyOptions ` : options for [ ` watchify2 ` ] .
129
+ If specified, file changes are watched.
182
130
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 ` .
131
+ ### b.bundle()
132
+ Return a [ ` vinyl ` ] stream,
133
+ which can be processed by gulp plugins.
214
134
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
135
``` 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
- ]))
136
+ b .bundle ().pipe (require (' gulp-uglifycss' )()).pipe (b .dest (' build' ))
228
137
229
138
```
230
139
231
- ### reduce.bundler(b, opts)
232
- Plugin for creating common shared bundles.
233
-
234
- ** opts**
235
-
236
- Default: ` {} `
140
+ ### b.dest(outFolder, urlTransformOptions)
141
+ Works almost the same with [ ` gulp.dest ` ] ,
142
+ except that file contents are transformed using [ ` postcss-custom-url ` ]
143
+ before written to disk.
237
144
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 .
145
+ ` urlTransformOptions ` is passed to both
146
+ the [ inline ] ( https://github.com/reducejs/postcss-custom-url#inline )
147
+ and [ copy ] ( https://github.com/reducejs/postcss-custom-url#copy )
148
+ transformers for [ ` postcss-custom-url ` ] .
242
149
150
+ The actual processor:
243
151
``` 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 ()
152
+ var url = require (' postcss-custom-url' )
153
+ var postcss = require (' postcss' )
154
+ var urlProcessor = postcss (url ([
155
+ [ url .util .inline , urlTransformOptions ],
156
+ [ url .util .copy , urlTransformOptions ],
157
+ ]))
279
158
280
159
```
281
160
@@ -293,6 +172,7 @@ b.start()
293
172
[ `gulp` ] : https://www.npmjs.com/package/gulp
294
173
[ `watchify2` ] : https://github.com/reducejs/watchify2
295
174
[ `postcss-custom-url` ] : https://github.com/reducejs/postcss-custom-url
175
+ [ `vinyl` ] : https://github.com/gulpjs/vinyl
296
176
[ `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
177
+ [ `gulp. dest` ] : https://github.com/gulpjs/vinyl-fs#destfolder-options
178
+ [ `globby ` ] : https://github. com/sindresorhus/globby
0 commit comments