magic-comments-loader 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +484 -176
- package/package.json +35 -20
- package/dist/booleanComment.js +0 -107
- package/dist/comment.js +0 -36
- package/dist/index.js +0 -13
- package/dist/loader.js +0 -33
- package/dist/package.json +0 -55
- package/dist/regexComment.js +0 -109
- package/dist/schema.js +0 -44
- package/dist/strategy.js +0 -34
- package/dist/util.js +0 -73
- package/dist/webpackChunkName.js +0 -70
- package/dist/webpackExclude.js +0 -15
- package/dist/webpackExports.js +0 -83
- package/dist/webpackIgnore.js +0 -24
- package/dist/webpackInclude.js +0 -15
- package/dist/webpackMode.js +0 -113
- package/dist/webpackPrefetch.js +0 -24
- package/dist/webpackPreload.js +0 -24
package/README.md
CHANGED
|
@@ -1,67 +1,430 @@
|
|
|
1
|
-
# [`magic-comments-loader`](https://www.npmjs.com/package/magic-comments-loader)
|
|
1
|
+
# [`magic-comments-loader`](https://www.npmjs.com/package/magic-comments-loader) 🪄
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
[](https://codecov.io/gh/morganney/magic-comments-loader)
|
|
5
5
|
|
|
6
6
|
Keep your source code clean, add [magic coments](https://webpack.js.org/api/module-methods/#magic-comments) to your dynamic `import()` statements at build time.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
## Getting Started
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
* `webpackChunkName`
|
|
12
|
-
* `webpackMode`
|
|
13
|
-
* `webpackIgnore`
|
|
14
|
-
* [`webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
15
|
-
* [`webpackPrefetch`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
16
|
-
* `webpackInclude`
|
|
17
|
-
* `webpackExclude`
|
|
18
|
-
* `webpackExports`
|
|
10
|
+
First install `magic-comments-loader`:
|
|
19
11
|
|
|
20
|
-
|
|
12
|
+
```
|
|
13
|
+
npm install magic-comments-loader
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Next add the loader to your `webpack.config.js` file:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
module: {
|
|
20
|
+
rules: [
|
|
21
|
+
{
|
|
22
|
+
test: /\.[jt]sx?$/,
|
|
23
|
+
use: ['magic-comments-loader']
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Then given a **file.js** with the following import:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
const dynamicModule = await import('./path/to/module.js')
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
While running `webpack` the dynamic import inside **file.js** becomes:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const dynamicModule = await import(/* webpackChunkName: "path-to-module" */ './path/to/module.js')
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The `webpackChunkName` comment is added by default when registering the loader. See the supported [options](#options) to learn about configuring other magic comments.
|
|
42
|
+
|
|
43
|
+
## Options
|
|
44
|
+
|
|
45
|
+
Most loader options can be defined with a [`CommentConfig`](#commentconfig) object to support overrides and suboptions ([`CommentOptions`](#commentoptions)). Options that support globs use [`micromatch`](https://github.com/micromatch/micromatch) for pattern matching.
|
|
46
|
+
|
|
47
|
+
* [`verbose`](#verbose)
|
|
48
|
+
* [`match`](#match)
|
|
49
|
+
* [`webpackChunkName`](#webpackchunkname)
|
|
50
|
+
* [`webpackFetchPriority`](#webpackfetchpriority)
|
|
51
|
+
* [`webpackMode`](#webpackmode)
|
|
52
|
+
* [`webpackPrefetch`](#webpackprefetch)
|
|
53
|
+
* [`webpackPreload`](#webpackpreload)
|
|
54
|
+
* [`webpackInclude`](#webpackinclude)
|
|
55
|
+
* [`webpackExclude`](#webpackexclude)
|
|
56
|
+
* [`webpackExports`](#webpackexports)
|
|
57
|
+
* [`webpackIgnore`](#webpackignore)
|
|
58
|
+
|
|
59
|
+
### `CommentConfig`
|
|
60
|
+
To allow configuration overrides based on module or import paths, or to support comment options that extend functionality, all options except `verbose`, and `match`, can be defined with an object using the following interface:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
interface CommentConfig {
|
|
64
|
+
config: CommentOptions;
|
|
65
|
+
overrides?: Array<{
|
|
66
|
+
files: string | string[];
|
|
67
|
+
config: CommentOptions;
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### `CommentOptions`
|
|
73
|
+
|
|
74
|
+
The exact `CommentOptions` shape defining `config` is determined by the loader option it is associated with, but the interface always extends `CommentOptionsBase`:
|
|
75
|
+
```ts
|
|
76
|
+
interface CommentOptions extends CommentOptionsBase {
|
|
77
|
+
// In general, a "falsy" value disables the comment.
|
|
78
|
+
[option: string]: boolean | string | Function | RegExp | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface CommentOptionsBase {
|
|
82
|
+
// Can be used to turn a magic comment on or off.
|
|
83
|
+
active?: boolean | ((modulePath: string, importPath: string) => boolean)
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
You can skip to the [overrides example](#overrides) to get a better sense of how this all works.
|
|
88
|
+
|
|
89
|
+
### `verbose`
|
|
90
|
+
**type**
|
|
91
|
+
```ts
|
|
92
|
+
boolean
|
|
93
|
+
```
|
|
94
|
+
**default** `false`
|
|
95
|
+
|
|
96
|
+
Prints console statements of the module filepath and updated `import()` during the webpack build. Useful for debugging.
|
|
97
|
+
|
|
98
|
+
### `match`
|
|
99
|
+
**type**
|
|
100
|
+
```ts
|
|
101
|
+
'module' | 'import'
|
|
102
|
+
```
|
|
103
|
+
**default** `'module'`
|
|
104
|
+
|
|
105
|
+
Sets how globs are matched, either the module file path, or the `import()` specifier.
|
|
106
|
+
|
|
107
|
+
### `webpackChunkName`
|
|
108
|
+
|
|
109
|
+
**type**
|
|
110
|
+
```ts
|
|
111
|
+
boolean
|
|
112
|
+
| string
|
|
113
|
+
| string[]
|
|
114
|
+
| ((modulePath: string, importPath: string) => any)
|
|
115
|
+
| CommentConfig
|
|
116
|
+
```
|
|
117
|
+
**default** `true`
|
|
118
|
+
|
|
119
|
+
Adds `webpackChunkName` magic comments. This option is enabled by default when registering the loader in your webpack configuration.
|
|
120
|
+
|
|
121
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
122
|
+
```ts
|
|
123
|
+
{
|
|
124
|
+
basename: boolean;
|
|
125
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Possible values:
|
|
130
|
+
* `true` - Adds `webpackChunkName` comments to **all** dynamic imports using the derived path from the import specifier in kebab-case as the chunk name. This is the default.
|
|
131
|
+
* `false` - Disables adding the `webpackChunkName` comment globally.
|
|
132
|
+
* `string | string[]` - When the glob(s) match a path from a [`match`](#match) path, a `webpackChunkName` comment is added using the derived path from the import specifier in kebab-case as the chunk name.
|
|
133
|
+
* `(modulePath: string, importPath: string) => any` - Return a string to be used as the chunk name. Returning a falsy value will skip adding the comment.
|
|
134
|
+
* `config.basename`:
|
|
135
|
+
* `true` - Use only the [basename](https://nodejs.org/api/path.html#pathbasenamepath-suffix) from the import specifier as the chunk name. Relative imports may result in name collisions. Use in areas where you know the basenames are unique.
|
|
136
|
+
* `false` - Use the full derived path from the import specifier in kebab-case as the chunk name, same as the default behavior.
|
|
137
|
+
* `config.active`:
|
|
138
|
+
* `true` - Disable the comment.
|
|
139
|
+
* `false` - Enable the comment.
|
|
140
|
+
|
|
141
|
+
### `webpackFetchPriority`
|
|
142
|
+
|
|
143
|
+
**type**
|
|
144
|
+
```ts
|
|
145
|
+
boolean
|
|
146
|
+
| 'high' | 'low' | 'auto'
|
|
147
|
+
| ((modulePath: string, importPath: string) => any)
|
|
148
|
+
| CommentConfig
|
|
149
|
+
```
|
|
150
|
+
**default** None
|
|
151
|
+
|
|
152
|
+
Adds `webpackFetchPriority` magic comments.
|
|
153
|
+
|
|
154
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
155
|
+
```ts
|
|
156
|
+
{
|
|
157
|
+
fetchPriority: 'high' | 'low' | 'auto' | ((modulePath: string, importPath: string) => any);
|
|
158
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Possible values:
|
|
163
|
+
* `false` - Disables the comment globally. This is the default behavior.
|
|
164
|
+
* `true` - Add `webpackFetchPriority` magic comments to **all** dynamic imports with the default value of `'auto'`.
|
|
165
|
+
* `string` - Add `webpackFetchPriority` magic comments to **all** dynamic imports with the provided string value as the priority. If the string is not `'high'`, `'low'`, or `'auto'` the comment will **not** be added.
|
|
166
|
+
* `(modulePath: string, importPath: string) => any` - Return a string to be used as the priority. Returning a falsy value or an unsupported string will **not** add the comment.
|
|
167
|
+
* `config.fetchPriority`:
|
|
168
|
+
* `'high' | 'low' | 'auto'` - Sets the fetch priority to the provided value when adding the comment.
|
|
169
|
+
* `(modulePath: string, importPath: string) => any` - Same as using a function for the loader option.
|
|
170
|
+
* `config.active`:
|
|
171
|
+
* `true` - Disable the comment.
|
|
172
|
+
* `false` - Enable the comment.
|
|
173
|
+
|
|
174
|
+
### `webpackMode`
|
|
175
|
+
|
|
176
|
+
**type**
|
|
177
|
+
```ts
|
|
178
|
+
boolean
|
|
179
|
+
| 'lazy' | 'lazy-once' | 'eager' | 'weak'
|
|
180
|
+
| ((modulePath: string, importPath: string) => any)
|
|
181
|
+
| CommentConfig
|
|
182
|
+
```
|
|
183
|
+
**default** None
|
|
184
|
+
|
|
185
|
+
Adds `webpackMode` magic comments.
|
|
186
|
+
|
|
187
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
188
|
+
```ts
|
|
189
|
+
{
|
|
190
|
+
mode: 'lazy' | 'lazy-once' | 'eager' | 'weak' | ((modulePath: string, importPath: string) => any);
|
|
191
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Possible values:
|
|
196
|
+
* `false` - Disables the comment globally. This is the default behavior.
|
|
197
|
+
* `true` - Add `webpackMode` magic comments to **all** dynamic imports with the default value of `'lazy'`.
|
|
198
|
+
* `string` - Add `webpackMode` magic comments to **all** dynamic imports with the provided string value as the mode. If the string is not `'lazy'`, `'lazy-once'`, `'eager'`, or `'weak'` the comment will **not** be added.
|
|
199
|
+
* `(modulePath: string, importPath: string) => any` - Return a string to be used as the mode. Returning a falsy value or an unsupported string will **not** add the comment.
|
|
200
|
+
* `config.mode`:
|
|
201
|
+
* `'lazy' | 'lazy-once' | 'eager' | 'weak'` - Sets the chunk loading mode to the provided value when adding the comment.
|
|
202
|
+
* `(modulePath: string, importPath: string) => any` - Same as using a function for the loader option.
|
|
203
|
+
* `config.active`:
|
|
204
|
+
* `true` - Disable the comment.
|
|
205
|
+
* `false` - Enable the comment.
|
|
206
|
+
|
|
207
|
+
### [`webpackPrefetch`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
208
|
+
|
|
209
|
+
**type**
|
|
210
|
+
```ts
|
|
211
|
+
boolean
|
|
212
|
+
| string
|
|
213
|
+
| string[]
|
|
214
|
+
| ((modulePath: string, importPath: string) => boolean)
|
|
215
|
+
| CommentConfig
|
|
216
|
+
```
|
|
217
|
+
**default** None
|
|
218
|
+
|
|
219
|
+
Adds `webpackPrefetch` magic comments.
|
|
220
|
+
|
|
221
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
222
|
+
```ts
|
|
223
|
+
{ active: boolean | ((modulePath: string, importPath: string) => boolean); }
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Possible values:
|
|
227
|
+
* `false` - Disables the comment globally. This is the default behavior.
|
|
228
|
+
* `true` - Add `webpackPrefetch` magic comments with a value of `true` to **all** dynamic imports.
|
|
229
|
+
* `string | string[]` - Add `webpackPrefetch` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
|
|
230
|
+
* `(modulePath: string, importPath: string) => boolean` - Returning `false` will disable adding the comment, otherwise it will be added.
|
|
231
|
+
* `config.active`:
|
|
232
|
+
* `true` - Disable the comment.
|
|
233
|
+
* `false` - Enable the comment.
|
|
234
|
+
|
|
235
|
+
### [`webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
236
|
+
|
|
237
|
+
**type**
|
|
238
|
+
```ts
|
|
239
|
+
boolean
|
|
240
|
+
| string
|
|
241
|
+
| string[]
|
|
242
|
+
| ((modulePath: string, importPath: string) => boolean)
|
|
243
|
+
| CommentConfig
|
|
244
|
+
```
|
|
245
|
+
**default** None
|
|
246
|
+
|
|
247
|
+
Adds `webpackPreload` magic comments.
|
|
248
|
+
|
|
249
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
250
|
+
```ts
|
|
251
|
+
{ active: boolean | ((modulePath: string, importPath: string) => boolean); }
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Possible values:
|
|
255
|
+
* `false` - Disables the comment globally. This is the default behavior.
|
|
256
|
+
* `true` - Add `webpackPreload` magic comments with a value of `true` to **all** dynamic imports.
|
|
257
|
+
* `string | string[]` - Add `webpackPreload` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
|
|
258
|
+
* `(modulePath: string, importPath: string) => boolean` - Returning `false` will disable adding the comment, otherwise it will be added.
|
|
259
|
+
* `config.active`:
|
|
260
|
+
* `true` - Disable the comment.
|
|
261
|
+
* `false` - Enable the comment.
|
|
262
|
+
|
|
263
|
+
### `webpackInclude`
|
|
264
|
+
|
|
265
|
+
**type**
|
|
266
|
+
```ts
|
|
267
|
+
RegExp
|
|
268
|
+
| ((modulePath: string, importPath: string) => RegExp)
|
|
269
|
+
| CommentConfig
|
|
270
|
+
```
|
|
271
|
+
**default** None
|
|
272
|
+
|
|
273
|
+
Adds `webpackInclude` magic comments.
|
|
274
|
+
|
|
275
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
276
|
+
```ts
|
|
277
|
+
{
|
|
278
|
+
include: (modulePath: string, importPath: string) => RegExp;
|
|
279
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Possible values:
|
|
284
|
+
* `RegExp` - Adds a `webpackInclude` comment to **all** dynamic imports using the provided regular expression.
|
|
285
|
+
* `(modulePath: string, importPath: string) => RegExp` - Adds a `webpackInclude` comment using the provided regular expression. Returning anything other than a regular expression does **not** add the comment.
|
|
286
|
+
* `config.include`:
|
|
287
|
+
* `RegExp` - Adds a `webpackInclude` comment to **all** dynamic imports, or only those matching a path from the [`match`](#match) path if using overrides.
|
|
288
|
+
* `(modulePath: string, importPath: string) => RegExp` - Same as using a function in the loader option.
|
|
289
|
+
* `config.active`:
|
|
290
|
+
* `true` - Disable the comment.
|
|
291
|
+
* `false` - Enable the comment.
|
|
292
|
+
|
|
293
|
+
### `webpackExclude`
|
|
294
|
+
|
|
295
|
+
**type**
|
|
296
|
+
```ts
|
|
297
|
+
RegExp
|
|
298
|
+
| ((modulePath: string, importPath: string) => RegExp)
|
|
299
|
+
| CommentConfig
|
|
300
|
+
```
|
|
301
|
+
**default** None
|
|
302
|
+
|
|
303
|
+
Adds `webpackExclude` magic comments.
|
|
304
|
+
|
|
305
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
306
|
+
```ts
|
|
307
|
+
{
|
|
308
|
+
exclude: (modulePath: string, importPath: string) => RegExp;
|
|
309
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Possible values:
|
|
314
|
+
* `RegExp` - Adds a `webpackExclude` comment to **all** dynamic imports using the provided regular expression.
|
|
315
|
+
* `(modulePath: string, importPath: string) => RegExp` - Adds a `webpackExclude` comment using the provided regular expression. Returning anything other than a regular expression does **not** add the comment.
|
|
316
|
+
* `config.exclude`:
|
|
317
|
+
* `RegExp` - Adds a `webpackExclude` comment to **all** dynamic imports, or only those matching a path from the [`match`](#match) path if using overrides.
|
|
318
|
+
* `(modulePath: string, importPath: string) => RegExp` - Same as using a function in the loader option.
|
|
319
|
+
* `config.active`:
|
|
320
|
+
* `true` - Disable the comment.
|
|
321
|
+
* `false` - Enable the comment.
|
|
322
|
+
|
|
323
|
+
### `webpackExports`
|
|
324
|
+
|
|
325
|
+
**type**
|
|
326
|
+
```ts
|
|
327
|
+
((modulePath: string, importPath: string) => string[])
|
|
328
|
+
| CommentConfig
|
|
329
|
+
```
|
|
330
|
+
**default** None
|
|
331
|
+
|
|
332
|
+
Adds `webpackExports` magic comments.
|
|
333
|
+
|
|
334
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
335
|
+
```ts
|
|
336
|
+
{
|
|
337
|
+
exports: (modulePath: string, importPath: string) => string[];
|
|
338
|
+
active: boolean | ((modulePath: string, importPath: string) => boolean);
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Possible values:
|
|
343
|
+
* `(modulePath: string, importPath: string) => string[]` - Adds a `webpackExports` comment using the strings in the returned array as the export names. Returning anything other than an array will **not** add the comment.
|
|
344
|
+
* `config.exports`:
|
|
345
|
+
* `(modulePath: string, importPath: string) => string[]` - Same as using a function in the loader option.
|
|
346
|
+
* `config.active`:
|
|
347
|
+
* `true` - Disable the comment.
|
|
348
|
+
* `false` - Enable the comment.
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
### `webpackIgnore`
|
|
352
|
+
|
|
353
|
+
**type**
|
|
354
|
+
```ts
|
|
355
|
+
boolean
|
|
356
|
+
| string
|
|
357
|
+
| string[]
|
|
358
|
+
| ((modulePath: string, importPath: string) => boolean)
|
|
359
|
+
| CommentConfig
|
|
360
|
+
```
|
|
361
|
+
**default** None
|
|
362
|
+
|
|
363
|
+
Adds `webpackIgnore` magic comments.
|
|
364
|
+
|
|
365
|
+
When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
|
|
366
|
+
```ts
|
|
367
|
+
{ active: boolean | ((modulePath: string, importPath: string) => boolean); }
|
|
368
|
+
```
|
|
21
369
|
|
|
22
|
-
|
|
370
|
+
Possible values:
|
|
371
|
+
* `false` - Disables the comment globally. This is the default behavior.
|
|
372
|
+
* `true` - Add `webpackIgnore` magic comments with a value of `true` to **all** dynamic imports. Effectively, opt-out of webpack code-splitting for dynamic imports.
|
|
373
|
+
* `string | string[]` - Add `webpackIgnore` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
|
|
374
|
+
* `(modulePath: string, importPath: string) => boolean` - Returning `false` will **not** add the comment, otherwise it will be added.
|
|
375
|
+
* `config.active`:
|
|
376
|
+
* `true` - Disable the comment.
|
|
377
|
+
* `false` - Enable the comment.
|
|
23
378
|
|
|
24
|
-
|
|
379
|
+
## Examples
|
|
25
380
|
|
|
26
|
-
|
|
381
|
+
Below are examples for some of the supported magic comments. Consult the [loader specification](https://github.com/morganney/magic-comments-loader/blob/main/__tests__/loader.spec.js) for a comprehensive usage example.
|
|
27
382
|
|
|
28
|
-
|
|
383
|
+
### webpackChunkName
|
|
29
384
|
|
|
30
|
-
|
|
385
|
+
Add `webpackChunkName` magic comments to dynamic imports matching the provided glob(s), using the import path in _kebab-case_ as the default chunk name. Glob matching is done using [micromatch](https://github.com/micromatch/micromatch).
|
|
31
386
|
|
|
387
|
+
**config**
|
|
32
388
|
```js
|
|
33
389
|
module: {
|
|
34
390
|
rules: [
|
|
35
391
|
{
|
|
36
|
-
test: /\.
|
|
37
|
-
|
|
38
|
-
|
|
392
|
+
test: /\.[jt]sx?$/,
|
|
393
|
+
use: {
|
|
394
|
+
loader: 'magic-comments-loader',
|
|
395
|
+
options: {
|
|
396
|
+
webpackChunkName: ['src/**/*.js']
|
|
397
|
+
}
|
|
398
|
+
}
|
|
39
399
|
}
|
|
40
400
|
]
|
|
41
401
|
}
|
|
42
402
|
```
|
|
403
|
+
**src**
|
|
404
|
+
```js
|
|
405
|
+
import('./folder/module.js')
|
|
406
|
+
```
|
|
407
|
+
**build**
|
|
408
|
+
```js
|
|
409
|
+
import(/* webpackChunkName: "folder-module" */ './folder/module.js')
|
|
410
|
+
```
|
|
43
411
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
The loader `options` is an object with keys corresponding to the names of supported magic comments. The following comments have a default behavior and do not require configuration beyond specifying where they should be applied (globally, or to certain files).
|
|
412
|
+
To define a custom chunk name, use a function as the option value. Returning nothing, or a falsy value skips adding the comment.
|
|
47
413
|
|
|
414
|
+
**config**
|
|
48
415
|
```js
|
|
49
416
|
module: {
|
|
50
417
|
rules: [
|
|
51
418
|
{
|
|
52
|
-
test: /\.
|
|
53
|
-
exclude: /node_modules/,
|
|
419
|
+
test: /\.[jt]sx?$/,
|
|
54
420
|
use: {
|
|
55
421
|
loader: 'magic-comments-loader',
|
|
56
422
|
options: {
|
|
57
|
-
webpackChunkName:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
'src/preload/**/*.js',
|
|
63
|
-
'!src/preload/skip/**/*.js'
|
|
64
|
-
]
|
|
423
|
+
webpackChunkName: (modulePath, importPath) => {
|
|
424
|
+
if (importPath.endsWith('module.js')) {
|
|
425
|
+
return 'custom-chunk-name'
|
|
426
|
+
}
|
|
427
|
+
}
|
|
65
428
|
}
|
|
66
429
|
}
|
|
67
430
|
}
|
|
@@ -69,20 +432,24 @@ module: {
|
|
|
69
432
|
}
|
|
70
433
|
```
|
|
71
434
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
435
|
+
**src**
|
|
436
|
+
```js
|
|
437
|
+
import('./folder/module.js')
|
|
438
|
+
```
|
|
75
439
|
|
|
76
|
-
|
|
440
|
+
**build**
|
|
441
|
+
```js
|
|
442
|
+
import(/* webpackChunkName: "custom-chunk-name" */ './folder/module.js')
|
|
443
|
+
```
|
|
77
444
|
|
|
78
|
-
|
|
445
|
+
Finally, using a [`CommentConfig`](#commentconfig) object you can change the chunk name to the import specifier's basename (instead of the full hyphenated path). This could potentially result in name collisions, so be mindful of import specifiers when activating. You could also achieve the same thing by using a function instead of `config.basename`.
|
|
79
446
|
|
|
447
|
+
**config**
|
|
80
448
|
```js
|
|
81
449
|
module: {
|
|
82
450
|
rules: [
|
|
83
451
|
{
|
|
84
|
-
test: /\.
|
|
85
|
-
exclude: /node_modules/,
|
|
452
|
+
test: /\.[jt]sx?$/,
|
|
86
453
|
use: {
|
|
87
454
|
loader: 'magic-comments-loader',
|
|
88
455
|
options: {
|
|
@@ -90,20 +457,6 @@ module: {
|
|
|
90
457
|
config: {
|
|
91
458
|
basename: true
|
|
92
459
|
}
|
|
93
|
-
},
|
|
94
|
-
webpackMode: {
|
|
95
|
-
config: {
|
|
96
|
-
mode: 'lazy-once'
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
webpackInclude: {
|
|
100
|
-
config: {
|
|
101
|
-
include: (modulePath, importPath) => {
|
|
102
|
-
if (/locales\/\${language}/.test(importPath)) {
|
|
103
|
-
return /\.json$/
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
460
|
}
|
|
108
461
|
}
|
|
109
462
|
}
|
|
@@ -112,79 +465,96 @@ module: {
|
|
|
112
465
|
}
|
|
113
466
|
```
|
|
114
467
|
|
|
115
|
-
|
|
468
|
+
**src**
|
|
469
|
+
```js
|
|
470
|
+
import('./folder/module.js')
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
**build**
|
|
474
|
+
```js
|
|
475
|
+
import(/* webpackChunkName: "module" */ './folder/module.js')
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
Most of the magic comments can be configured similarly, and **all** support configuration as a function with the signature `(modulePath: string, importPath: string) => any`, albeit the return type is checked at runtime for compliance with the expected values. Check out the loader [options](#options) for more details.
|
|
479
|
+
|
|
480
|
+
### Multiple
|
|
116
481
|
|
|
117
|
-
You can
|
|
482
|
+
You can add multiple magic comments.
|
|
118
483
|
|
|
484
|
+
**config**
|
|
119
485
|
```js
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
486
|
+
module: {
|
|
487
|
+
rules: [
|
|
488
|
+
{
|
|
489
|
+
test: /\.js$/,
|
|
490
|
+
use: {
|
|
491
|
+
loader: 'magic-comments-loader',
|
|
492
|
+
options: {
|
|
493
|
+
webpackChunkName: true,
|
|
494
|
+
webpackMode: 'lazy',
|
|
495
|
+
webpackFetchPriority: (modulePath, importPath) => {
|
|
496
|
+
if (importPath.includes('priority')) {
|
|
497
|
+
return 'high'
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
]
|
|
504
|
+
}
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
**src**
|
|
508
|
+
```js
|
|
509
|
+
import('./priority/module.js')
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
**build**
|
|
513
|
+
```js
|
|
514
|
+
import(/* webpackChunkName: "priority-module", webpackMode: "lazy", webpackFetchPriority: "high" */ './priority/module.js')
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### Overrides
|
|
518
|
+
|
|
519
|
+
When using a [`CommentConfig`](#commentconfig) object, you can override the configuration passed in the `config` key by defining `overrides`. It is an array of objects that look like:
|
|
520
|
+
|
|
521
|
+
```ts
|
|
522
|
+
{
|
|
523
|
+
files: string | string[];
|
|
524
|
+
config: CommentOptions;
|
|
525
|
+
}
|
|
526
|
+
```
|
|
136
527
|
|
|
528
|
+
The `files` and `config` keys are both required, where the former is glob string, or array thereof, and the latter is the associated magic comment's [`CommentOptions`](#commentoptions).
|
|
529
|
+
|
|
530
|
+
Here's a more complete example of how overrides can be applied:
|
|
531
|
+
|
|
532
|
+
**config**
|
|
137
533
|
```js
|
|
138
534
|
module: {
|
|
139
535
|
rules: [
|
|
140
536
|
{
|
|
141
537
|
test: /\.js$/,
|
|
142
|
-
exclude: /node_modules/,
|
|
143
538
|
use: {
|
|
144
539
|
loader: 'magic-comments-loader',
|
|
145
540
|
options: {
|
|
146
|
-
|
|
147
|
-
webpackChunkName:
|
|
148
|
-
config: {
|
|
149
|
-
basename: false
|
|
150
|
-
},
|
|
151
|
-
overrides: [
|
|
152
|
-
{
|
|
153
|
-
files: 'src/unique/**/*.js',
|
|
154
|
-
config: {
|
|
155
|
-
basename: true
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
files: 'src/off/**/*.js',
|
|
160
|
-
config: {
|
|
161
|
-
active: false
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
]
|
|
165
|
-
},
|
|
166
|
-
webpackPrefetch: [
|
|
167
|
-
'src/prefetch/**/*.js',
|
|
168
|
-
'!src/prefetch/skip/**/*.js'
|
|
169
|
-
],
|
|
541
|
+
match: 'import', // Now provided globs match against the import specifier
|
|
542
|
+
webpackChunkName: '*.json',
|
|
170
543
|
webpackMode: {
|
|
171
544
|
config: {
|
|
172
545
|
mode: 'lazy'
|
|
173
546
|
},
|
|
174
547
|
overrides: [
|
|
175
548
|
{
|
|
176
|
-
files: '
|
|
549
|
+
files: ['eager/**/*.js'],
|
|
177
550
|
config: {
|
|
178
|
-
|
|
551
|
+
mode: 'eager'
|
|
179
552
|
}
|
|
180
553
|
},
|
|
181
554
|
{
|
|
182
|
-
files: [
|
|
183
|
-
'src/**/*.js',
|
|
184
|
-
'!src/weak/**/*.js'
|
|
185
|
-
],
|
|
555
|
+
files: ['locales/**/*.json'],
|
|
186
556
|
config: {
|
|
187
|
-
mode: '
|
|
557
|
+
mode: 'lazy-once'
|
|
188
558
|
}
|
|
189
559
|
}
|
|
190
560
|
]
|
|
@@ -196,80 +566,18 @@ module: {
|
|
|
196
566
|
}
|
|
197
567
|
```
|
|
198
568
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
With loader options configured like
|
|
202
|
-
|
|
569
|
+
**src**
|
|
203
570
|
```js
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
webpackPrefetch: 'src/some/module.js',
|
|
209
|
-
webpackMode: 'eager'
|
|
210
|
-
}
|
|
211
|
-
}
|
|
571
|
+
const lang = 'es'
|
|
572
|
+
import('./folder/module.js')
|
|
573
|
+
import('./eager/module.js')
|
|
574
|
+
import(`./locales/${lang}.json`)
|
|
212
575
|
```
|
|
213
576
|
|
|
214
|
-
|
|
215
|
-
|
|
577
|
+
**build**
|
|
216
578
|
```js
|
|
217
|
-
const
|
|
579
|
+
const lang = 'es'
|
|
580
|
+
import(/* webpackMode: "lazy" */ './folder/module.js')
|
|
581
|
+
import(/* webpackMode: "eager" */ './eager/module.js')
|
|
582
|
+
import(/* webpackChunkName: "locales-[request]", webpackMode: "lazy-once" */ `./locales/${lang}.json`)
|
|
218
583
|
```
|
|
219
|
-
|
|
220
|
-
becomes
|
|
221
|
-
|
|
222
|
-
```js
|
|
223
|
-
const dynamicModule = await import(/* webpackChunkName: "path-to-module", webpackPrefetch: true, webpackMode: "eager" */ './path/to/module.js')
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
## Options
|
|
227
|
-
|
|
228
|
-
These are the options that can be configured under the loader `options`. When using comments with a [`config`](#with-config-options) key, you may also specify [`overrides`](#overrides).
|
|
229
|
-
|
|
230
|
-
* `verbose`: Boolean. Prints console statements of the module filepath and updated `import()` during the webpack build. Useful for debugging your custom configurations.
|
|
231
|
-
* `match`: `String(module|import)`. Sets how globs are matched, either the module file path or the `import()` path. Defaults to `'module'`.
|
|
232
|
-
* `webpackChunkName`
|
|
233
|
-
* `true`: Adds `webpackChunkName` comments to **all** dynamic imports. This is the default.
|
|
234
|
-
* `false`: Disables adding the `webpackChunkName` comment globally.
|
|
235
|
-
* `['/src/**/*.js']`: Adds the comment when the glob(s) match a path from a `match` path.
|
|
236
|
-
* `Function`: `(modulePath, importPath) => String(<chunk name>)`. Returning `false` does not add the comment.
|
|
237
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
238
|
-
* `config.basename`: Boolean. Use only the basename from the import path as the chunk name. Relative imports may result in name collisions. Use in areas where you know the basenames are unique.
|
|
239
|
-
* [`webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
240
|
-
* `true`: Adds `webpackPreload` comments to **all** dynamic imports.
|
|
241
|
-
* `false`: Disables adding the `webpackPreload` comment globally. This is the default.
|
|
242
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
243
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
244
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
245
|
-
* [`webpackPrefetch`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
246
|
-
* `true`: Adds `webpackPrefetch` comments to **all** dynamic imports.
|
|
247
|
-
* `false`: Disables adding the `webpackPrefetch` comment globally. This is the default.
|
|
248
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
249
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
250
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
251
|
-
* `webpackIgnore`
|
|
252
|
-
* `true`: Adds `webpackIgnore` comments to **all** dynamic imports. **You don't want to do this**.
|
|
253
|
-
* `false`: Disables adding the `webpackIgnore` comment globally. This is the default.
|
|
254
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
255
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
256
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
257
|
-
* `webpackMode`
|
|
258
|
-
* `true`: Adds `webpackMode` comments to **all** dynamic imports using `lazy`.
|
|
259
|
-
* `false`: Disables adding the comment globally. This is the default.
|
|
260
|
-
* `String(lazy|lazy-once|eager|weak)`: Adds the comment to **all** dynamic imports using the provided value.
|
|
261
|
-
* `Function`: `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip.
|
|
262
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
263
|
-
* `config.mode`: `String(lazy|lazy-once|eager|weak)` | `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip.
|
|
264
|
-
* `webpackExports`
|
|
265
|
-
* `Function`: `(modulePath, importPath) => [String(<module names|default>)]`. Return falsy value to skip.
|
|
266
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
267
|
-
* `config.exports`: `(modulePath, importPath) => [String(<module names|default>)]`. Return falsy value to skip.
|
|
268
|
-
* `webpackInclude`
|
|
269
|
-
* `Function`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
270
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
271
|
-
* `config.include`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
272
|
-
* `webpackExclude`
|
|
273
|
-
* `Function`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
274
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
275
|
-
* `config.exclude`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|