Class: Condenser::PurgeCSSProcessor

Inherits:
NodeProcessor show all
Defined in:
lib/condenser/processors/purgecss_processor.rb

Instance Attribute Summary collapse

Attributes inherited from NodeProcessor

#npm_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeProcessor

#binary, #exec_runtime, #exec_runtime_error, #exec_syntax_error, #name, #npm_install, #npm_module_path, setup

Constructor Details

#initialize(dir = nil, options = {}) ⇒ PurgeCSSProcessor

Returns a new instance of PurgeCSSProcessor.



26
27
28
29
30
# File 'lib/condenser/processors/purgecss_processor.rb', line 26

def initialize(dir = nil, options = {})
  super(dir)
  @options = options
  npm_install('purgecss')
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/condenser/processors/purgecss_processor.rb', line 5

def options
  @options
end

Class Method Details

.call(environment, input) ⇒ Object

Public: initialize with custom options.

dir - String (path to node_modules directory) options - Hash

content - Array - html files to process
    ex. [File.expand_path('./docs-src/**/*.erb'), File.expand_path('./docs-src/assets/javascripts/**/*.js')]

Options are passed to PurgeCSS checkout [PurgeCSS Configurations](purgecss.com/configuration.html)



17
18
19
20
21
22
23
# File 'lib/condenser/processors/purgecss_processor.rb', line 17

def self.call(environment, input)
  @instances ||= {}
  @instances[environment] ||= new(environment.npm_path, {
    content: [File.join(environment.base, '**/*.html'), File.join(environment.base, '**/*.js')]
  })
  @instances[environment].call(environment, input)
end

Instance Method Details

#call(environment, input) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/condenser/processors/purgecss_processor.rb', line 32

def call(environment, input)
  result = exec_runtime("    const { PurgeCSS } = require(\"\#{File.join(npm_module_path('purgecss'))}\")\n    const options = \#{@options.to_json}\n    options.css = [{\n      raw: \#{JSON.dump(input[:source])}\n    }]\n    if(options.safelist) {\n      options.safelist = options.safelist.map(s => {\n        if(s[0] == \"/\" && s[s.length - 1] == \"/\") {\n          return new RegExp(s.slice(1, -1))\n        }\n        return s\n      })\n    }\n    if(!options.defaultExtractor) {\n      options.defaultExtractor = content => content.match(/[\\\\w\\\\-\\\\/\\\\:]+(?<!:)/g) || []\n    }\n    const result = new PurgeCSS().purge(options)\n    try {\n      result.then(\n        r => console.log(JSON.stringify({\n          success: r[0]\n        })),\n        function(e) {console.log(JSON.stringify({'error': [e.name, e.message, e.stack]}))}\n      )\n    } catch(e) {\n      console.log(JSON.stringify({'error': [e.name, e.message, e.stack]}));\n    }\n  JS\n  if result['error']\n    if result['error'][0] == 'SyntaxError'\n      raise exec_syntax_error(result['error'][1], \"/assets/\#{input[:filename]}\")\n    else\n      raise exec_runtime_error(result['error'][\"0\"][\"name\"] + \": \" + result['error'][\"0\"][\"reason\"])\n    end\n  else\n    input[:source] = result[\"success\"][\"css\"]\n  end\nend\n")