Class: Sprockets::DirectiveProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/directive_processor.rb

Overview

The `DirectiveProcessor` is responsible for parsing and evaluating directive comments in a source file.

A directive comment starts with a comment prefix, followed by an “=”, then the directive name, then any arguments.

// JavaScript
//= require "foo"

# CoffeeScript
#= require "bar"

/* CSS
 *= require "baz"
 */

This makes it possible to disable or modify the processor to do whatever you'd like. You could add your own custom directives or invent your own directive syntax.

`Environment#processors` includes `DirectiveProcessor` by default.

To remove the processor entirely:

env.unregister_processor('text/css', Sprockets::DirectiveProcessor)
env.unregister_processor('application/javascript', Sprockets::DirectiveProcessor)

Then inject your own preprocessor:

env.register_processor('text/css', MyProcessor)

Constant Summary collapse

DIRECTIVE_PATTERN =

Directives are denoted by a `=` followed by the name, then argument list.

A few different styles are allowed:

// =require foo
//= require foo
//= require "foo"
/
  ^ \W* = \s* (\w+.*?) (\*\/)? $
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comments: []) ⇒ DirectiveProcessor


60
61
62
# File 'lib/sprockets/directive_processor.rb', line 60

def initialize(comments: [])
  @header_pattern = compile_header_pattern(Array(comments))
end

Class Method Details

.call(input) ⇒ Object


56
57
58
# File 'lib/sprockets/directive_processor.rb', line 56

def self.call(input)
  instance.call(input)
end

.instanceObject


51
52
53
54
# File 'lib/sprockets/directive_processor.rb', line 51

def self.instance  # Default to C comment styles

  @instance ||= new(comments: ["//", ["/*", "*/"]])
end

Instance Method Details

#_call(input) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sprockets/directive_processor.rb', line 68

def _call(input)
  @environment  = input[:environment]
  @uri          = input[:uri]
  @filename     = input[:filename]
  @dirname      = File.dirname(@filename)  # If loading a source map file like `application.js.map` resolve
  # dependencies using `.js` instead of `.js.map`

  @content_type = SourceMapProcessor.original_content_type(input[:content_type], error_when_not_found: false)
  @required     = Set.new(input[:metadata][:required])
  @stubbed      = Set.new(input[:metadata][:stubbed])
  @links        = Set.new(input[:metadata][:links])
  @dependencies = Set.new(input[:metadata][:dependencies])
  @to_link      = Set.new
  @to_load      = Set.new

  data, directives = process_source(input[:data])
  process_directives(directives)

  {
    data:         data,
    required:     @required,
    stubbed:      @stubbed,
    links:        @links,
    to_load:      @to_load,
    to_link:      @to_link,
    dependencies: @dependencies
  }
end

#call(input) ⇒ Object


64
65
66
# File 'lib/sprockets/directive_processor.rb', line 64

def call(input)
  dup._call(input)
end