Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Asset/JsOptimizer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Asset; | |
4 | |
5 use Drupal\Component\Utility\Unicode; | |
6 | |
7 /** | |
8 * Optimizes a JavaScript asset. | |
9 */ | |
10 class JsOptimizer implements AssetOptimizerInterface { | |
11 | |
12 /** | |
13 * {@inheritdoc} | |
14 */ | |
15 public function optimize(array $js_asset) { | |
16 if ($js_asset['type'] !== 'file') { | |
17 throw new \Exception('Only file JavaScript assets can be optimized.'); | |
18 } | |
19 if (!$js_asset['preprocess']) { | |
20 throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.'); | |
21 } | |
22 | |
23 // If a BOM is found, convert the file to UTF-8, then use substr() to | |
24 // remove the BOM from the result. | |
25 $data = file_get_contents($js_asset['data']); | |
26 if ($encoding = (Unicode::encodingFromBOM($data))) { | |
27 $data = Unicode::substr(Unicode::convertToUtf8($data, $encoding), 1); | |
28 } | |
29 // If no BOM is found, check for the charset attribute. | |
30 elseif (isset($js_asset['attributes']['charset'])) { | |
31 $data = Unicode::convertToUtf8($data, $js_asset['attributes']['charset']); | |
32 } | |
33 | |
34 // No-op optimizer: no optimizations are applied to JavaScript assets. | |
35 return $data; | |
36 } | |
37 | |
38 /** | |
39 * Processes the contents of a javascript asset for cleanup. | |
40 * | |
41 * @param string $contents | |
42 * The contents of the javascript asset. | |
43 * | |
44 * @return string | |
45 * Contents of the javascript asset. | |
46 */ | |
47 public function clean($contents) { | |
48 // Remove JS source and source mapping urls or these may cause 404 errors. | |
49 $contents = preg_replace('/\/\/(#|@)\s(sourceURL|sourceMappingURL)=\s*(\S*?)\s*$/m', '', $contents); | |
50 | |
51 return $contents; | |
52 } | |
53 | |
54 } |