magic-comments-loader 1.4.1 → 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,17 +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
- ### Configuration
24
+ ```
25
+ npm install magic-comments-loader
26
+ ```
27
+
28
+ Next add the loader to your `webpack` config:
29
+
30
+ ```js
31
+ module: {
32
+ rules: [
33
+ {
34
+ test: /\.js$/,
35
+ exclude: /node_modules/,
36
+ use: ['magic-comments-loader']
37
+ }
38
+ ]
39
+ }
40
+ ```
41
+
42
+ Then given a **file.js** with the following import:
43
+
44
+ ```js
45
+ const dynamicModule = await import('./path/to/module.js')
46
+ ```
47
+
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
+ ```
53
+
54
+ ## Examples
25
55
 
26
- Add this inside your `webpack.config.js`.
56
+ Below are some basic usage examples for some of the supported magic comments.
27
57
 
28
- #### Without options
58
+ ### webpackChunkName
29
59
 
30
- 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.
31
61
 
32
62
  ```js
33
63
  module: {
@@ -41,7 +71,74 @@ module: {
41
71
  }
42
72
  ```
43
73
 
44
- #### 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
45
142
 
46
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).
47
144
 
@@ -69,7 +166,7 @@ module: {
69
166
  }
70
167
  ```
71
168
 
72
- #### With `config` options
169
+ ### With `config` options
73
170
 
74
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.
75
172
 
@@ -112,7 +209,7 @@ module: {
112
209
  }
113
210
  ```
114
211
 
115
- #### Overrides
212
+ ### Overrides
116
213
 
117
214
  You can also override the configuration passed in the `config` key by using `overrides`. It is an array of objects that look like:
118
215
 
@@ -196,33 +293,6 @@ module: {
196
293
  }
197
294
  ```
198
295
 
199
- ## Magic Comments
200
-
201
- With loader options configured like
202
-
203
- ```js
204
- {
205
- loader: 'magic-comments-loader',
206
- options: {
207
- webpackChunkName: true,
208
- webpackPrefetch: 'src/some/module.js',
209
- webpackMode: 'eager'
210
- }
211
- }
212
- ```
213
-
214
- an import statement inside `src/some/module.js` like
215
-
216
- ```js
217
- const dynamicModule = await import('./path/to/module.js')
218
- ```
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
296
  ## Options
227
297
 
228
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).
@@ -249,7 +319,7 @@ These are the options that can be configured under the loader `options`. When us
249
319
  * `Function`: `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
250
320
  * `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment.
251
321
  * `webpackIgnore`
252
- * `true`: Adds `webpackIgnore` comments to **all** dynamic imports. **You don't want to do this**.
322
+ * `true`: Adds `webpackIgnore` comments to **all** dynamic imports.
253
323
  * `false`: Disables adding the `webpackIgnore` comment globally. This is the default.
254
324
  * `['/src/**/*.js']`: Adds the comment with a value of `true` when the glob(s) match a path from a `match` path.
255
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.1",
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": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magic-comments-loader",
3
- "version": "1.4.1",
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",