82. 资源解析器AssetResolver

龳龲龯龫龩龨龥龣龡龞龲龯龫龛龗龓龑龏龌 assets library 齸齴齲齮齸齪齧齴龑龏龌齦齥齡齠

齞齜齴齘齕齒齑齏龗龑龏龌齋齊齈齆齂齁龗 js 鼽鼼鼺 drupalSettings 鼩龣鼨鼧鼦鼣鼠

\Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAssetLibraries

齑齏龑龏黻鼠

龳龛黷齁齞齑齏龑龏黻黴黰黮黫黧黣齕齒龗龑龏龣黠黝黚龑龏齴齘黙默黔黓龗黴黰龣黐黍黋鼠

\Drupal\Core\Asset\AttachedAssets

黉黴黰黅黂麾麻麷麳鼠

龑龏龌鼠 $render_array['#attached']['library']

js 鼽鼼鼠 $render_array['#attached']['drupalSettings']

麡麠麝龥麛齕齒齏麙龗龌鼠麗黍龳 ajax 麔龣麡麠齏麙龗龌麒麏麎麋齏麙

龳麇黣龡麃麀鹼齁齑齏龗龑龏鹻鹺黻鹷黠鹵龣龳鹲鹱鹰鹯鹫鹧麔龣鹥鹰鹯黴黰齁鹢鹷龑龏鹞鹚鹙黅黂鹷黉黴黰龣鹕鹙鹓鹏鹋龑龏齴齘黙齮鹢鹷齦齥鹊鹆鹃龗麷麳

龑龏齴齘黙鼠

鹀鸾鸻鼠 asset.resolver

黐鼠 Drupal\Core\Asset\AssetResolver

黉鹀鸾鸴齞鸲黂鹙麛 cache.data 鸯鸲黂齦齥齆鸮鸪龩默鸦鸥鸤黋鸣

鸡鸠 css 黻鼠

黧黣齕齒龗鸝鸙 css 齮黍黋 cid 麝鸲黂鼠

$cid = 'css: 鸖鸒龩龨鸻 :' . Crypt::hashBase64(serialize($libraries_to_load)) . (int) 黚鸀鷾鷽 ;

鸲黂鷹鷶鷵鼠 ['library_info'] 龣鸲黂麷麳黚黧黣鷲鷮齏麙鷭鷪鷧鷥鷪龗齆齂龣黠鷣黚鷢鷠鷜默龗 css 黻齆齂龣黍鷙鷕鼼齥鷑齧鷾鷽龣鷎鷍鷋鸻黚 css 鷈黴鷄鷂鼺鸙麾鷀黐鶿鼠鶾鶺鷄鷂鶸鶶鶳齦黴 URI 鶩麀鷄鷂鶩鶦齦黴齜龛黷鶣鶢鶞龗鶛鸙鶚 / 鶔龥鶐龗鷄鷂鼩龣鸀鶍黚齆鶋鶇鶅龣鷋鶁黚黧黣鵾鵼齆齂龣鵸黮黫鷜默齏麙龗黧黣 css 龑龏鵶鹧龣鵾鵼齆齂黚黧黣 css 龑龏龗麷鶺黫鵲龣鵸鸯鵰龌鵯鵬麔龗鵨鵧齆齂龣麏鵣鼧黍黋黧龛鵠鵟鵛鼠

鵙龞鵣鼧鵕鵔齑齏龣齮鵓鵐鵌鵋鶁龣黧黣 css 龑龏麏麝齑齏齮黋鵕鵔鵾鵼鵉鶁鼠

    $default_options = [
      'type' => 'file',
      'group' => CSS_AGGREGATE_DEFAULT,
      'weight' => 0,
      'media' => 'all',
      'preprocess' => TRUE,
      'browsers' => ['IE' => TRUE,'!IE' => TRUE,],
    ];

鹕鹙鴮鴪 preprocess weight 龗鶁龣鴦鴣鴡 data 鷋齮黅黂鵶鹧鷈黴鷄鷂鼺鴟鴞黍鴝鸝鴛鼩

齮龑龏龗鷈黴鷄鷂鼺黍鴝鸝鴛鼩黓鷵鷋鸻龣鵾鵼齆齂鴗鷋鶁龣鴔鴐鷲鷮鴎鴍齥龛鷧鷪龗 css 黻齆齂

龩龨鶦鴉鴅鴃齮鵯鵬鴀鳾鳼鳻 'css' 鳷鴀鳾黠黣 css 黻齆齂龣鳳齆黍黋鼠

