Skip to content

Commit da5c6b3

Browse files
committed
Now has PhantomJS alternative (slightly faster, though without JS execution)
2 parents aa4ff4a + 094e08b commit da5c6b3

File tree

9 files changed

+63
-1552
lines changed

9 files changed

+63
-1552
lines changed

lib/domino-chains/extract.coffee

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ getElementAttributes = (el, ignore...) ->
1616

1717
module.exports = exports =
1818
extractStylesheets: (window, document, options, next) ->
19-
options.cout "Extracting stylesheets"
19+
options.cout "Extracting stylesheets from page"
20+
21+
media = _.union(["", "all", "screen"], options.cssMedia)
2022
links = document.querySelectorAll("link[rel='stylesheet']")
2123
sheets = []
2224

@@ -36,6 +38,8 @@ module.exports = exports =
3638

3739
urlParsed = url.parse(options.url)
3840

41+
console.log sheets
42+
3943
sheets = sheets.filter (sheet) ->
4044
_.every options.ignoreSheets, (ignore) ->
4145
if ignore instanceof RegExp and ignore.test(sheet.href)
@@ -44,12 +48,11 @@ module.exports = exports =
4448

4549
return sheet.href isnt ignore
4650

47-
if options.ignoreExternalSheets
48-
if urlParsed.hostname isnt url.parse(sheet.href).hostname
49-
options.cout "|> Ignoring stylesheet (#{sheet.href}) because external stylesheets are ignored"
50-
return no
51+
if options.ignoreExternalSheets and urlParsed.hostname isnt url.parse(sheet.href).hostname
52+
options.cout "|> Ignoring stylesheet (#{sheet.href}) because external stylesheets are ignored"
53+
return no
5154

52-
return true
55+
return yes
5356

5457
sheets = sheets.filter((sheet) -> return media.indexOf(sheet.media) isnt -1)
5558

lib/domino-chains/return.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module.exports = exports =
33
return next(null) if not options.cssExpose or typeof options.cssExpose isnt "string"
44

55
name = options.cssExpose
6-
6+
77
script = document.createElement("script")
8-
script.innerHTML = "#{name} = #{JSON.stringify(stylesheets)};"
8+
script.innerHTML = "#{name} = #{JSON.stringify(sheets)};"
99
script.innerHTML = "var " + script.innerHTML if name.indexOf(".") < 0
1010
document.getElementsByTagName("body")[0].appendChild(script)
1111

lib/inliner.coffee

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
async = require("async")
2-
_ = require("underscore")
2+
_ = require("lodash")
33
Phantom = require("./phantom")
44
domino = require("domino")
55
request = require("request")
66

77
defaultOptions =
88
# Core
9-
log: no
9+
log: no
1010

1111
# CSS
12-
cssMedia: ""
13-
cssMinify: no
14-
cssOnly: no
15-
cssId: no
16-
cssExpose: no
12+
cssMedia: ""
13+
cssMinify: no
14+
cssOnly: no
15+
cssId: no
16+
cssExpose: no
1717

18-
# Experimental
19-
useDomino: no
18+
19+
# Ignore
20+
ignoreSheets: []
21+
ignoreSelectors: []
22+
ignoreExternalSheets: no
2023

2124
# Miscellaneous
22-
ignoreSheets: []
23-
ignoreSelectors: []
24-
# ...
25-
userAgent: "CSS Inliner for node.js by Nicolai Persson"
25+
userAgent: "CSS Inliner for node.js by Nicolai Persson"
26+
# !! Experimental
27+
useDomino: no
2628

2729
module.exports = exports = (options = {}, cb) ->
2830
options = _.extend(defaultOptions, options)
2931

32+
# Validate options
3033
if not options?.url?
3134
cb("The URL option is required both for 'faking' (i.e. for providing the HTML) and loading an actual website")
32-
return
35+
return false
36+
37+
for attribute of options
38+
switch attribute
39+
when "log", "cssOnly", "ignoreExternalSheets"
40+
if not _.isBoolean(options[attribute])
41+
cb('Options type error: "log", "cssOnly", "ignoreExternalSheets" must be booleans.')
42+
return false
43+
when "ignoreSheets", "ignoreSelectors"
44+
if not _.isArray(options[attribute])
45+
cb('Options type error: "ignoreSheets", "ignoreSelectors" must be arrays.')
46+
return false
3347

3448
if options.log
3549
console.log "Inlining #{options.url}", if options.html then "(fake URL with content)" else ""
@@ -63,7 +77,7 @@ module.exports = exports = (options = {}, cb) ->
6377
return cb(error) if error
6478

