magic-comments-loader 1.4.0 → 1.4.2

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
@@ -17,35 +17,47 @@ All magic comments are supported:
17
17
  * `webpackExclude`
18
18
  * `webpackExports`
19
19
 
20
- ## Usage
20
+ ## Getting Started
21
21
 
22
- First `npm install magic-comments-loader`.
22
+ First install `magic-comments-loader`:
23
23
 
24
- Try *not* to have dynamic `import()` statements behind [comments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#comments). **It is better to remove unused code in production**.
25
- If you must, e.g. your comment is referencing usage of dynamic imports, then these styles are ok:
24
+ ```
25
+ npm install magic-comments-loader
26
+ ```
27
+
28
+ Next add the loader to your `webpack` config:
26
29
 
27
30
  ```js
28
- // import('some-ignored-module')
31
+ module: {
32
+ rules: [
33
+ {
34
+ test: /\.js$/,
35
+ exclude: /node_modules/,
36
+ use: ['magic-comments-loader']
37
+ }
38
+ ]
39
+ }
40
+ ```
29
41
 
30
- /* Some comment about a dynamic import('module') */
42
+ Then given a **file.js** with the following import:
31
43
 
32
- /**
33
- * Some multiline comment, only use one import() per line.
34
- * Comment about import('module/one')
35
- * Comment about import('module/two')
36
- * import('module/three'), etc.
37
- */
44
+ ```js
45
+ const dynamicModule = await import('./path/to/module.js')
38
46
  ```
39
47
 
40
- This module uses a RegExp not a parser. If you would like to add better support for ignoring `import()` behind multiline comments please open a pull request.
48
+ While running `webpack` the import inside **file.js** becomes:
49
+
50
+ ```js
51
+ const dynamicModule = await import(/* webpackChunkName: "path-to-module" */ './path/to/module.js')
52
+ ```
41
53
 
42
- ### Configuration
54
+ ## Examples
43
55
 
44
- Add this inside your `webpack.config.js`.
56
+ Below are some basic usage examples for some of the supported magic comments.
45
57
 
46
- #### Without options
58
+ ### webpackChunkName
47
59
 
48
- Adds `/* webpackChunkName: "path-to-module" */` to all dynamic imports (same as `webpackChunkName: true` when using options).
60
+ Add `webpackChunkName` magic comments to **all** of your dynamic imports using the hyphenated path as the chunk name.
49
61
 
50
62
  ```js
51
63
  module: {
@@ -59,7 +71,74 @@ module: {
59
71
  }
60
72
  ```
61
73
 
62
- #### With options
74
+ Or using the configuration options you can change the chunk name to the module's filename (instead of the full hyphenated path).
75
+
76
+ ```js
77
+ module: {
78
+ rules: [
79
+ {
80
+ test: /\.js$/,
81
+ exclude: /node_modules/,
82
+ use: {
83
+ loader: 'magic-comments-loader',
84
+ options: {
85
+ webpackChunkName: {
86
+ config: {
87
+ basename: true
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ ]
94
+ }
95
+ ```
96
+
97
+ ### webpackIgnore
98
+
99
+ Have webpack ignore **all** dynamic imports and use the native `import()`, for instance if you wanted to opt out of code-splitting or use native ES Modules.
100
+
101
+ ```js
102
+ module: {
103
+ rules: [
104
+ {
105
+ test: /\.js$/,
106
+ exclude: /node_modules/,
107
+ use: {
108
+ loader: 'magic-comments-loader',
109
+ options: {
110
+ webpackIgnore: true
111
+ }
112
+ }
113
+ }
114
+ ]
115
+ }
116
+ ```
117
+
118
+ Or only for modules in a specific directory.
119
+
120
+ ```js
121
+ module: {
122
+ rules: [
123
+ {
124
+ test: /\.js$/,
125
+ exclude: /node_modules/,
126
+ use: {
127
+ loader: 'magic-comments-loader',
128
+ options: {
129
+ webpackIgnore: 'src/esm/**/*.js'
130
+ }
131
+ }
132
+ }
133
+ ]
134
+ }
135
+ ```
136
+
137
+ ## Configuration
138
+
139
+ The loader supports configuration with options and overrides for each magic comment.
140
+
141
+ ### With options
63
142
 
64
143
  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).
65
144
 
@@ -87,7 +166,7 @@ module: {
87
166
  }
88
167
  ```
89
168
 
90
- #### With `config` options
169
+ ### With `config` options
91
170
 
92
171
  For more control, all comments support a configuration object with two supported keys, `config` and `overrides`. The `config` key is an object used to specifiy [comment options](#options). The [`overrides`](#overrides) key is defined below.
93
172
 
@@ -130,7 +209,7 @@ module: {
130
209
  }
131
210
  ```
132
211
 
133
- #### Overrides
212
+ ### Overrides
134
213
 
135
214
  You can also override the configuration passed in the `config` key by using `overrides`. It is an array of objects that look like:
136
215
 
@@ -214,33 +293,6 @@ module: {
214
293
  }
215
294
  ```
216
295
 
217
- ## Magic Comments
218
-
219
- With loader options configured like
220
-
221
- ```js
222
- {
223
- loader: 'magic-comments-loader',
224
- options: {
225
- webpackChunkName: true,
226
- webpackPrefetch: 'src/some/module.js',
227
- webpackMode: 'eager'
228
- }
229
- }
230
- ```
231
-
232
- an import statement inside `src/some/module.js` like
233
-
234
- ```js
235
- const dynamicModule = await import('./path/to/module.js')
236
- ```
237
-
238
- becomes
239
-
240
- ```js
241
- const dynamicModule = await import(/* webpackChunkName: "path-to-module", webpackPrefetch: true, webpackMode: "eager" */ './path/to/module.js')
242
- ```
243
-
244
296
  ## Options
245
297
 
246
298
  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).
@@ -267,7 +319,7 @@ These are the options that can be configured under the loader `options`. When us
267
319
  * `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
268
320
  * `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
269
321
  * `webpackIgnore`
270
- * `true`: Adds `webpackIgnore` comments to **all** dynamic imports. **You don't want to do this**.
322
+ * `true`: Adds `webpackIgnore` comments to **all** dynamic imports.
271
323
  * `false`: Disables adding the `webpackIgnore` comment globally. This is the default.
272
324
  * `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
273
325
  * `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magic-comments-loader",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Add webpack magic comments to your dynamic imports during build time",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -42,7 +42,7 @@
42
42
  "eslint-config-prettier": "^8.3.0",
43
43
  "eslint-plugin-jest": "^24.4.0",
44
44
  "eslint-plugin-prettier": "^4.0.0",
45
- "jest": "^27.1.0",
45
+ "jest": "^27.1.1",
46
46
  "memfs": "^3.2.4",
47
47
  "prettier": "^2.3.2",
48
48
  "webpack": "^5.52.0"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magic-comments-loader",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Add webpack magic comments to your dynamic imports during build time",
5
5
  "main": "dist",
6
6
  "type": "module",
@@ -43,7 +43,7 @@
43
43
  "eslint-config-prettier": "^8.3.0",
44
44
  "eslint-plugin-jest": "^24.4.0",
45
45
  "eslint-plugin-prettier": "^4.0.0",
46
- "jest": "^27.1.0",
46
+ "jest": "^27.1.1",
47
47
  "memfs": "^3.2.4",
48
48
  "prettier": "^2.3.2",
49
49
  "webpack": "^5.52.0"