magic-comments-loader 1.4.2 → 1.5.1
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 +432 -194
- package/dist/booleanComment.js +18 -35
- package/dist/cjs/booleanComment.cjs +97 -0
- package/dist/cjs/comment.cjs +23 -0
- package/dist/cjs/enumComment.cjs +99 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/loader.cjs +25 -0
- package/dist/cjs/regexComment.cjs +101 -0
- package/dist/cjs/schema.cjs +37 -0
- package/dist/cjs/strategy.cjs +27 -0
- package/dist/cjs/util.cjs +62 -0
- package/dist/cjs/webpackChunkName.cjs +61 -0
- package/dist/cjs/webpackExclude.cjs +13 -0
- package/dist/cjs/webpackExports.cjs +72 -0
- package/dist/cjs/webpackFetchPriority.cjs +13 -0
- package/dist/cjs/webpackIgnore.cjs +18 -0
- package/dist/cjs/webpackInclude.cjs +13 -0
- package/dist/cjs/webpackMode.cjs +13 -0
- package/dist/cjs/webpackPrefetch.cjs +18 -0
- package/dist/cjs/webpackPreload.cjs +18 -0
- package/dist/comment.js +5 -24
- package/dist/enumComment.js +93 -0
- package/dist/index.js +1 -13
- package/dist/loader.js +9 -23
- package/dist/regexComment.js +14 -28
- package/dist/schema.js +19 -32
- package/dist/strategy.js +19 -32
- package/dist/util.js +6 -28
- package/dist/webpackChunkName.js +6 -22
- package/dist/webpackExclude.js +3 -12
- package/dist/webpackExports.js +8 -26
- package/dist/webpackFetchPriority.js +6 -0
- package/dist/webpackIgnore.js +4 -17
- package/dist/webpackInclude.js +3 -12
- package/dist/webpackMode.js +6 -113
- package/dist/webpackPrefetch.js +4 -17
- package/dist/webpackPreload.js +4 -17
- package/package.json +35 -20
- package/dist/package.json +0 -55
package/README.md
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
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
|
-
NOTE: **This loader ignores dynamic imports that already include comments of any kind**.
|
|
9
|
-
|
|
10
|
-
All magic comments are supported:
|
|
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`
|
|
19
|
-
|
|
20
8
|
## Getting Started
|
|
21
9
|
|
|
22
10
|
First install `magic-comments-loader`:
|
|
@@ -25,14 +13,13 @@ First install `magic-comments-loader`:
|
|
|
25
13
|
npm install magic-comments-loader
|
|
26
14
|
```
|
|
27
15
|
|
|
28
|
-
Next add the loader to your `webpack`
|
|
16
|
+
Next add the loader to your `webpack.config.js` file:
|
|
29
17
|
|
|
30
18
|
```js
|
|
31
19
|
module: {
|
|
32
20
|
rules: [
|
|
33
21
|
{
|
|
34
|
-
test: /\.
|
|
35
|
-
exclude: /node_modules/,
|
|
22
|
+
test: /\.[jt]sx?$/,
|
|
36
23
|
use: ['magic-comments-loader']
|
|
37
24
|
}
|
|
38
25
|
]
|
|
@@ -45,88 +32,399 @@ Then given a **file.js** with the following import:
|
|
|
45
32
|
const dynamicModule = await import('./path/to/module.js')
|
|
46
33
|
```
|
|
47
34
|
|
|
48
|
-
While running `webpack` the import inside **file.js** becomes:
|
|
35
|
+
While running `webpack` the dynamic import inside **file.js** becomes:
|
|
49
36
|
|
|
50
37
|
```js
|
|
51
38
|
const dynamicModule = await import(/* webpackChunkName: "path-to-module" */ './path/to/module.js')
|
|
52
39
|
```
|
|
53
40
|
|
|
54
|
-
|
|
41
|
+
The `webpackChunkName` comment is added by default when registering the loader. See the supported [options](#options) to learn about configuring other magic comments.
|
|
55
42
|
|
|
56
|
-
|
|
43
|
+
## Options
|
|
57
44
|
|
|
58
|
-
|
|
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
|
+
```
|
|
59
71
|
|
|
60
|
-
|
|
72
|
+
#### `CommentOptions`
|
|
61
73
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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)
|
|
71
84
|
}
|
|
72
85
|
```
|
|
73
86
|
|
|
74
|
-
|
|
87
|
+
You can skip to the [overrides example](#overrides) to get a better sense of how this all works.
|
|
75
88
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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);
|
|
94
280
|
}
|
|
95
281
|
```
|
|
96
282
|
|
|
97
|
-
|
|
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
|
+
```
|
|
98
341
|
|
|
99
|
-
|
|
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.
|
|
100
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
|
+
```
|
|
369
|
+
|
|
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.
|
|
378
|
+
|
|
379
|
+
## Examples
|
|
380
|
+
|
|
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.
|
|
382
|
+
|
|
383
|
+
### webpackChunkName
|
|
384
|
+
|
|
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).
|
|
386
|
+
|
|
387
|
+
**config**
|
|
101
388
|
```js
|
|
102
389
|
module: {
|
|
103
390
|
rules: [
|
|
104
391
|
{
|
|
105
|
-
test: /\.
|
|
106
|
-
exclude: /node_modules/,
|
|
392
|
+
test: /\.[jt]sx?$/,
|
|
107
393
|
use: {
|
|
108
394
|
loader: 'magic-comments-loader',
|
|
109
395
|
options: {
|
|
110
|
-
|
|
396
|
+
webpackChunkName: ['src/**/*.js']
|
|
111
397
|
}
|
|
112
398
|
}
|
|
113
399
|
}
|
|
114
400
|
]
|
|
115
401
|
}
|
|
116
402
|
```
|
|
403
|
+
**src**
|
|
404
|
+
```js
|
|
405
|
+
import('./folder/module.js')
|
|
406
|
+
```
|
|
407
|
+
**build**
|
|
408
|
+
```js
|
|
409
|
+
import(/* webpackChunkName: "folder-module" */ './folder/module.js')
|
|
410
|
+
```
|
|
117
411
|
|
|
118
|
-
|
|
412
|
+
To define a custom chunk name, use a function as the option value. Returning nothing, or a falsy value skips adding the comment.
|
|
119
413
|
|
|
414
|
+
**config**
|
|
120
415
|
```js
|
|
121
416
|
module: {
|
|
122
417
|
rules: [
|
|
123
418
|
{
|
|
124
|
-
test: /\.
|
|
125
|
-
exclude: /node_modules/,
|
|
419
|
+
test: /\.[jt]sx?$/,
|
|
126
420
|
use: {
|
|
127
421
|
loader: 'magic-comments-loader',
|
|
128
422
|
options: {
|
|
129
|
-
|
|
423
|
+
webpackChunkName: (modulePath, importPath) => {
|
|
424
|
+
if (importPath.endsWith('module.js')) {
|
|
425
|
+
return 'custom-chunk-name'
|
|
426
|
+
}
|
|
427
|
+
}
|
|
130
428
|
}
|
|
131
429
|
}
|
|
132
430
|
}
|
|
@@ -134,31 +432,32 @@ module: {
|
|
|
134
432
|
}
|
|
135
433
|
```
|
|
136
434
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
435
|
+
**src**
|
|
436
|
+
```js
|
|
437
|
+
import('./folder/module.js')
|
|
438
|
+
```
|
|
140
439
|
|
|
141
|
-
|
|
440
|
+
**build**
|
|
441
|
+
```js
|
|
442
|
+
import(/* webpackChunkName: "custom-chunk-name" */ './folder/module.js')
|
|
443
|
+
```
|
|
142
444
|
|
|
143
|
-
|
|
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`.
|
|
144
446
|
|
|
447
|
+
**config**
|
|
145
448
|
```js
|
|
146
449
|
module: {
|
|
147
450
|
rules: [
|
|
148
451
|
{
|
|
149
|
-
test: /\.
|
|
150
|
-
exclude: /node_modules/,
|
|
452
|
+
test: /\.[jt]sx?$/,
|
|
151
453
|
use: {
|
|
152
454
|
loader: 'magic-comments-loader',
|
|
153
455
|
options: {
|
|
154
|
-
webpackChunkName:
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
'src/preload/**/*.js',
|
|
160
|
-
'!src/preload/skip/**/*.js'
|
|
161
|
-
]
|
|
456
|
+
webpackChunkName: {
|
|
457
|
+
config: {
|
|
458
|
+
basename: true
|
|
459
|
+
}
|
|
460
|
+
}
|
|
162
461
|
}
|
|
163
462
|
}
|
|
164
463
|
}
|
|
@@ -166,40 +465,36 @@ module: {
|
|
|
166
465
|
}
|
|
167
466
|
```
|
|
168
467
|
|
|
169
|
-
|
|
468
|
+
**src**
|
|
469
|
+
```js
|
|
470
|
+
import('./folder/module.js')
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
**build**
|
|
474
|
+
```js
|
|
475
|
+
import(/* webpackChunkName: "module" */ './folder/module.js')
|
|
476
|
+
```
|
|
170
477
|
|
|
171
|
-
|
|
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.
|
|
172
479
|
|
|
173
|
-
|
|
480
|
+
### Multiple
|
|
174
481
|
|
|
175
|
-
|
|
482
|
+
You can add multiple magic comments.
|
|
176
483
|
|
|
484
|
+
**config**
|
|
177
485
|
```js
|
|
178
486
|
module: {
|
|
179
487
|
rules: [
|
|
180
488
|
{
|
|
181
489
|
test: /\.js$/,
|
|
182
|
-
exclude: /node_modules/,
|
|
183
490
|
use: {
|
|
184
491
|
loader: 'magic-comments-loader',
|
|
185
492
|
options: {
|
|
186
|
-
webpackChunkName:
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
webpackMode: {
|
|
192
|
-
config: {
|
|
193
|
-
mode: 'lazy-once'
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
webpackInclude: {
|
|
197
|
-
config: {
|
|
198
|
-
include: (modulePath, importPath) => {
|
|
199
|
-
if (/locales\/\${language}/.test(importPath)) {
|
|
200
|
-
return /\.json$/
|
|
201
|
-
}
|
|
202
|
-
}
|
|
493
|
+
webpackChunkName: true,
|
|
494
|
+
webpackMode: 'lazy',
|
|
495
|
+
webpackFetchPriority: (modulePath, importPath) => {
|
|
496
|
+
if (importPath.includes('priority')) {
|
|
497
|
+
return 'high'
|
|
203
498
|
}
|
|
204
499
|
}
|
|
205
500
|
}
|
|
@@ -209,79 +504,57 @@ module: {
|
|
|
209
504
|
}
|
|
210
505
|
```
|
|
211
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
|
+
|
|
212
517
|
### Overrides
|
|
213
518
|
|
|
214
|
-
|
|
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:
|
|
215
520
|
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
]
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
Here's a more complete example using `config` and `overrides` to customize how comments are applied:
|
|
521
|
+
```ts
|
|
522
|
+
{
|
|
523
|
+
files: string | string[];
|
|
524
|
+
config: CommentOptions;
|
|
525
|
+
}
|
|
526
|
+
```
|
|
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:
|
|
233
531
|
|
|
532
|
+
**config**
|
|
234
533
|
```js
|
|
235
534
|
module: {
|
|
236
535
|
rules: [
|
|
237
536
|
{
|
|
238
537
|
test: /\.js$/,
|
|
239
|
-
exclude: /node_modules/,
|
|
240
538
|
use: {
|
|
241
539
|
loader: 'magic-comments-loader',
|
|
242
540
|
options: {
|
|
243
|
-
|
|
244
|
-
webpackChunkName:
|
|
245
|
-
config: {
|
|
246
|
-
basename: false
|
|
247
|
-
},
|
|
248
|
-
overrides: [
|
|
249
|
-
{
|
|
250
|
-
files: 'src/unique/**/*.js',
|
|
251
|
-
config: {
|
|
252
|
-
basename: true
|
|
253
|
-
}
|
|
254
|
-
},
|
|
255
|
-
{
|
|
256
|
-
files: 'src/off/**/*.js',
|
|
257
|
-
config: {
|
|
258
|
-
active: false
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
]
|
|
262
|
-
},
|
|
263
|
-
webpackPrefetch: [
|
|
264
|
-
'src/prefetch/**/*.js',
|
|
265
|
-
'!src/prefetch/skip/**/*.js'
|
|
266
|
-
],
|
|
541
|
+
match: 'import', // Now provided globs match against the import specifier
|
|
542
|
+
webpackChunkName: '*.json',
|
|
267
543
|
webpackMode: {
|
|
268
544
|
config: {
|
|
269
545
|
mode: 'lazy'
|
|
270
546
|
},
|
|
271
547
|
overrides: [
|
|
272
548
|
{
|
|
273
|
-
files: '
|
|
549
|
+
files: ['eager/**/*.js'],
|
|
274
550
|
config: {
|
|
275
|
-
|
|
551
|
+
mode: 'eager'
|
|
276
552
|
}
|
|
277
553
|
},
|
|
278
554
|
{
|
|
279
|
-
files: [
|
|
280
|
-
'src/**/*.js',
|
|
281
|
-
'!src/weak/**/*.js'
|
|
282
|
-
],
|
|
555
|
+
files: ['locales/**/*.json'],
|
|
283
556
|
config: {
|
|
284
|
-
mode: '
|
|
557
|
+
mode: 'lazy-once'
|
|
285
558
|
}
|
|
286
559
|
}
|
|
287
560
|
]
|
|
@@ -293,53 +566,18 @@ module: {
|
|
|
293
566
|
}
|
|
294
567
|
```
|
|
295
568
|
|
|
296
|
-
|
|
569
|
+
**src**
|
|
570
|
+
```js
|
|
571
|
+
const lang = 'es'
|
|
572
|
+
import('./folder/module.js')
|
|
573
|
+
import('./eager/module.js')
|
|
574
|
+
import(`./locales/${lang}.json`)
|
|
575
|
+
```
|
|
297
576
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
* `['/src/**/*.js']`: Adds the comment when the glob(s) match a path from a `match` path.
|
|
306
|
-
* `Function`: `(modulePath, importPath) => String(<chunk name>)`. Returning `false` does not add the comment.
|
|
307
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
308
|
-
* `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.
|
|
309
|
-
* [`webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
310
|
-
* `true`: Adds `webpackPreload` comments to **all** dynamic imports.
|
|
311
|
-
* `false`: Disables adding the `webpackPreload` comment globally. This is the default.
|
|
312
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
313
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
314
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
315
|
-
* [`webpackPrefetch`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
|
|
316
|
-
* `true`: Adds `webpackPrefetch` comments to **all** dynamic imports.
|
|
317
|
-
* `false`: Disables adding the `webpackPrefetch` comment globally. This is the default.
|
|
318
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
319
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
320
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
321
|
-
* `webpackIgnore`
|
|
322
|
-
* `true`: Adds `webpackIgnore` comments to **all** dynamic imports.
|
|
323
|
-
* `false`: Disables adding the `webpackIgnore` comment globally. This is the default.
|
|
324
|
-
* `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
|
|
325
|
-
* `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
326
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
327
|
-
* `webpackMode`
|
|
328
|
-
* `true`: Adds `webpackMode` comments to **all** dynamic imports using `lazy`.
|
|
329
|
-
* `false`: Disables adding the comment globally. This is the default.
|
|
330
|
-
* `String(lazy|lazy-once|eager|weak)`: Adds the comment to **all** dynamic imports using the provided value.
|
|
331
|
-
* `Function`: `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip.
|
|
332
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
333
|
-
* `config.mode`: `String(lazy|lazy-once|eager|weak)` | `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip.
|
|
334
|
-
* `webpackExports`
|
|
335
|
-
* `Function`: `(modulePath, importPath) => [String(<module names|default>)]`. Return falsy value to skip.
|
|
336
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
337
|
-
* `config.exports`: `(modulePath, importPath) => [String(<module names|default>)]`. Return falsy value to skip.
|
|
338
|
-
* `webpackInclude`
|
|
339
|
-
* `Function`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
340
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
341
|
-
* `config.include`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
342
|
-
* `webpackExclude`
|
|
343
|
-
* `Function`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
344
|
-
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
|
|
345
|
-
* `config.exclude`: `(modulePath, importPath) => RegExp`. Return falsy value to skip.
|
|
577
|
+
**build**
|
|
578
|
+
```js
|
|
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`)
|
|
583
|
+
```
|