6579
if options.cssOnly
66-
cb(null, usedCss)
80+
cb(null, finalCSS)
6781
else
6882
returnChain = require("./phantom-chains/return")
6983

@@ -86,7 +100,7 @@ module.exports = exports = (options = {}, cb) ->
86100
return cb(error) if error
87101

88102
if options.cssOnly
89-
cb(null, usedCss)
103+
cb(null, finalCSS)
90104
else
91105
returnChain = require("./domino-chains/return")
92106

@@ -100,4 +114,4 @@ module.exports = exports = (options = {}, cb) ->
100114
return cb(error) if error
101115
proceed(body)
102116
else
103-
proceed(options.html)
117+
proceed(options.html)

lib/phantom-chains/extract.coffee

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
async = require("async")
2-
request = require("request")
3-
css = require("css")
4-
CleanCSS = require("clean-css")
5-
_ = require("underscore")
6-
7-
8-
url = require("url")
1+
async = require("async")
2+
request = require("request")
3+
css = require("css")
4+
CleanCSS = require("clean-css")
5+
_ = require("lodash")
6+
url = require("url")
97

108
module.exports = exports =
119
loadPageContent: (page, options, next) ->
@@ -47,6 +45,8 @@ module.exports = exports =
4745
stylesheets ||= []
4846

4947
# Loop through ignore list
48+
urlParsed = url.parse(options.url)
49+
5050
stylesheets = stylesheets.filter (sheet) ->
5151
_.every options.ignoreSheets, (ignore) ->
5252
if ignore instanceof RegExp and ignore.test(sheet.href)
@@ -55,14 +55,11 @@ module.exports = exports =
5555

5656
return sheet.href isnt ignore
5757

58-
if options.ignoreExternalSheets
59-
urlParsed = url.parse(options.url)
60-
sheetUrlParsed = url.parse(sheet.href)
61-
if urlParsed.hostname isnt sheetUrlParsed.hostname
62-
options.cout "|> Ignoring stylesheet (#{sheet.href}) because external stylesheets are ignored"
63-
return no
64-
65-
return true
58+
if options.ignoreExternalSheets and urlParsed.hostname isnt url.parse(sheet.href).hostname
59+
options.cout "|> Ignoring stylesheet (#{sheet.href}) because external stylesheets are ignored"
60+
return no
61+
62+
return yes
6663

6764
stylesheets = stylesheets.filter((sheet) -> return media.indexOf(sheet.media) isnt -1)
6865

@@ -81,7 +78,7 @@ module.exports = exports =
8178
mapFn = (sheet, done) ->
8279
requestOptions =
8380
headers:
84-
"User-Agent": "CSS Inliner for node.js"
81+
"User-Agent": options.userAgent
8582

8683
request sheet.href, requestOptions, (error, response, body) ->
8784
if error
@@ -152,5 +149,5 @@ module.exports = exports =
152149

153150
next(null, stylesheets, finalCSS)
154151
catch e
155-
next(error)
152+
next(e.toString())
156153

lib/phantom-chains/filter.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
async = require("async")
22
css = require("css")
3-
_ = require("underscore")
3+
_ = require("lodash")
44

55
dePseudify = (->
66
ignoredPseudos = [

lib/phantom-chains/return.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module.exports = exports =
44

55
evalFn = (name, stylesheets) ->
66
script = document.createElement("script")
7-
script.innerHTML = "var #{name} = #{JSON.stringify(stylesheets)};"
7+
script.innerHTML = "#{name} = #{JSON.stringify(stylesheets)};"
8+
script.innerHTML = "var " + script.innerHTML if name.indexOf(".") < 0
89
return document.getElementsByTagName("body")[0].appendChild(script)
910

1011
resultFn = (error) ->
1112
next(error)
1213

1314
page.evaluate(evalFn, resultFn, options.cssExpose, stylesheets)
1415

16+
1517
removeStylesheetsAndInjectUsedStyles: (page, options, stylesheets, finalCSS, next) ->
1618
evalFn = (options, stylesheets, finalCSS) ->
1719
links = document.querySelectorAll("link[rel='stylesheet']")

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"css": "~1.6.0",
3737
"domino": "^1.0.17",
3838
"generic-pool": "~2.0.4",
39+
"lodash": "^2.4.1",
3940
"node-phantom-simple": "~1.2.0",
4041
"phantomjs": "~1.9.7-5",
41-
"request": "~2.34.0",
42-
"underscore": "~1.6.0"
42+
"request": "~2.34.0"
4343
}
4444
}

test.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)