public class CSSModuleBuilder extends TextModuleBuilder implements IExtensionInitializer, IExtensionSingleton, IShutdownListener, IConfigListener
getContentReader(String, com.ibm.jaggr.core.resource.IResource, javax.servlet.http.HttpServletRequest, java.util.List)
.
Removes comments identified by /* ... */ comment tags
Removes carriage returns, line-feeds, tabs and multiple spaces, replacing them with a single space character where necessary. It will also remove unneeded tokens like trailing semi-colons and quotes surrounding URLs.
@import statements will be replaced with the contents of the imported CSS. In order for this to work, the @import URLs must be relative, and the must be locatable on the server relative to the location of the importing CSS. Non-relative URLs may be used in @import statements but their use is severely restricted by CSS rules which require that all @import statements be ahead of any styles in a document. Since in-lining an @import replaces it with the contents of the imported style-sheet, any in-lined style sheets which contain style definitions will cause subsequent @import statements which don't get in-lined to be ignored.
Relative URLs in imported CSS files will be rewritten to make them relative to the location of the top level CSS module that is being requested.
In-lining of @import statements is controlled by the following property specified in the server-side AMD config:
INLINEIMPORTS_CONFIGPARAM
- If true, then @import statements will be inlined. The default value is true.
Foo Image URLs in CSS can optionally be in-lined, replacing the URL with the base64 encoded contents of the image using the Data URI scheme. In-lining of image URLs by is controlled by the following properties specified in the server-side AMD config:
SIZETHRESHOLD_CONFIGPARAM
- This parameter specifies the maximum size of an image that may be in-lined. Images larger than this size will not be in-lined. The size is specified in bytes. The default size is 0
IMAGETYPES_CONFIGPARAM
- A comma delimited list of mime types that may be in-lined. The standard image types (image/gif, image/png, image/jpeg and image/tiff) do not need to be specified. If you want to in-line any other image types, you can add the mime type to this list.
EXCLUDELIST_CONFIGPARAM
- A comma delimited list of filespecs to be excluded from in-lining. The filespec may contain the wildcard characters * and ?. If the filespec does not contain the path separator character '/', then the filespec is matched against the filename portion of image resource only. If the filespec does contain a path separator, then it is matched against the full path of the image resource on the server. Note that * and ? will match the path separator so, for example, the filespec */foo/bar.css will match any path that ends with /foo/bar.css. Any image resource that matches one of these filespecs will not be in-lined.
INCLUDELIST_CONFIGPARAM
- A comma delimited list of filespecs to be in-lined. Resources that match these filespecs will be in-lined, even if they don't meet any of the other qualifications for in-lining, and no other files will be inlined (i.e. the include list is a white-list). Filespec matching rules are the same as for inlinedImageExcludeList.
Plugins are configured in the Server-side AMD JavaScript config using an array
of two element arrays as in the following example:
postcss: {
plugins: [
[
'autoprefixer', // Name of the plugin resource
// Can be an AMD module id or an absolute URI to a server resource
function(autoprefixer) {
return autoprefixer({browsers: '> 1%'}).postcss; } // the init function
}
]
}
],
IExtensionInitializer.IExtensionRegistrar
Modifier | Constructor and Description |
---|---|
|
CSSModuleBuilder() |
protected |
CSSModuleBuilder(IAggregator aggregator) |
Modifier and Type | Method and Description |
---|---|
void |
configLoaded(IConfig conf,
long sequence)
This method is called when the config is loaded/reloaded, after the
config has been modified by any registered
IConfigModifier
services. |
protected org.mozilla.javascript.Scriptable |
createThreadScope(org.mozilla.javascript.Context cx,
org.mozilla.javascript.Scriptable protoScope)
Creates a thread scope for the PostCSS processor and it's plugins.
|
String |
dequote(String in)
Removes single or double quotes from a quoted string.
|
protected String |
getBase64(InputStream in)
Returns a base64 encoded string representation of the contents of the
resource associated with the
URLConnection . |
List<ICacheKeyGenerator> |
getCacheKeyGenerators(IAggregator aggregator)
This method may be called, before
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
is called by a separate worker thread, to obtain a cache key generator
for this builder. |
protected Reader |
getContentReader(String mid,
IResource resource,
javax.servlet.http.HttpServletRequest request,
List<ICacheKeyGenerator> keyGens) |
protected BlockingQueue<org.mozilla.javascript.Scriptable> |
getThreadScopes()
Returns the thread scope queue
|
boolean |
handles(String mid,
IResource resource)
Returns true if this module builder handles the specified resource
|
void |
initialize(IAggregator aggregator,
IAggregatorExtension extension,
IExtensionInitializer.IExtensionRegistrar registrar)
Called immediately after the extension is created to set the aggregator
instance that this extension object was created for.
|
protected void |
initPostcss(IConfig config)
Initializes Rhino with PostCSS and configured plugins.
|
protected String |
inlineImageUrls(javax.servlet.http.HttpServletRequest req,
String css,
IResource res)
Replace
url(<relative-path>) references in the
input CSS with
url(data:<mime-type>;<base64-encoded-data>
). |
protected String |
inlineImports(javax.servlet.http.HttpServletRequest req,
String css,
IResource res,
String path)
Processes the input CSS to replace @import statements with the
contents of the imported CSS.
|
protected String |
postcss(String css,
IResource res)
Runs given CSS through PostCSS processor for minification and any other processing
by configured plugins
.
|
protected String |
readToString(Reader in)
Copies the contents of the specified
Reader to a String. |
void |
shutdown(IAggregator aggregator)
This method is called from within the aggregator's
GenericServlet.destroy() method to notify listeners that the servlet
is shutting down. |
protected Pattern |
toRegexp(String filespec)
Returns a regular expression for a filepath that can include standard
file system wildcard characters (e.g.
|
build, isScript
public static final String INLINEIMPORTS_CONFIGPARAM
public static final String INCLUDELIST_CONFIGPARAM
public static final String EXCLUDELIST_CONFIGPARAM
public static final String IMAGETYPES_CONFIGPARAM
public static final String SIZETHRESHOLD_CONFIGPARAM
public static final String INCLUDEAMDPATHS_CONFIGPARAM
public static final String POSTCSSPLUGINS_CONFIGPARAM
public static final String SCOPEPOOLSIZE_CONFIGPARAM
public static final String POSTCSS_CONFIGPARAM
public static final boolean INLINEIMPORTS_DEFAULT_VALUE
public static final boolean INCLUDEAMDPATHS_DEFAULT_VALUE
public static final int SIZETHRESHOLD_DEFAULT_VALUE
public static final String INLINEIMPORTS_REQPARAM_NAME
public static final String INLINEIMAGES_REQPARAM_NAME
public static final String PREAMBLE
public static final String BLANK
public static final String UTF8_CHARSET
public static final String POSTCSS_RES_NAME
public static final String MINIFYER_RES_NAME
public static final String POSTCSS
public static final String PROCESS
public static final String PLUGINS
public static final String POSTCSS_INSTANCE
public static final String MODULE
public static final String EXPORTS
public static final String AMD_DEFINE_SHIM_JS
public static final String MINIFIER_INITIALIZATION_VAR
public static final String MINIFIER_INITIALIZATION_JS
public static final int INITIALIZER_THREAD_POOL_SIZE
public static final int DEFAULT_SCOPE_POOL_SIZE
public static final int SCOPE_POOL_TIMEOUT_SECONDS
protected static final Pattern urlPattern
protected static final Pattern protocolPattern
protected static final Collection<String> s_inlineableImageTypes
protected static final List<ICacheKeyGenerator> s_cacheKeyGenerators
public int imageSizeThreshold
public boolean inlineImports
public boolean includeAMDPaths
public Collection<Pattern> inlinedImageExcludeList
public CSSModuleBuilder()
protected CSSModuleBuilder(IAggregator aggregator)
protected Reader getContentReader(String mid, IResource resource, javax.servlet.http.HttpServletRequest request, List<ICacheKeyGenerator> keyGens) throws IOException
getContentReader
in class TextModuleBuilder
IOException
protected String readToString(Reader in) throws IOException
Reader
to a String.in
- The input ReaderIOException
protected String inlineImports(javax.servlet.http.HttpServletRequest req, String css, IResource res, String path) throws IOException
req
- The request associated with the call.css
- The current CSS containing @import statements to be
processedres
- The resource for the CSS file.path
- The path, as specified in the @import statement used to
import the current CSS, or null if this is the top level CSS.IOException
protected String inlineImageUrls(javax.servlet.http.HttpServletRequest req, String css, IResource res)
url(<relative-path>)
references in the
input CSS with
url(data:<mime-type>;<base64-encoded-data>
). The conversion is controlled by option settings as described in
CSSModuleBuilder
.req
- The request associated with the call.css
- The input CSSres
- The resource for the CSS fileprotected String postcss(String css, IResource res) throws IOException
css
- res
- IOException
protected void initPostcss(IConfig config)
CSSModuleBuilder
for a description of the plugin config JavaScriptconfig
- The config objectIllegalArgumentException
- if any of the config parameters are not validRuntimeException
- for any other exception caught within this moduleprotected org.mozilla.javascript.Scriptable createThreadScope(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable protoScope)
cx
- the Rhino ContextprotoScope
- the parent scope objectprotected BlockingQueue<org.mozilla.javascript.Scriptable> getThreadScopes()
protected String getBase64(InputStream in) throws IOException
URLConnection
.in
- The input stream for the resourceIOException
protected Pattern toRegexp(String filespec)
filespec
- A filespec that can contain wildcardsfilespec
public final List<ICacheKeyGenerator> getCacheKeyGenerators(IAggregator aggregator)
IModuleBuilder
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
is called by a separate worker thread, to obtain a cache key generator
for this builder. If cache keys for this builder depend on module
content, then this method should return a provisional cache key
generator, which generates cache keys based on information in the request
only. Provisional cache keys may be more exclusive than non-provisional
cache keys in matching requests to module builds.
If this method returns a provisional cache key generator, then
IModuleBuilder.build(String, IResource, HttpServletRequest, List)
will be called with a null cache key generator list and that method
MUST return a
ModuleBuild
object with a new immutable list of non-provisional cache key
generators when subsequently called for the same request.
getCacheKeyGenerators
in interface IModuleBuilder
getCacheKeyGenerators
in class TextModuleBuilder
aggregator
- The aggregator instancepublic boolean handles(String mid, IResource resource)
IModuleBuilder
handles
in interface IModuleBuilder
handles
in class TextModuleBuilder
mid
- the module idresource
- the resource for the module idpublic void shutdown(IAggregator aggregator)
IShutdownListener
GenericServlet.destroy()
method to notify listeners that the servlet
is shutting down.shutdown
in interface IShutdownListener
aggregator
- The aggregator that is shutting downpublic void initialize(IAggregator aggregator, IAggregatorExtension extension, IExtensionInitializer.IExtensionRegistrar registrar)
IExtensionInitializer
initialize
in interface IExtensionInitializer
aggregator
- The aggregator that this extension was created for.extension
- The extension objectregistrar
- A registrar object that can be used to register additional
aggregator extendablespublic void configLoaded(IConfig conf, long sequence)
IConfigListener
IConfigModifier
services.configLoaded
in interface IConfigListener
conf
- The new config object. In order to detect and react to config
changes, you can save the value returned from
IConfig.toString()
and compare it with the value
returned in subsequent configLoaded events.sequence
- The sequence number. Notifications for different listener
events (options, config, dependencies) that have the same
cause have the same sequence number. Notifications resulting
from servlet initialization have the sequence number 1. The
sequence number is incremented for subsequent event causes,
but there is no guarantee about the the values for subsequent
notifications other than that they will be increasing.public String dequote(String in)
in
- The possibly quoted stringCopyright © 2011-2015 IBM. All Rights Reserved.