'/', '.' => '\\.', )); $exclude_pattern = preg_split('/\r?\n/', $exclude_pattern, -1, PREG_SPLIT_NO_EMPTY); $exclude_pattern = '~'.implode("$|", $exclude_pattern).'$~'; } } // One regular expression to extract and remove .js paths and filenames from $scripts variable $pattern = "!(\n"; } // Reconstruct the scripts variable, making sure to add the aggregated files at the beginning. $scripts = trim($script_links . $scripts); } return $scripts; } /** * Delete all cached JS files. */ function javascript_aggregator_clear_cache() { $success = file_scan_directory(file_create_path('js'), '.*', array('.', '..', 'CVS'), 'file_delete', FALSE); if ($success) { drupal_set_message(t('Javascript cache cleared.'), $type = 'status'); } else { drupal_set_message(t('Javascript cache could not be cleared. Or already empty.'), $type = 'error'); } drupal_goto('admin/settings/performance'); } /** * Implementation of hook_form_alter(). * * Adds the configuration stuff to admin/settings/performance. Inspired by http://drupal.org/node/149402 */ function javascript_aggregator_form_alter($form_id, &$form) { if ($form_id == 'system_performance_settings') { $directory = file_directory_path(); $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); $form['javascript_aggregation'] = array( '#type' => 'fieldset', '#title' => t('Javascript Aggregation'), '#description' => t('
The Javascript Aggregator Module aggregates javascript files into a single cached file. This will help to reduce the number of requests made to your webserver on every pageload, reducing server load and average page loading time.
To enable this feature, make sure you have set up your files directory correctly and your download method is not set to private.
Click here to clear the javascript cache.
NOTICE: If you clear the cache while Javascript Aggregation is enabled, your files/js directory will NOT be empty because a new file is instantly created.
', array('@clearjscache' => url('clearjscache'))), '#weight' => 0 ); $form['javascript_aggregation']['javascript_aggregator_aggregate_js'] = array( '#type' => 'radios', '#title' => t('Aggregate JavaScript files'), '#default_value' => variable_get('javascript_aggregator_aggregate_js', 0), '#disabled' => !$is_writable, '#options' => array(t('Disabled'), t('Enabled')), '#description' => t("This option can interfere with module development. It is recommended to only turn this on when your site is complete."), ); $form['javascript_aggregation']['javascript_aggregator_jsmin'] = array( '#type' => 'checkbox', '#title' => t('Minify with JSMin'), '#default_value' => variable_get('javascript_aggregator_jsmin', FALSE), '#description' => t('When enabled, will use the JSMin library to compress the aggregated JavaScript file.', array('@jsmin' => 'http://code.google.com/p/jsmin-php/')), ); $form['javascript_aggregation']['javascript_aggregator_gzip'] = array( '#type' => 'checkbox', '#title' => t('GZip JavaScript'), '#default_value' => variable_get('javascript_aggregator_gzip', FALSE), '#description' => t('Once aggregated, optionally GZip the JavaScript to dramatically decrease its size.', array('@gzip' => 'http://en.wikipedia.org/wiki/Gzip')), ); $form['javascript_aggregation']['javascript_aggregator_exclude_js'] = array( '#type' => 'textarea', '#title' => t('Exclude from js aggregation'), '#default_value' => variable_get('javascript_aggregator_exclude_js', ''), '#disabled' => !$is_writable, '#description' => t('Enter one js file per line that should be excluded from js aggregation. Check your HTML source for paths. TinyMCE Example: /sites/all/modules/tinymce/tinymce/jscripts/tiny_mce/tiny_mce.js Partial paths are also possible, this does the same tiny_mce.js'), ); $form['javascript_aggregation']['javascript_aggregator_base_path'] = array( '#type' => 'textfield', '#title' => t('Advanced: Base URL for javascript aggregated files'), '#default_value' => variable_get('javascript_aggregator_base_path', ''), '#description' => t('If you would like to serve your JS files from a different server, like one dedicated for static files (usually images and JS files), add its URL here, like http://images.example.com/. Make sure it ends with a slash. Leave empty if you want JS files to be served from the same server and directory as they are created in'), ); } } /** * Implementation of hook_menu(). * * Adds a callback mapped on the clear_cache function. */ function javascript_aggregator_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'clearjscache', 'callback' => 'javascript_aggregator_clear_cache', 'access' => user_access('administer site configuration'), 'type' => MENU_CALLBACK ); } return $items; }