From d1900cc33ef4f637607918a227c7994bf87227fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Piecyk?= Date: Tue, 18 Sep 2018 08:32:55 +0200 Subject: [PATCH 1/4] Context Replacement Plugin Add example on how to tree shake locales from date-fns --- src/content/plugins/context-replacement-plugin.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/content/plugins/context-replacement-plugin.md b/src/content/plugins/context-replacement-plugin.md index a067322a9a23..12829b4983cf 100644 --- a/src/content/plugins/context-replacement-plugin.md +++ b/src/content/plugins/context-replacement-plugin.md @@ -31,10 +31,17 @@ If the resource (directory) matches `resourceRegExp`, the plugin replaces the de Here's a small example to restrict module usage: ```javascript +// moment.js new webpack.ContextReplacementPlugin( /moment[/\\]locale$/, /de|fr|hu/ ); + +// date-fns +new ContextReplacementPlugin( + /date\-fns[\/\\]/, + /[/\\](de|fr|hu)[/\\]/ + ); ``` The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see [this issue](https://github.com/moment/moment/issues/2373) for more information). From 92c29ecc524570b0454ac2f53715475a8f20ee63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Piecyk?= Date: Tue, 18 Sep 2018 16:37:48 +0200 Subject: [PATCH 2/4] fix indentation --- src/content/plugins/context-replacement-plugin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/plugins/context-replacement-plugin.md b/src/content/plugins/context-replacement-plugin.md index 12829b4983cf..3f99a45e4387 100644 --- a/src/content/plugins/context-replacement-plugin.md +++ b/src/content/plugins/context-replacement-plugin.md @@ -41,7 +41,7 @@ new webpack.ContextReplacementPlugin( new ContextReplacementPlugin( /date\-fns[\/\\]/, /[/\\](de|fr|hu)[/\\]/ - ); +); ``` The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see [this issue](https://github.com/moment/moment/issues/2373) for more information). From 422959f64fdfb00753152014844254dd678a0c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Piecyk?= Date: Tue, 30 Oct 2018 20:24:28 +0100 Subject: [PATCH 3/4] update context replacement plugin docs - date-fns replace date-fns description with link to documentation --- src/content/plugins/context-replacement-plugin.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/content/plugins/context-replacement-plugin.md b/src/content/plugins/context-replacement-plugin.md index 3f99a45e4387..440853cd53a0 100644 --- a/src/content/plugins/context-replacement-plugin.md +++ b/src/content/plugins/context-replacement-plugin.md @@ -3,9 +3,12 @@ title: ContextReplacementPlugin contributors: - simon04 - byzyk + - masives related: - title: Issue 2783 - ContextReplacementPlugin Description url: https://github.com/webpack/webpack/issues/2783#issuecomment-234137265 + - title: Using context replacement module for date-fns + url: https://github.com/date-fns/date-fns/blob/master/docs/webpack.md --- *Context* refers to a [require with an expression](/guides/dependency-management/#require-with-expression) such as `require('./locale/' + name + '.json')`. When encountering such an expression, webpack infers the directory (`'./locale/'`) and a regular expression (`/^.*\.json$/`). Since the `name` is not known at compile time, webpack includes every file as module in the bundle. @@ -31,19 +34,13 @@ If the resource (directory) matches `resourceRegExp`, the plugin replaces the de Here's a small example to restrict module usage: ```javascript -// moment.js new webpack.ContextReplacementPlugin( /moment[/\\]locale$/, /de|fr|hu/ ); - -// date-fns -new ContextReplacementPlugin( - /date\-fns[\/\\]/, - /[/\\](de|fr|hu)[/\\]/ -); ``` + The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see [this issue](https://github.com/moment/moment/issues/2373) for more information). From bcfddcf03053cde0d2c6198647d3799057d88101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Piecyk?= Date: Tue, 30 Oct 2018 20:25:49 +0100 Subject: [PATCH 4/4] context-replacement-plugin - remove linebreak --- src/content/plugins/context-replacement-plugin.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/content/plugins/context-replacement-plugin.md b/src/content/plugins/context-replacement-plugin.md index 440853cd53a0..fff08c617c25 100644 --- a/src/content/plugins/context-replacement-plugin.md +++ b/src/content/plugins/context-replacement-plugin.md @@ -40,7 +40,6 @@ new webpack.ContextReplacementPlugin( ); ``` - The `moment/locale` context is restricted to files matching `/de|fr|hu/`. Thus only those locales are included (see [this issue](https://github.com/moment/moment/issues/2373) for more information).