Hook_css_alter(&$css, $assets);

鳮黧黣鳫齆 $css 鹯黉齮鶅齞鳩鳥

鳮鳣黣鳫齆黚齑齏龑龏黻黴黰 \Drupal\Core\Asset\AttachedAssets 龣麒鹯黉鴀鳾鵸龣齞齜鳟鳞鳛鳚龣鹵齒鳗鸙 js 鵉鳔鼽鼼鳑龑龏

鳐齞黉鴀鳾鳼鹙麎麋鼨鼧鷧鷪鼺鳏龥龑龏麡麠黚鴎鸮鴎鴍齥龛鷧鳎鷪龣黠鴃黴鶛鸙鴎鴍齥龛龗龑龏鷧鷪龣鳋黠黝鴃齮鳈鳾鴎鴍齥龛鴔鴐龗鷧鷪鼩龣 css 龑龏龗鷧鷪龞齮齂 group 鵣鼧龣鳔鶁黚黧黣麇齆龣麎齮鳆鳂 weight 鵣鼧鼺鲿麔 weight 麡黚 SMACSS 鲺黐鳆鳂齋鵯鵬鳆鳂齦齏龗鲸鷙鼩龣齆鶁鲶鲲鲶鹙齏麙龣龳龥麛鷾龞鲯鲶鲲龣龛黷鲫鵯龗鵕鵔齂黚龩龨鵯鵬麔鷵 100 鼺鲦鲤 CSS_AGGREGATE_THEME 鼩龣鴉鴅鷵 0 鼺鲦鲤 CSS_AGGREGATE_DEFAULT 鼩龣鷢鷠鴃齮龳龥鵶鲢鹷龗鶚 css 鶔鴀鳾鳼齁鴀鳾鵸鸣

鲡鲟鼨鼧龩龨 info 鵶鹧齁鵯鵬龗黫鲞鲜鲛龣

黍鷙鲗齞齧 css 鲔鲐鷾鷽龣鷎鷍鲎鲍齞 \Drupal::service('asset.css.collection_optimizer')->optimize($css); 鵣鼧鷾鷽龣鸤龫龛鵠龓龑龏 css js 鷾鷽 鵟鵛齲龩龨鸣

鲇鹙鲆鲃黧黣鵾鵼齆齂鲁鴐龗 css 黻齆齂龣鱽黣鵾鵼齆齂黮黫鷜默齏麙龗黧黣 css 鵶鹧龣鵸鷠麡麠鷲鷮齕齒齏麙鷭鷪鷧鷪

鱺鱸鼠龳鶛鸙鲗齞鲔鲐鷾鷽麔龣齕齒 css 黻齁鵾鵼齆齂鸙齮黋鷋鸻鼠

type group weight media preprocess browsers data

黍鷙鲗齞齧鷎鷍鷋鸻 weight 鲎麝鳷鱴鼺鹙鲟麒默鴎鴍黉鷋鸻龣黻麡鷧鷥鷪龣麒麎鷜默鵸鼩龣黍鷙龑龏黚麝鲔鲐鳎龗龣鷎鷍麏鴡齏鷋鸻鼠 preprocessed 龣鶁鷵 true.

鹥龑龏齴齘黙齁鸡鹢龗 css 黻龳龛黷鹙鲟麀鹼齁鲎鲡鲟鹓鹏鹋 css 黻齊齈黙鸣

鸡鸠 js 黻鼠

css 黧鱰 js 齆鸮鱭鱰鼽鼼鸙鸲黂齏鱪龣鸲黂 id 鷵鼠

$cid = 'js: 鸖鸒龩龨鸻 : 鱨鱥 id:' . Crypt::hashBase64(serialize($libraries_to_load)) . (int) (count($assets->getSettings()) > 0) . (int) 黚鸀鷾鷽 ;

css 鵶鹧鱣鶺龳 html 鱟鶺齏麙龣齋 css 麒鱭龗黚 js 鴃齮龳鱟鶺齋鱞鶺齏麙龣黍鷙龌鼽鼼龗 header 鵾鵼鷵鱝龣鷎鷍黉龌鵉鳔鴎鴍龗龌齁 js 鱣鶺龳齕鱟齏麙龣 js 龗鵾鵼麏麝齑齏齮黋鵕鵔鶁鼠

      $default_options = [
        'type' => 'file',
        'group' => JS_DEFAULT,
        'weight' => 0,
        'cache' => TRUE,
        'preprocess' => TRUE,
        'attributes' => [],
        'version' => NULL,
        'browsers' => [],
      ];

