magic-comments-loader 1.6.0 → 2.0.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 CHANGED
@@ -1,7 +1,8 @@
1
1
  # [`magic-comments-loader`](https://www.npmjs.com/package/magic-comments-loader) 🪄
2
2
 
3
3
  ![CI](https://github.com/morganney/magic-comments-loader/actions/workflows/ci.yml/badge.svg)
4
- [![codecov](https://codecov.io/gh/morganney/magic-comments-loader/branch/master/graph/badge.svg?token=1DWQL43B8V)](https://codecov.io/gh/morganney/magic-comments-loader)
4
+ [![codecov](https://codecov.io/gh/morganney/magic-comments-loader/branch/main/graph/badge.svg?token=1DWQL43B8V)](https://codecov.io/gh/morganney/magic-comments-loader)
5
+ [![NPM version](https://img.shields.io/npm/v/magic-comments-loader.svg)](https://www.npmjs.com/package/magic-comments-loader)
5
6
 
6
7
  Keep your source code clean, add [magic comments](https://webpack.js.org/api/module-methods/#magic-comments) to your dynamic `import()` expressions at build time.
7
8
 
@@ -42,50 +43,10 @@ The `webpackChunkName` comment is added by default when registering the loader.
42
43
 
43
44
  ## Options
44
45
 
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
46
  * [`verbose`](#verbose)
48
47
  * [`mode`](#mode)
49
48
  * [`match`](#match)
50
- * [`webpackChunkName`](#webpackchunkname)
51
- * [`webpackFetchPriority`](#webpackfetchpriority)
52
- * [`webpackMode`](#webpackmode)
53
- * [`webpackPrefetch`](#webpackprefetch)
54
- * [`webpackPreload`](#webpackpreload)
55
- * [`webpackInclude`](#webpackinclude)
56
- * [`webpackExclude`](#webpackexclude)
57
- * [`webpackExports`](#webpackexports)
58
- * [`webpackIgnore`](#webpackignore)
59
-
60
- ### `CommentConfig`
61
- 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:
62
-
63
- ```ts
64
- interface CommentConfig {
65
- config: CommentOptions;
66
- overrides?: Array<{
67
- files: string | string[];
68
- config: CommentOptions;
69
- }>;
70
- }
71
- ```
72
-
73
- #### `CommentOptions`
74
-
75
- The exact `CommentOptions` shape defining `config` is determined by the loader option it is associated with, but the interface always extends `CommentOptionsBase`:
76
- ```ts
77
- interface CommentOptions extends CommentOptionsBase {
78
- // In general, a "falsy" value disables the comment.
79
- [option: string]: boolean | string | Function | RegExp | undefined;
80
- }
81
-
82
- interface CommentOptionsBase {
83
- // Can be used to turn a magic comment on or off.
84
- active?: boolean | ((modulePath: string, importPath: string) => boolean)
85
- }
86
- ```
87
-
88
- You can skip to the [overrides example](#overrides) to get a better sense of how this all works.
49
+ * `[magicCommentName: string]: MagicCommentValue` see `magic-comments` [options](https://github.com/morganney/magic-comments#options) for details
89
50
 
90
51
  ### `verbose`
91
52
  **type**
@@ -114,278 +75,6 @@ Sets how the loader finds dynamic import expressions in your source code, either
114
75
 
115
76
  Sets how globs are matched, either the module file path, or the `import()` specifier.
116
77
 
117
- ### `webpackChunkName`
118
-
119
- **type**
120
- ```ts
121
- boolean
122
- | string
123
- | string[]
124
- | ((modulePath: string, importPath: string) => any)
125
- | CommentConfig
126
- ```
127
- **default** `true`
128
-
129
- Adds `webpackChunkName` magic comments. This option is enabled by default when registering the loader in your webpack configuration.
130
-
131
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
132
- ```ts
133
- {
134
- basename: boolean;
135
- active: boolean | ((modulePath: string, importPath: string) => boolean);
136
- }
137
- ```
138
-
139
- Possible values:
140
- * `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.
141
- * `false` - Disables adding the `webpackChunkName` comment globally.
142
- * `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.
143
- * `(modulePath: string, importPath: string) => any` - Return a string to be used as the chunk name. Returning a falsy value will skip adding the comment.
144
- * `config.basename`:
145
- * `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.
146
- * `false` - Use the full derived path from the import specifier in kebab-case as the chunk name, same as the default behavior.
147
- * `config.active`:
148
- * `true` - Disable the comment.
149
- * `false` - Enable the comment.
150
-
151
- ### `webpackFetchPriority`
152
-
153
- **type**
154
- ```ts
155
- boolean
156
- | 'high' | 'low' | 'auto'
157
- | ((modulePath: string, importPath: string) => any)
158
- | CommentConfig
159
- ```
160
- **default** None
161
-
162
- Adds `webpackFetchPriority` magic comments.
163
-
164
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
165
- ```ts
166
- {
167
- fetchPriority: 'high' | 'low' | 'auto' | ((modulePath: string, importPath: string) => any);
168
- active: boolean | ((modulePath: string, importPath: string) => boolean);
169
- }
170
- ```
171
-
172
- Possible values:
173
- * `false` - Disables the comment globally. This is the default behavior.
174
- * `true` - Add `webpackFetchPriority` magic comments to **all** dynamic imports with the default value of `'auto'`.
175
- * `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.
176
- * `(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.
177
- * `config.fetchPriority`:
178
- * `'high' | 'low' | 'auto'` - Sets the fetch priority to the provided value when adding the comment.
179
- * `(modulePath: string, importPath: string) => any` - Same as using a function for the loader option.
180
- * `config.active`:
181
- * `true` - Disable the comment.
182
- * `false` - Enable the comment.
183
-
184
- ### `webpackMode`
185
-
186
- **type**
187
- ```ts
188
- boolean
189
- | 'lazy' | 'lazy-once' | 'eager' | 'weak'
190
- | ((modulePath: string, importPath: string) => any)
191
- | CommentConfig
192
- ```
193
- **default** None
194
-
195
- Adds `webpackMode` magic comments.
196
-
197
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
198
- ```ts
199
- {
200
- mode: 'lazy' | 'lazy-once' | 'eager' | 'weak' | ((modulePath: string, importPath: string) => any);
201
- active: boolean | ((modulePath: string, importPath: string) => boolean);
202
- }
203
- ```
204
-
205
- Possible values:
206
- * `false` - Disables the comment globally. This is the default behavior.
207
- * `true` - Add `webpackMode` magic comments to **all** dynamic imports with the default value of `'lazy'`.
208
- * `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.
209
- * `(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.
210
- * `config.mode`:
211
- * `'lazy' | 'lazy-once' | 'eager' | 'weak'` - Sets the chunk loading mode to the provided value when adding the comment.
212
- * `(modulePath: string, importPath: string) => any` - Same as using a function for the loader option.
213
- * `config.active`:
214
- * `true` - Disable the comment.
215
- * `false` - Enable the comment.
216
-
217
- ### [`webpackPrefetch`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
218
-
219
- **type**
220
- ```ts
221
- boolean
222
- | string
223
- | string[]
224
- | ((modulePath: string, importPath: string) => boolean)
225
- | CommentConfig
226
- ```
227
- **default** None
228
-
229
- Adds `webpackPrefetch` magic comments.
230
-
231
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
232
- ```ts
233
- { active: boolean | ((modulePath: string, importPath: string) => boolean); }
234
- ```
235
-
236
- Possible values:
237
- * `false` - Disables the comment globally. This is the default behavior.
238
- * `true` - Add `webpackPrefetch` magic comments with a value of `true` to **all** dynamic imports.
239
- * `string | string[]` - Add `webpackPrefetch` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
240
- * `(modulePath: string, importPath: string) => boolean` - Returning `false` will disable adding the comment, otherwise it will be added.
241
- * `config.active`:
242
- * `true` - Disable the comment.
243
- * `false` - Enable the comment.
244
-
245
- ### [`webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules)
246
-
247
- **type**
248
- ```ts
249
- boolean
250
- | string
251
- | string[]
252
- | ((modulePath: string, importPath: string) => boolean)
253
- | CommentConfig
254
- ```
255
- **default** None
256
-
257
- Adds `webpackPreload` magic comments.
258
-
259
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
260
- ```ts
261
- { active: boolean | ((modulePath: string, importPath: string) => boolean); }
262
- ```
263
-
264
- Possible values:
265
- * `false` - Disables the comment globally. This is the default behavior.
266
- * `true` - Add `webpackPreload` magic comments with a value of `true` to **all** dynamic imports.
267
- * `string | string[]` - Add `webpackPreload` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
268
- * `(modulePath: string, importPath: string) => boolean` - Returning `false` will disable adding the comment, otherwise it will be added.
269
- * `config.active`:
270
- * `true` - Disable the comment.
271
- * `false` - Enable the comment.
272
-
273
- ### `webpackInclude`
274
-
275
- **type**
276
- ```ts
277
- RegExp
278
- | ((modulePath: string, importPath: string) => RegExp)
279
- | CommentConfig
280
- ```
281
- **default** None
282
-
283
- Adds `webpackInclude` magic comments.
284
-
285
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
286
- ```ts
287
- {
288
- include: (modulePath: string, importPath: string) => RegExp;
289
- active: boolean | ((modulePath: string, importPath: string) => boolean);
290
- }
291
- ```
292
-
293
- Possible values:
294
- * `RegExp` - Adds a `webpackInclude` comment to **all** dynamic imports using the provided regular expression.
295
- * `(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.
296
- * `config.include`:
297
- * `RegExp` - Adds a `webpackInclude` comment to **all** dynamic imports, or only those matching a path from the [`match`](#match) path if using overrides.
298
- * `(modulePath: string, importPath: string) => RegExp` - Same as using a function in the loader option.
299
- * `config.active`:
300
- * `true` - Disable the comment.
301
- * `false` - Enable the comment.
302
-
303
- ### `webpackExclude`
304
-
305
- **type**
306
- ```ts
307
- RegExp
308
- | ((modulePath: string, importPath: string) => RegExp)
309
- | CommentConfig
310
- ```
311
- **default** None
312
-
313
- Adds `webpackExclude` magic comments.
314
-
315
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
316
- ```ts
317
- {
318
- exclude: (modulePath: string, importPath: string) => RegExp;
319
- active: boolean | ((modulePath: string, importPath: string) => boolean);
320
- }
321
- ```
322
-
323
- Possible values:
324
- * `RegExp` - Adds a `webpackExclude` comment to **all** dynamic imports using the provided regular expression.
325
- * `(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.
326
- * `config.exclude`:
327
- * `RegExp` - Adds a `webpackExclude` comment to **all** dynamic imports, or only those matching a path from the [`match`](#match) path if using overrides.
328
- * `(modulePath: string, importPath: string) => RegExp` - Same as using a function in the loader option.
329
- * `config.active`:
330
- * `true` - Disable the comment.
331
- * `false` - Enable the comment.
332
-
333
- ### `webpackExports`
334
-
335
- **type**
336
- ```ts
337
- ((modulePath: string, importPath: string) => string[])
338
- | CommentConfig
339
- ```
340
- **default** None
341
-
342
- Adds `webpackExports` magic comments.
343
-
344
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
345
- ```ts
346
- {
347
- exports: (modulePath: string, importPath: string) => string[];
348
- active: boolean | ((modulePath: string, importPath: string) => boolean);
349
- }
350
- ```
351
-
352
- Possible values:
353
- * `(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.
354
- * `config.exports`:
355
- * `(modulePath: string, importPath: string) => string[]` - Same as using a function in the loader option.
356
- * `config.active`:
357
- * `true` - Disable the comment.
358
- * `false` - Enable the comment.
359
-
360
-
361
- ### `webpackIgnore`
362
-
363
- **type**
364
- ```ts
365
- boolean
366
- | string
367
- | string[]
368
- | ((modulePath: string, importPath: string) => boolean)
369
- | CommentConfig
370
- ```
371
- **default** None
372
-
373
- Adds `webpackIgnore` magic comments.
374
-
375
- When using a [`CommentConfig`](#commentconfig) the following comment options are supported:
376
- ```ts
377
- { active: boolean | ((modulePath: string, importPath: string) => boolean); }
378
- ```
379
-
380
- Possible values:
381
- * `false` - Disables the comment globally. This is the default behavior.
382
- * `true` - Add `webpackIgnore` magic comments with a value of `true` to **all** dynamic imports. Effectively, opt-out of webpack code-splitting for dynamic imports.
383
- * `string | string[]` - Add `webpackIgnore` comment with a value of `true` when the glob(s) match a path from a [`match`](#match) path.
384
- * `(modulePath: string, importPath: string) => boolean` - Returning `false` will **not** add the comment, otherwise it will be added.
385
- * `config.active`:
386
- * `true` - Disable the comment.
387
- * `false` - Enable the comment.
388
-
389
78
  ## Examples
390
79
 
391
80
  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.
@@ -452,7 +141,7 @@ import('./folder/module.js')
452
141
  import(/* webpackChunkName: "custom-chunk-name" */ './folder/module.js')
453
142
  ```
454
143
 
455
- 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
+ Finally, using a [`CommentConfig`](https://github.com/morganney/magic-comments#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 `options.basename`.
456
145
 
457
146
  **config**
458
147
  ```js
@@ -464,7 +153,7 @@ module: {
464
153
  loader: 'magic-comments-loader',
465
154
  options: {
466
155
  webpackChunkName: {
467
- config: {
156
+ options: {
468
157
  basename: true
469
158
  }
470
159
  }
@@ -485,7 +174,7 @@ import('./folder/module.js')
485
174
  import(/* webpackChunkName: "module" */ './folder/module.js')
486
175
  ```
487
176
 
488
- 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.
177
+ 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 [options](https://github.com/morganney/magic-comments#options) for `magic-comments` more details.
489
178
 
490
179
  ### Multiple
491
180
 
@@ -526,16 +215,16 @@ import(/* webpackChunkName: "priority-module", webpackMode: "lazy", webpackFetch
526
215
 
527
216
  ### Overrides
528
217
 
529
- 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:
218
+ When using a [`CommentConfig`](https://github.com/morganney/magic-comments#commentconfig) object, you can override the configuration passed in the `options` key by defining `overrides`. It is an array of objects that look like:
530
219
 
531
220
  ```ts
532
- {
221
+ Array<{
533
222
  files: string | string[];
534
- config: CommentOptions;
535
- }
223
+ options: CommentOptions;
224
+ }>
536
225
  ```
537
226
 
538
- The `files` and `config` keys are both required, where the former is a glob string, or an array thereof, and the latter is the associated magic comment's [`CommentOptions`](#commentoptions).
227
+ The `files` and `options` keys are both required, where the former is a glob string, or an array thereof, and the latter is the associated magic comment's [`CommentOptions`](https://github.com/morganney/magic-comments#commentoptions).
539
228
 
540
229
  Here's a more complete example of how overrides can be applied:
541
230
 
@@ -551,19 +240,19 @@ module: {
551
240
  match: 'import', // Now provided globs match against the import specifier
552
241
  webpackChunkName: '*.json',
553
242
  webpackMode: {
554
- config: {
243
+ options: {
555
244
  mode: 'lazy'
556
245
  },
557
246
  overrides: [
558
247
  {
559
248
  files: ['eager/**/*.js'],
560
- config: {
249
+ options: {
561
250
  mode: 'eager'
562
251
  }
563
252
  },
564
253
  {
565
254
  files: ['locales/**/*.json'],
566
- config: {
255
+ options: {
567
256
  mode: 'lazy-once'
568
257
  }
569
258
  }
@@ -592,6 +281,8 @@ import(/* webpackMode: "eager" */ './eager/module.js')
592
281
  import(/* webpackChunkName: "locales-[request]", webpackMode: "lazy-once" */ `./locales/${lang}.json`)
593
282
  ```
594
283
 
284
+ You can also see the [example for overrides in `magic-comments`](https://github.com/morganney/magic-comments#overrides).
285
+
595
286
  ### TypeScript
596
287
 
597
288
  When using TypeScript or experimental ECMAScript features <= [stage 3](https://tc39.es/process-document/), i.e. non spec compliant, you must chain the appropriate loaders with `magic-comments-loader` coming after.
@@ -4,18 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getCommenter = void 0;
7
- var _strategy = require("./strategy.cjs");
8
- var _util = require("./util.cjs");
7
+ var _magicComments = require("magic-comments");
9
8
  const getCommenter = (filepath, options, logger) => (rgxMatch, capturedImportPath) => {
10
9
  const importPath = capturedImportPath.trim();
11
- const bareImportPath = (0, _util.getBareImportSpecifier)(importPath);
12
10
  const {
13
11
  verbose,
14
12
  match,
15
13
  magicCommentOptions
16
14
  } = options;
17
- const magicComment = Object.keys(magicCommentOptions).map(key => _strategy.commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
18
- const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `/* ${magicComment.join(', ')} */ ${importPath}` : importPath);
15
+ const magicComment = (0, _magicComments.getMagicComment)({
16
+ match,
17
+ importPath,
18
+ modulePath: filepath,
19
+ options: magicCommentOptions
20
+ });
21
+ const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `${magicComment} ${importPath}` : importPath);
19
22
  if (verbose) {
20
23
  logger.info(`${filepath} : ${magicImport}`);
21
24
  }
@@ -4,9 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.format = void 0;
7
+ var _magicComments = require("magic-comments");
7
8
  var _magicString = _interopRequireDefault(require("magic-string"));
8
- var _strategy = require("./strategy.cjs");
9
- var _util = require("./util.cjs");
10
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
10
  const format = ({
12
11
  match,
@@ -17,7 +16,6 @@ const format = ({
17
16
  importExpressionNodes
18
17
  }) => {
19
18
  const magicImports = [];
20
- const step = 'import('.length;
21
19
  const cmts = [...comments];
22
20
  const src = new _magicString.default(source);
23
21
  const hasComment = node => {
@@ -30,13 +28,16 @@ const format = ({
30
28
  };
31
29
  for (const node of importExpressionNodes) {
32
30
  if (!hasComment(node)) {
33
- const specifier = source.substring(node.start + step, node.end - 1);
34
- const bareImportPath = (0, _util.getBareImportSpecifier)(specifier);
35
- const magic = Object.keys(magicCommentOptions).map(key => _strategy.commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
36
- if (magic.length) {
37
- const magicComment = `/* ${magic.join(', ')} */ `;
38
- magicImports.push(src.snip(node.start, node.end).toString().replace(specifier, `${magicComment}${specifier}`));
39
- src.appendRight(node.start + step, `${magicComment}`);
31
+ const specifier = source.substring(node.source.start, node.source.end);
32
+ const magicComment = (0, _magicComments.getMagicComment)({
33
+ match,
34
+ modulePath: filepath,
35
+ importPath: specifier,
36
+ options: magicCommentOptions
37
+ });
38
+ if (magicComment) {
39
+ magicImports.push(src.snip(node.start, node.end).toString().replace(specifier, `${magicComment} ${specifier}`));
40
+ src.appendLeft(node.source.start, `${magicComment} `);
40
41
  }
41
42
  }
42
43
  }
@@ -4,12 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.loader = void 0;
7
+ var _nodePath = require("node:path");
7
8
  var _schemaUtils = require("schema-utils");
8
9
  var _schema = require("./schema.cjs");
9
10
  var _parser = require("./parser.cjs");
10
11
  var _formatter = require("./formatter.cjs");
12
+ var _magicComments = require("magic-comments");
11
13
  var _comment = require("./comment.cjs");
12
- var _util = require("./util.cjs");
13
14
  const loader = function (source) {
14
15
  const options = this.getOptions();
15
16
  const logger = this.getLogger('MCL');
@@ -25,7 +26,7 @@ const loader = function (source) {
25
26
  const magicCommentOptions = Object.keys(rest).length ? rest : {
26
27
  webpackChunkName: true
27
28
  };
28
- const filepath = this.utils.contextify(this.rootContext, this.resourcePath).replace(/^\.\/?/, '');
29
+ const filepath = this.resourcePath;
29
30
  if (mode === 'parser') {
30
31
  const [magicSource, magicImports] = (0, _formatter.format)({
31
32
  ...(0, _parser.parse)(source),
@@ -34,13 +35,14 @@ const loader = function (source) {
34
35
  magicCommentOptions
35
36
  });
36
37
  if (verbose) {
38
+ const relativePath = (0, _nodePath.relative)(this.rootContext, filepath);
37
39
  magicImports.forEach(magicImport => {
38
- logger.info(`${filepath} : ${magicImport}`);
40
+ logger.info(`${relativePath} : ${magicImport}`);
39
41
  });
40
42
  }
41
43
  return magicSource;
42
44
  }
43
- return source.replace(_util.dynamicImportsWithoutComments, (0, _comment.getCommenter)(filepath, {
45
+ return source.replace(_magicComments.dynamicImportsWithoutComments, (0, _comment.getCommenter)(filepath, {
44
46
  verbose,
45
47
  match,
46
48
  magicCommentOptions
@@ -4,36 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.schema = void 0;
7
- var _webpackChunkName = require("./webpackChunkName.cjs");
8
- var _webpackFetchPriority = require("./webpackFetchPriority.cjs");
9
- var _webpackMode = require("./webpackMode.cjs");
10
- var _webpackIgnore = require("./webpackIgnore.cjs");
11
- var _webpackPrefetch = require("./webpackPrefetch.cjs");
12
- var _webpackPreload = require("./webpackPreload.cjs");
13
- var _webpackExports = require("./webpackExports.cjs");
14
- var _webpackInclude = require("./webpackInclude.cjs");
15
- var _webpackExclude = require("./webpackExclude.cjs");
7
+ var _magicComments = require("magic-comments");
16
8
  const schema = {
17
9
  type: 'object',
18
10
  properties: {
19
- verbose: {
20
- type: 'boolean'
21
- },
11
+ ..._magicComments.schema.properties,
22
12
  mode: {
23
13
  enum: ['parser', 'regexp']
24
- },
25
- match: {
26
- enum: ['module', 'import']
27
- },
28
- webpackChunkName: _webpackChunkName.schema,
29
- webpackFetchPriority: _webpackFetchPriority.schema,
30
- webpackMode: _webpackMode.schema,
31
- webpackIgnore: _webpackIgnore.schema,
32
- webpackPrefetch: _webpackPrefetch.schema,
33
- webpackPreload: _webpackPreload.schema,
34
- webpackExports: _webpackExports.schema,
35
- webpackInclude: _webpackInclude.schema,
36
- webpackExclude: _webpackExclude.schema
14
+ }
37
15
  },
38
16
  additionalProperties: false
39
17
  };
package/dist/comment.js CHANGED
@@ -1,15 +1,18 @@
1
- import { commentFor } from './strategy.js';
2
- import { getBareImportSpecifier } from './util.js';
1
+ import { getMagicComment } from 'magic-comments';
3
2
  const getCommenter = (filepath, options, logger) => (rgxMatch, capturedImportPath) => {
4
3
  const importPath = capturedImportPath.trim();
5
- const bareImportPath = getBareImportSpecifier(importPath);
6
4
  const {
7
5
  verbose,
8
6
  match,
9
7
  magicCommentOptions
10
8
  } = options;
11
- const magicComment = Object.keys(magicCommentOptions).map(key => commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
12
- const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `/* ${magicComment.join(', ')} */ ${importPath}` : importPath);
9
+ const magicComment = getMagicComment({
10
+ match,
11
+ importPath,
12
+ modulePath: filepath,
13
+ options: magicCommentOptions
14
+ });
15
+ const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `${magicComment} ${importPath}` : importPath);
13
16
  if (verbose) {
14
17
  logger.info(`${filepath} : ${magicImport}`);
15
18
  }
package/dist/formatter.js CHANGED
@@ -1,6 +1,5 @@
1
+ import { getMagicComment } from 'magic-comments';
1
2
  import MagicString from 'magic-string';
2
- import { commentFor } from './strategy.js';
3
- import { getBareImportSpecifier } from './util.js';
4
3
  const format = ({
5
4
  match,
6
5
  source,
@@ -10,7 +9,6 @@ const format = ({
10
9
  importExpressionNodes
11
10
  }) => {
12
11
  const magicImports = [];
13
- const step = 'import('.length;
14
12
  const cmts = [...comments];
15
13
  const src = new MagicString(source);
16
14
  const hasComment = node => {
@@ -23,13 +21,16 @@ const format = ({
23
21
  };
24
22
  for (const node of importExpressionNodes) {
25
23
  if (!hasComment(node)) {
26
- const specifier = source.substring(node.start + step, node.end - 1);
27
- const bareImportPath = getBareImportSpecifier(specifier);
28
- const magic = Object.keys(magicCommentOptions).map(key => commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
29
- if (magic.length) {
30
- const magicComment = `/* ${magic.join(', ')} */ `;
31
- magicImports.push(src.snip(node.start, node.end).toString().replace(specifier, `${magicComment}${specifier}`));
32
- src.appendRight(node.start + step, `${magicComment}`);
24
+ const specifier = source.substring(node.source.start, node.source.end);
25
+ const magicComment = getMagicComment({
26
+ match,
27
+ modulePath: filepath,
28
+ importPath: specifier,
29
+ options: magicCommentOptions
30
+ });
31
+ if (magicComment) {
32
+ magicImports.push(src.snip(node.start, node.end).toString().replace(specifier, `${magicComment} ${specifier}`));
33
+ src.appendLeft(node.source.start, `${magicComment} `);
33
34
  }
34
35
  }
35
36
  }
package/dist/loader.js CHANGED
@@ -1,9 +1,10 @@
1
+ import { relative } from 'node:path';
1
2
  import { validate } from 'schema-utils';
2
3
  import { schema } from './schema.js';
3
4
  import { parse } from './parser.js';
4
5
  import { format } from './formatter.js';
6
+ import { dynamicImportsWithoutComments } from 'magic-comments';
5
7
  import { getCommenter } from './comment.js';
6
- import { dynamicImportsWithoutComments } from './util.js';
7
8
  const loader = function (source) {
8
9
  const options = this.getOptions();
9
10
  const logger = this.getLogger('MCL');
@@ -19,7 +20,7 @@ const loader = function (source) {
19
20
  const magicCommentOptions = Object.keys(rest).length ? rest : {
20
21
  webpackChunkName: true
21
22
  };
22
- const filepath = this.utils.contextify(this.rootContext, this.resourcePath).replace(/^\.\/?/, '');
23
+ const filepath = this.resourcePath;
23
24
  if (mode === 'parser') {
24
25
  const [magicSource, magicImports] = format({
25
26
  ...parse(source),
@@ -28,8 +29,9 @@ const loader = function (source) {
28
29
  magicCommentOptions
29
30
  });
30
31
  if (verbose) {
32
+ const relativePath = relative(this.rootContext, filepath);
31
33
  magicImports.forEach(magicImport => {
32
- logger.info(`${filepath} : ${magicImport}`);
34
+ logger.info(`${relativePath} : ${magicImport}`);
33
35
  });
34
36
  }
35
37
  return magicSource;