source: frontend/node_modules/eslint-plugin-import/docs/rules/dynamic-import-chunkname.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
Line 
1# import/dynamic-import-chunkname
2
3💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
4
5<!-- end auto-generated rule header -->
6
7This rule reports any dynamic imports without a webpackChunkName specified in a leading block comment in the proper format.
8
9This rule enforces naming of webpack chunks in dynamic imports. When you don't explicitly name chunks, webpack will autogenerate chunk names that are not consistent across builds, which prevents long-term browser caching.
10
11## Rule Details
12
13This rule runs against `import()` by default, but can be configured to also run against an alternative dynamic-import function, e.g. 'dynamicImport.'
14You can also configure the regex format you'd like to accept for the webpackChunkName - for example, if we don't want the number 6 to show up in our chunk names:
15
16 ```javascript
17{
18 "import/dynamic-import-chunkname": [2, {
19 importFunctions: ["dynamicImport"],
20 webpackChunknameFormat: "[a-zA-Z0-57-9-/_]+",
21 allowEmpty: false
22 }]
23}
24```
25
26### invalid
27
28The following patterns are invalid:
29
30```javascript
31// no leading comment
32import('someModule');
33
34// incorrectly formatted comment
35import(
36 /*webpackChunkName:"someModule"*/
37 'someModule',
38);
39import(
40 /* webpackChunkName : "someModule" */
41 'someModule',
42);
43
44// chunkname contains a 6 (forbidden by rule config)
45import(
46 /* webpackChunkName: "someModule6" */
47 'someModule',
48);
49
50// invalid syntax for webpack comment
51import(
52 /* totally not webpackChunkName: "someModule" */
53 'someModule',
54);
55
56// single-line comment, not a block-style comment
57import(
58 // webpackChunkName: "someModule"
59 'someModule',
60);
61
62// chunk names are disallowed when eager mode is set
63import(
64 /* webpackMode: "eager" */
65 /* webpackChunkName: "someModule" */
66 'someModule',
67)
68```
69
70### valid
71
72The following patterns are valid:
73
74```javascript
75 import(
76 /* webpackChunkName: "someModule" */
77 'someModule',
78 );
79 import(
80 /* webpackChunkName: "someOtherModule12345789" */
81 'someModule',
82 );
83 import(
84 /* webpackChunkName: "someModule" */
85 /* webpackPrefetch: true */
86 'someModule',
87 );
88 import(
89 /* webpackChunkName: "someModule", webpackPrefetch: true */
90 'someModule',
91 );
92
93 // using single quotes instead of double quotes
94 import(
95 /* webpackChunkName: 'someModule' */
96 'someModule',
97 );
98```
99
100### `allowEmpty: true`
101
102If you want to allow dynamic imports without a webpackChunkName, you can set `allowEmpty: true` in the rule config. This will allow dynamic imports without a leading comment, or with a leading comment that does not contain a webpackChunkName.
103
104Given `{ "allowEmpty": true }`:
105
106<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
107### valid
108
109The following patterns are valid:
110
111```javascript
112import('someModule');
113
114import(
115 /* webpackChunkName: "someModule" */
116 'someModule',
117);
118```
119<!-- markdownlint-disable-next-line MD024 -- duplicate header -->
120### invalid
121
122The following patterns are invalid:
123
124```javascript
125// incorrectly formatted comment
126import(
127 /*webpackChunkName:"someModule"*/
128 'someModule',
129);
130```
131
132## When Not To Use It
133
134If you don't care that webpack will autogenerate chunk names and may blow up browser caches and bundle size reports.
Note: See TracBrowser for help on using the repository browser.