css 黧鱰龣龳麷鶺齞鵾鵼齆齂黮黫黧黣 js 龑龏龣龳黉齆齂齁齞鶚 scope 鶔鲫鵲黉龑龏龳 html 鱟鶺鳗黚鱞鶺齏麙

黍鷙 js 龑龏麒鱕鸲黂鶦鼽鼼齧鷹鷶鵨鵧龣鷎鷍鲎麒鵣鼧鲔鲐

鴉鴅齋龩龨鴃齮鱑鳎鶚 js 鶔鴀鳾鳼鳻鳷鴀鳾齏麙龗 js 龑龏鼠

Hook_js_alter(&$js, $assets);

鳫齆 $js 黚齮鴎鴍鷭鷪鷧鷪龗 js 龑龏齆齂龣鷋鸻黚鱣鷄鷂鼺鱭 css 鼩龣鷋鶁黚鵾鵼齆齂龣鳫齆 $assets css

鼨鼧鱍鴀鳾鳼鳻鹙麎鷲鷮 weight 龗鶁鳷鳐麇齏麙鷭鷪龣鱭 css 龣黉鶁鷾龞鲯鲲齜鴎鴍齥龛

鷧鷪鱍鹙龣齋 css 黧鱰鵣鼧 js 鵶鹧鲔鲐

龛黷鲢鱌齧黧黣鱉鱅鱁龗龌鶚 core/drupalSettings 鶔齞齮鵟鵛 js 鼽鼼齆鸮龣黉齆鸮鸙鰿鷀鸦鴞鲫鵯鼠

1 鶩龳龌鵯鵬齁齮鶚 drupalSettings 鶔鵾鵼鲫鵯

2 鶩龳齊齈齆齂齁齮鶚 $render_array['#attached']['drupalSettings'] 鶔鲫鵯

js 鼽鼼齆鸮鵟鵛麀鹼黍黋鼠

龌鵯鵬龗鼽鼼齆鸮鷲鷮鴎鴍鷭鷪鴎麋鲐鴦龣鲍齞黍黋鲐鴦鸦鸥鼠

\Drupal\Component\Utility\NestedArray::mergeDeepArray

鼨鼧鴉鴅鳼鳻鴀鳾鲐鴦鲸鷙龣鳳齆黍黋鼠

hook_js_settings_build(&$settings, $assets);

鱺鱸鼠黠鹵龗鼽鼼鳫齆鴦麒鰻鰸齊齈齆齂齁龗鼽鼼鶁龣鲆鲃鹙鰷鲎鲍齞鱭鱰龗鲐鴦鸦鸥鲈齊齈齆齂龗鼽鼼鶁鲐鴦龣齊齈齆齂鼽鼼龗鼽鼼鶁鷾龞鲯鲇鲲龣鴃齮鰳鰲鴝鴛齆鸮龣鲐鴦鹙鹲鹱鴀鳾鳼鳻鶚 js_settings 鶔鼠

hook_js_settings_alter(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets)

黍鷙龌鶚 core/drupalSettings 鶔龳鱟鶺齏麙鷎鷍鼽鼼齆鸮鲎龳鰮鰫 js 齏麙龥齏麙龣黍鷙龳鱞鶺齏麙龣鷎鷍鼽鼼齆鸮龳鰮鰫鱞鶺 js 齏麙龥齏麙鸣

龌鹱鰩鼠

黠黚黧黣鹀鸾龣齞齜鹢鹷黧黣鰧鰤鵯鵬龗鸝鸙龑龏龌龣鴃鱑鳎鰧鰤鸻鹢鹷鳔黋鸝鸙龑龏龌鵯鵬龣黝鴃鱑鳎鰧鰤鸻齋龌鸻鹢鹷鰣鰡龗黧黣龑龏龌鵯鵬龣黠鰠鵯鵬麡麠麝鹯齞齧鰳鰲齋鲡鰝龣鱭麔鲢鱌鰛鲛鸲黂鰘鱕龣龌鹱鰩鹀鸾鵯鵬黍黋鼠

鹀鸾鸻鼠 library.discovery

黐鼠 Drupal\Core\Asset\LibraryDiscovery

黉鹀鸾龫鰕鴝鰓鲢鱌齧鰐鰎鸲黂鰘鱕龣鰋鰊鰆黓黚龳龌鹱鰩鳥黻黙齁鵣鼧龗

