Skip to content

Commit 719ed2c

Browse files
authored
new 'url' option (#390)
1 parent fe10a9f commit 719ed2c

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ For example:
136136

137137
```javascript
138138
minimalcss
139-
.minimize({ urls: ['https://example.com/'] })
139+
.minimize({ url: 'https://example.com/' })
140140
.then(result => {
141141
console.log('OUTPUT', result.finalCss.length, result.finalCss);
142142
})
@@ -151,10 +151,21 @@ That `result` object that is returned by the `minimize` function contains:
151151
* `stylesheetContents` - an object of stylesheet URLs as keys and their
152152
content as text.
153153

154+
Optionally, you can supply a list of URLs like this:
155+
156+
```javascript
157+
minimalcss
158+
.minimize({ urls: ['https://example.com/page1', 'https://example.com/page2'] })
159+
...
160+
```
161+
162+
and `minimalcss` will try to merge the minimal critical CSS across all pages.
163+
But we aware that this can be "dangerous" because of the inherit order of CSS.
164+
154165
## API Options
155166

156167
Calling `minimalcss.run(options)` takes an object whose only mandatory
157-
key is `urls`. Other optional options are:
168+
key is `urls` or `url`. Other optional options are:
158169

159170
* `debug` - all console logging during page rendering are included in the
160171
stdout. Also, any malformed selector that cause errors in `document.querySelector`
@@ -215,7 +226,7 @@ With the API, you can do it like this:
215226
```javascript
216227
minimalcss
217228
.minimize({
218-
urls: ['https://example.com'],
229+
url: 'https://example.com',
219230
skippable: request => {
220231
return !!request.url().match('fonts.googleapis.com');
221232
}
@@ -248,9 +259,9 @@ and another URL with...:
248259

249260
When combining these, it will optimize the CSS in this order:
250261

251-
1. `base.css`
252-
2. `specific.css`
253-
3. `base.css`
262+
1. `base.css`
263+
2. `specific.css`
264+
3. `base.css`
254265

255266
But if `specific.css` was meant to override something in `base.css` in the
256267
first URL, that might get undone when `base.css` becomes the last CSS

src/run.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,19 @@ const processPage = ({
409409

410410
/**
411411
*
412-
* @param {{ urls: Array<string>, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array<string>, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array<string>, ignoreRequestErrors?: boolean }} options
412+
* @param {{ urls: Array<string>, url: string, debug: boolean, loadimages: boolean, skippable: function, browser: any, userAgent: string, withoutjavascript: boolean, viewport: any, puppeteerArgs: Array<string>, cssoOptions: Object, ignoreCSSErrors?: boolean, ignoreJSErrors?: boolean, styletags?: boolean, enableServiceWorkers?: boolean, disableJavaScript?: boolean, whitelist?: Array<string>, ignoreRequestErrors?: boolean }} options
413413
* @return Promise<{ finalCss: string, stylesheetContents: { [key: string]: string }, doms: Array<object> }>
414414
*/
415415
const minimalcss = async (options) => {
416-
const { urls } = options;
416+
const { urls = [], url = '' } = options;
417+
if (url) {
418+
urls.push(url);
419+
}
420+
if (!urls.length) {
421+
throw new Error(
422+
"no URLs supplied (hint: use 'urls' (array) or 'url' (string)"
423+
);
424+
}
417425
const debug = options.debug || false;
418426
const cssoOptions = options.cssoOptions || {};
419427
const enableServiceWorkers = options.enableServiceWorkers || false;

tests/main.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let browser;
2828

2929
const runMinimalcss = (path, options = {}) => {
3030
options.browser = browser;
31-
options.urls = [`http://localhost:3000/${path}.html`];
31+
options.url = `http://localhost:3000/${path}.html`;
3232
return minimalcss.minimize(options);
3333
};
3434

0 commit comments

Comments
 (0)