누군가 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
entry
chunks (entry1
,entry2
andvendors
). - The configuration sets the
commons
chunk as a common chunk. - The plugin processes the
commons
common 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
,entry2
andvendors
usejquery
so the module is removed from these chunks and is added to thecommons
chunk. - The
commons
chunk is flagged as anentry
chunk while theentry1
,entry2
andvendors
chunks are unflagged asentry
.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
commons
chunk is anentry
chunk it contains the runtime and thejquery
module.
CASE 2:
- There are 3
entry
chunks (entry1
,entry2
andvendors
). - The configuration sets the
vendors
chunk as a common chunk. - The plugin processes the
vendors
common chunk:- It collects the modules that are used more than once in the other chunks:
entry1
andentry2
usejquery
so the module is removed from these chunks (note that it is not added to thevendors
chunk because thevendors
chunk already contains it). - The
vendors
chunk is flagged as anentry
chunk while theentry1
andentry2
chunks are unflagged asentry
.
- It collects the modules that are used more than once in the other chunks:
- Finally, since the
vendors
chunk is anentry
chunk, it contains the runtime and thejquery
/jquery_plugin
modules.
CASE 3:
- There are 3
entry
chunks (entry1
,entry2
andvendors
). - The configuration sets the
vendors
chunk and themanifest
chunk as common chunks. - The plugin creates the
manifest
chunk as it does not exist. - The plugin processes the
vendors
common chunk:- It collects the modules that are used more than once in the other chunks:
entry1
andentry2
usejquery
so the module is removed from these chunks (note that it is not added to thevendors
chunk because thevendors
chunk already contains it). - The
vendors
chunk is flagged as anentry
chunk while theentry1
andentry2
chunks are unflagged asentry
.
- It collects the modules that are used more than once in the other chunks:
- The plugin processes the
manifest
common 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
manifest
chunk is flagged asentry
chunk while theentry1
,entry2
andvendors
are unflagged asentry
.
- Finally, since the
manifest
chunk is anentry
chunk 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 |