龌鹱鰩鳥黻黙 library.discovery.collector

鹀鸾鼠 library.discovery.collector

黐鼠 Drupal\Core\Asset\LibraryDiscoveryCollector

黉鹀鸾鲡鰝齧鸲黂鳥黻黙 CacheCollector \Drupal\Core\Cache\CacheCollector 鼩龣鸲黂鳥黻黙齞齜鸙鰅鲤齆鸮鷜默鸲黂龗鰂鴔黋龣黍鷙鱽麋龡麃鹻鱣鶺齏麙龣鵧鱕黚鱉鯾鯻龗龣鵸齦鯸齜黧黣鳎鯴黙龣鰓齏麙麝鱝鰋鸴齞鳎龗齆鸮龣鳋鴦麒麏鯱鯰鰮鰫齆鸮龣黠龳鯮鼧麔龩龨鱺鯪黫鼺 \Drupal\Core\Utility\ThemeRegistry 鼩齁黝麝齞鹷鸣

龳龌鹱鰩鳥黻黙齁龣龌鵯鵬齆鸮麝鸲黂龳 cache.discovery 齁龣 cid 黚鶚 library_info: + 鯸龥鸖鸒龩龨鸻龣鸲黂鷹鷶鼠 ['library_info'] 龣齆鸮黚齮鰧鰤鷵鲞鹆鯧黂龗龣黝鷣黚鯥黉鸲黂齆鸮黚齮鰧鰤鸻鷵鷋鸻龗黧黣齆齂龣鲇鯡黠黣齆齂黚鯟龗龣龳龛黷鯮鼧鳎鹼齁鯜鯛齏鼦鰧鰤鵯鵬龗龌齆鸮龣鰓鸙龳鰧鰤鵯鵬龗龌麝齞鹷鹙龣鯘麏鲎黉鰧鰤齏鼦鲿鸲黂齮鯖麎齞龣黠鷣黚鴝鴛鸲黂鳥黻黙龗黓齞龣默鹢鹷鯓黣鰧鰤鵯鵬龗鱣鶺龌鴃齮黠鱰鼠

\Drupal::service("library.discovery.collector")->get("bartik");

黉黮鯍黧鯋鼨鼧龣黴鹯鰧鰤龗龌鵯鵬齆鸮鲎麝鸲黂鯊齮鹙鱽麋龡麃鹻麏麝齏麙龣鯉鲿麒默鯅鱸鸴齞黉黮鯍龣鯁黝鴃齮鸴齞齮黋鸦鸥鰛鵛鸲黂鼠

\Drupal::service("library.discovery")->clearCachedDefinitions();

// 鮿鮼鸲黂鮻鱭龫麋鯮鼧麔龗鰐鰎鸲黂黧鮺鰛鵛

\Drupal::service("library.discovery.collector")->clear();

// 齋龥黧鸦鸥齦麗鰓鰛鵛鮿鮼鸲黂

龳龌鹱鰩鳥黻黙齁鵟鵛齧龌鲡鰝齋黧鶺鲺龌鰳鰲龣鳈鮹龌黔黓龳龌鹱鰩齴齘黙齁鵣鼧鸣

龌鹱鰩齴齘黙 library.discovery.parser

齞齜鹢鹷鲞黣鰧鰤鼺鮵鮱鶩鴉鴅鶩龩龨鼩鵯鵬龗鱣鶺龌鹞鹚龣黠鰠龌鶛鸙鵟鵛龌鲡鰝龣鵣鼧齧鶺鲺龌鰳鰲

鹀鸾 id library.discovery.parser

黐鼠 Drupal\Core\Asset\LibraryDiscoveryParser

鳐齞鼦鼣鼠

\Drupal::service("library.discovery.parser")->buildByExtension($extension);

黉鹀鸾鼨鼧龩默麀鹼黚鼠

龯鸠鰧鰤鵯鵬龗龌鵶鹧

黍鷙鰧鰤鰋鰩齧鸒鰎鵯鵬鼺黝鷣黚鰋鰩齧鳼鳻鼠 library_info_build 龣鳳齆鸻鼠 moduleName_library_info_build() 鼩鷎鷍鲎鵶鹧鵯鵬齋鸒鰎鵯鵬鲐鴦

鼨鼧鴉鴅齋龩龨龗鶚 library_info 鶔鴀鳾鳼鳻鼠 hook_library_info_alter(&$libraries, $extension) 鳷鳐麇龌鵯鵬

