누군가 Webpack의 CommonsChunkPlugin을 설명 할 수 있습니까?
나는 CommonsChunkPlugin모든 진입 점을 살펴보고 그들 사이에 공통된 패키지 / 종속성이 있는지 확인하고 그들을 자신의 번들로 분리 한다는 일반적인 요점을 얻습니다 .
따라서 다음 구성이 있다고 가정 해 보겠습니다.
...
enrty : {
entry1 : 'entry1.js', //which has 'jquery' as a dependency
entry2 : 'entry2.js', //which has 'jquery as a dependency
vendors : [
'jquery',
'some_jquery_plugin' //which has 'jquery' as a dependency
]
},
output: {
path: PATHS.build,
filename: '[name].bundle.js'
}
...
사용하지 않고 동고하면 CommonsChunkPlugin
3 개의 새로운 번들 파일로 끝납니다.
entry1.bundle.js이는에서 전체 코드를 포함entry1.js하고jquery자체 런타임을 포함entry2.bundle.js이는에서 전체 코드를 포함entry2.js하고jquery자체 런타임을 포함vendors.bundle.js이는에서 전체 코드를 포함jquery하고some_jquery_plugin자체 런타임을 포함
jquery페이지에 잠재적으로 3 번 로드 할 것이기 때문에 이것은 분명히 나쁘기 때문에 우리는 그것을 원하지 않습니다.
동봉하면 CommonsChunkPlugin
내가 전달하는 인수에 따라 CommonsChunkPlugin다음 중 하나가 발생합니다.
사례 1 : 통과
{ name : 'commons' }하면 다음 번들 파일이 생성됩니다.entry1.bundle.js이는에서 전체 코드 포함entry1.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는entry2.bundle.js이는에서 전체 코드 포함entry2.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는vendors.bundle.js이는에서 전체 코드 포함some_jquery_plugin에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는commons.bundle.js완전한 코드를jquery포함하고 런타임을 포함합니다.
이렇게하면 전체적으로 더 작은 번들로 끝나고 런타임이
commons번들에 포함됩니다 . 꽤 괜찮지 만 이상적이지는 않습니다.CASE 2 : 통과
{ name : 'vendors' }하면 다음 번들 파일이 생성됩니다.entry1.bundle.js이는에서 전체 코드 포함entry1.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는entry2.bundle.js이는에서 전체 코드 포함entry2.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는vendors.bundle.js여기에는 완전한 코드jquery와some_jquery_plugin런타임이 포함됩니다.
이렇게하면 전체적으로 더 작은 번들로 끝나지만 이제 런타임이
vendors번들에 포함됩니다 . 런타임이 이제vendors번들 에 있기 때문에 이전 사례보다 약간 나쁩니다 .CASE 3 : 통과
{ names : ['vendors', 'manifest'] }하면 다음 번들 파일이 생성됩니다.entry1.bundle.js이는에서 전체 코드 포함entry1.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는entry2.bundle.js이는에서 전체 코드 포함entry2.js에 대한 요구 사항을,jquery그리고 런타임을 포함하지 않는vendors.bundle.js이는에서 전체 코드를 포함jquery하고some_jquery_plugin및 런타임을 포함하지 않는manifest.bundle.js다른 모든 번들에 대한 요구 사항을 포함하고 런타임을 포함합니다.
이렇게하면 전체적으로 더 작은 번들로 끝나고 런타임이
manifest번들에 포함됩니다 . 이것이 이상적인 경우입니다.
내가 이해하지 못하는 것 / 이해할 수없는 것
에서 CASE 2 왜 우리는 끝낼 않았다
vendors모두 공통 코드 (포함 된 번들jquery로부터 남아 무엇이든)과vendors항목을 (some_jquery_plugin)? 내 이해에서CommonsChunkPlugin여기서 한 일은 공통 코드 (jquery)를 모아서vendors번들 로 출력하도록 강제했기 때문에 공통 코드를vendors번들 로 "병합"한 것 입니다.some_jquery_plugin). 확인하거나 설명하십시오.CASE 3 에서는
{ names : ['vendors', 'manifest'] }플러그인에 전달했을 때 무슨 일이 있었는지 이해할 수 없습니다 . 왜 / 어떻게vendors번들이 손상되지 않았고,jquery와 둘 다 포함되어 있으며some_jquery_plugin, 언제jquery는 공통 종속성이며, 생성 된manifest.bundle.js파일이 생성 된 방식대로 생성 된 이유는 무엇입니까 (다른 모든 번들이 필요하고 런타임 포함)?
이것이 CommonsChunkPlugin작동 하는 방식입니다.
공통 청크는 여러 항목 청크가 공유하는 모듈을 "수신"합니다. 복잡한 구성의 좋은 예는 Webpack 저장소 에서 찾을 수 있습니다 .
는 CommonsChunkPlugin청크가 밀봉 디스크에 기록하기 직전, 그것은 메모리 동작 함을 의미 웹팩의 최적화 단계 동안 실행됩니다.
When several common chunks are defined, they are processed in order. In your case 3, it is like running the plugin twice. But please note that the CommonsChunkPlugin can have a more complex configuration (minSize, minChunks, etc) that impacts the way modules are moved.
CASE 1:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
commonschunk as a common chunk. - The plugin processes the
commonscommon chunk (since the chunk does not exist, it is created):- It collects the modules that are used more than once in the other chunks:
entry1,entry2andvendorsusejqueryso the module is removed from these chunks and is added to thecommonschunk. - The
commonschunk is flagged as anentrychunk while theentry1,entry2andvendorschunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
commonschunk is anentrychunk it contains the runtime and thejquerymodule.
CASE 2:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
vendorschunk as a common chunk. - The plugin processes the
vendorscommon chunk:- It collects the modules that are used more than once in the other chunks:
entry1andentry2usejqueryso the module is removed from these chunks (note that it is not added to thevendorschunk because thevendorschunk already contains it). - The
vendorschunk is flagged as anentrychunk while theentry1andentry2chunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
vendorschunk is anentrychunk, it contains the runtime and thejquery/jquery_pluginmodules.
CASE 3:
- There are 3
entrychunks (entry1,entry2andvendors). - The configuration sets the
vendorschunk and themanifestchunk as common chunks. - The plugin creates the
manifestchunk as it does not exist. - The plugin processes the
vendorscommon chunk:- It collects the modules that are used more than once in the other chunks:
entry1andentry2usejqueryso the module is removed from these chunks (note that it is not added to thevendorschunk because thevendorschunk already contains it). - The
vendorschunk is flagged as anentrychunk while theentry1andentry2chunks are unflagged asentry.
- It collects the modules that are used more than once in the other chunks:
- The plugin processes the
manifestcommon chunk (since the chunk does not exist, it is created):- It collects the modules that are used more than once in the other chunks: as there are no modules used more than once, no module is moved.
- The
manifestchunk is flagged asentrychunk while theentry1,entry2andvendorsare unflagged asentry.
- Finally, since the
manifestchunk is anentrychunk it contains the runtime.
Hope it helps.
참고URL : https://stackoverflow.com/questions/39548175/can-someone-explain-webpacks-commonschunkplugin
'Program Tip' 카테고리의 다른 글
| 웹 브라우저의 뒤로 버튼은 어떻게 작동합니까? (0) | 2020.10.10 |
|---|---|
| 형제 요소를 부모 태그에 래핑하지 않고 렌더링하려면 어떻게해야합니까? (0) | 2020.10.10 |
| Angular ReactiveForms : 체크 박스 값 배열 생성? (0) | 2020.10.10 |
| "pragma"라는 단어는 어디에서 왔습니까? (0) | 2020.10.10 |
| 보고서에 매개 변수 (다중 값) 표시 (0) | 2020.10.09 |