Exemplo n.º 1
0
  /**
   * The constructor
   *
   * @param config the jawr config
   */
  public EhCacheManager(JawrConfig config) {

    super(config);
    String configPath = config.getProperty(JAWR_EHCACHE_CONFIG_PATH, DEFAULT_EHCACHE_CONFIG_PATH);
    String cacheName = config.getProperty(JAWR_EHCACHE_CACHE_NAME);
    try {
      CacheManager cacheMgr =
          CacheManager.create(ClassLoaderResourceUtils.getResourceAsStream(configPath, this));
      cache = cacheMgr.getCache(cacheName);

    } catch (Exception e) {
      LOGGER.error("Unable to load EHCACHE configuration file", e);
    }
  }
  /** Initialize the postprocessor */
  private void initialize(JawrConfig config) {

    StopWatch stopWatch = new StopWatch("Initializing JS engine for Autoprefixer");
    stopWatch.start();

    // Load JavaScript Script Engine
    String script =
        config.getProperty(AUTOPREFIXER_SCRIPT_LOCATION, AUTOPREFIXER_SCRIPT_DEFAULT_LOCATION);
    String jsEngineName = config.getJavascriptEngineName(AUTOPREFIXER_JS_ENGINE);
    jsEngine = new JavascriptEngine(jsEngineName, true);
    jsEngine.getBindings().put("logger", PERF_LOGGER);
    InputStream inputStream = getResourceInputStream(config, script);
    jsEngine.evaluate("autoprefixer.js", inputStream);
    String strOptions =
        config.getProperty(AUTOPREFIXER_SCRIPT_OPTIONS, AUTOPREFIXER_DEFAULT_OPTIONS);
    this.options = jsEngine.execEval(strOptions);

    jsEngine.evaluate(
        "initAutoPrefixer.js", String.format("processor = autoprefixer(%s);", strOptions));
    jsEngine.evaluate(
        "jawrAutoPrefixerProcess.js",
        String.format(
            "function process(cssSource, opts){"
                + "var result = processor.process(cssSource, opts);"
                + "if(result.warnings){"
                + "result.warnings().forEach(function(message){"
                + "if(logger.isWarnEnabled()){"
                + "logger.warn(message.toString());"
                + "}"
                + "});}"
                + "return result.css;"
                + "}"));

    stopWatch.stop();
    if (PERF_LOGGER.isDebugEnabled()) {
      PERF_LOGGER.debug(stopWatch.shortSummary());
    }
  }
  @Test
  public void testPostProcessWithOutputOptions() throws Exception {

    String src = FileUtils.readClassPathFile("postprocessor/js/uglify/simpleJS.js");
    StringBuffer sb = new StringBuffer(src);
    when(config.getProperty(JawrConstant.UGLIFY_POSTPROCESSOR_OPTIONS, "{}"))
        .thenReturn("{output : {comments : /@preserve/ }}");
    BundleProcessingStatus status =
        new BundleProcessingStatus(
            BundleProcessingStatus.BUNDLE_PROCESSING_TYPE, bundle, null, config);
    StringBuffer ret = processor.postProcessBundle(status, sb);

    String expected =
        FileUtils.readClassPathFile(
            "postprocessor/js/uglify/simpleJS_WithOutputOptions_expected.js");
    assertEquals(expected, ret.toString());
  }