鲎齮鴝鯛鮯鹢鹷龗龌鵯鵬鯮齞龩龨鵯鵬龗鰳鰲

鵓鵐鮮鵾鵼鵕鵔鶁龣鮭鷽龑龏鷄鷂鷵鷈黴鷄鷂

鱺鱸黉鹀鸾鶛鸙鯮齞鰮鰫鸲黂鮪鮦龣鮤黴麇黣龌龗鰳鰲鮡鶛鸙鱍鴐鮠鮟鶦鮛鲛龣黝鶛鸙鱍鴐龌鲡鰝龣黠鰠龳龌鹱鰩鳥黻黙齁鱍鴐鼠 "library.discovery.collector"

龌鴎鴍齴齘黙 library.dependency_resolver

鹀鸾鸻鼠 library.dependency_resolver

黐鼠 Drupal\Core\Asset\LibraryDependencyResolver

黉鹀鸾龗黮鯍鲤麒鮹龣鳋鮘鲦鮕鮑龣黠鹵齴鮏黧黋鲍齞龗鮎鸥鼠

doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = [])

黉鸦鸥鲆鲃鹓鼦龗鸝鸙龌齋鵸鷠龗鸝鸙鴎鴍龣鴦麡麠鷧鷥鷪龣鴃麝鮊鳩鸴齞鸣

鵙龞鷢鷠鷜默鮉鮇麝鴎鴍龗龌鹯黉龞齏麙龣龳龌齆齂齁鷧龳龥齒龣龌齮鶚鰧鰤鸻 / 龌鸻鶔龗鴟鴞鮃鰩鸪龌龗鴎鴍鲸鲁鴃齮魿鷵黧魽鸙魻魹魸魷龣鼺鳫魴齆魰魬魨龗龓魷魥齲鶦魢鮎鮪魠魰鲈魞魚魬魨龗龓齆鸮鲸鲁齲鼩龣鴎鴍麒鱕鮃鰩魙魸龣鸀鶍鷣黚鸙魸魷齧龣黉鸦鸥鲍齞魕魔鷾龞魑魏鮎鸥龣鱕魋黅魈鲆鲃龗齆齂齁麝鴎鴍龗龌黧鵯鮃鰩龳鴎鴍鵸龗龌龗龥齒龣鯊鱽黧黣龌鰓鮃鰩黧麋龣黉鸦鸥鳮黧黣鳫齆鹯黉黚齮齏麙鷭鷪鷧鷥鷪龗龌齆齂龣龞齏麙龗鷧龳龥齒龣黍鷙鹓鼦龗齆齂鮮魇魆鳏魅龫魂黂龳鴎鴍齥龛龣鷎鷍鲎魀鬾鹓鼦龗鷭鷪齮黅魈麝鴎鴍龗龌龳龥齒鸣

getMinimalRepresentativeSubset(array $libraries)

黉鸦鸥齞齜鲆鲃黧黣龌黻龗鲇鬽黫鵲龣鮟鬹鬸鯥鷣黚龳黠黣龌鲔鲐齁龣黍鷙黧黣龌麝黻鲐齁龗鳔鬵龌鴎鴍龣鷎鷍麝鴎鴍龗黠黣龌鲎麝鳷鱴龣鬴黣鬲鳻龣鬯鼽鹓鼦龗龌黻鲐鷵鼠

['core/a', 'core/b', 'core/c']

鳔齁 core/c 鴎鴍 core/a 龣鷎鷍麠鳎黉鸦鸥鵟鵛鹙龣鲎鲆鲃鼠

['core/b', 'core/c']

鬬黉鸦鸥鹢鹷龗鲇鬽黫鵲黚鲇鬫鬧龗龣鵸鷠龗鴎鴍鴃齮鬬 getLibrariesWithDependencies(array $libraries) 鸦鸥鲐鴦鵣鸯

龑龏齴齘齦齥鼽鼼鼠

黚鸀鲔鲐 css js 鵶鹧龣龳龛黷鹙鬡黉鵟鼽鼼鼠

/admin/config/development/performance

鹥鷕鼼龛黷鹢鹷黉鹞鹚鼠

\Drupal::config('system.performance')->get('css.preprocess');

\Drupal::config('system.performance')->get('js.preprocess');

黝鴃齮龳鷕鼼鵶鹧齁鼽鼼齮黋鶁鬞鼧鰳鰲鼠

$config['system.performance']['css']['preprocess'] = FALSE;

$config['system.performance']['js']['preprocess'] = FALSE;

本书共88小节:

评论 (写第一个评论)