Skip to content

Commit e6a8de3

Browse files
authored
Merge pull request #1 from morganney/develop
Add webpackPreload and webpackPrefetch
2 parents 57c380f + b77f8b3 commit e6a8de3

22 files changed

+418
-120
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Magic comments supported:
88
* `webpackChunkName`
99
* `webpackMode`
1010
* `webpackIgnore`
11-
11+
* `webpackPreload`
12+
* `webpackPrefetch`
1213

1314
## Usage
1415

@@ -48,7 +49,7 @@ module: {
4849
options: {
4950
webpackChunkName: true,
5051
webpackMode: 'lazy',
51-
webpackIgnore: 'src/ignore/**/*.js'
52+
webpackPreload: 'src/preload/**/*.js'
5253
}
5354
}
5455
}
@@ -85,23 +86,24 @@ module: {
8586
}
8687
```
8788

88-
You can also override the configuration passed in the `config` key by using the `overrides` key, which is an array of objects that look like:
89+
#### Overrides
90+
91+
You can also override the configuration passed in the `config` key by using `overrides`, which is an array of objects that look like:
8992

9093
```js
9194
overrides: [
9295
{
93-
// Array of globs or a specific file (can be a string too)
94-
// Uses micromatch to compare the module filepath to the provided string
95-
files: ['src/**/*.js', '!/src/skip/**/*.js']
96-
// Other configuration keys for the comment type can go here too
96+
// Can be an array of strings too
97+
files: 'src/**/*.js',
9798
config: {
98-
active: false
99+
active: false,
100+
// Possibly other configuration values
99101
}
100102
}
101103
]
102104
```
103105

104-
Here's a more complete example using comment `config` and `overrides`:
106+
Here's a more complete example using `config` and `overrides` to customize how comments are applied:
105107

106108
```js
107109
module: {
@@ -189,21 +191,32 @@ const dynamicModule = await import(/* webpackChunkName: "path-to-some-module", w
189191

190192
### Options
191193

192-
These are the options that can be configured under the loader `options`.
194+
These are the options that can be configured under the loader `options`. All comments accept an [`overrides`](#overrides) key in addition to `config` when defined as an object.
193195

194196
* `verbose`: Prints console statements of the updated `import()`s per module filepath during the webpack build. Useful for debugging your custom configurations.
195197
* `webpackChunkName`
196198
* `true`: Adds `webpackChunkName` comments to **all** dynamic imports using the full path to the imported module to construct the name, so `import('path/to/module')` becomes `import(/* webpackChunkName: "path-to-module" */ 'path/to/module')`. This is the default.
197199
* `false`: Disables adding the `webpackChunkName` comment globally.
200+
* `some/glob/**/*.js`|`['/some/globs/**/*.js']`: Adds the comment with the default behavior of slugifying (hyphenating) the import path.
198201
* `config.active`: Boolean to enable/disable the comment.
199202
* `config.basename`: Boolean to use only the basename from the import path as the chunk name. Some relative path imports may end up with the same basename depsite importing different modules. Use in areas where you know the basenames are unique.
200203
* `webpackMode`
201-
* `true`: Adds `webpackMode` comments to **all** dynamic imports using `lazy` so `import('path/to/module')` becomes `import(/* webpackMode: "lazy" */ 'path/to/module')`.
204+
* `true`: Adds `webpackMode` comments to **all** dynamic imports using `lazy`, so `import('path/to/module')` becomes `import(/* webpackMode: "lazy" */ 'path/to/module')`.
202205
* `false`: Disables adding the `webpackChunkName` comment globally. This is the default.
203206
* `config.active`: Boolean to enable/disable the comment.
204207
* `config.mode`: String to set the mode. `lazy`, `lazy-once`, `eager`, or `weak`.
205208
* `webpackIgnore`
206-
* `true`: Adds `webpackIgnore` comments to **all** dynamic imports `true` so `import('path/to/module')` becomes `import(/* webpackIgnore: true */ 'path/to/module')`.
209+
* `true`: Adds `webpackIgnore` comments to **all** dynamic imports, so `import('path/to/module')` becomes `import(/* webpackIgnore: true */ 'path/to/module')`.
207210
* `false`: Disables adding the `webpackIgnore` comment globally. This is the default.
208211
* `some/glob/**/*.js`|`['/some/globs/**/*.js']`: Adds the comment with a value of `true` to all module filepaths that match the string or array of strings.
209212
* `config.active`: Boolean to enable/disable the comment.
213+
* `webpackPreload`
214+
* `true`: Adds `webpackPreload` comments to **all** dynamic imports, so `import('path/to/module')` becomes `import(/* webpackPreload: true */ 'path/to/module')`.
215+
* `false`: Disables adding the `webpackPreload` comment globally. This is the default.
216+
* `some/glob/**/*.js`|`['/some/globs/**/*.js']`: Adds the comment with a value of `true` to all module filepaths that match the string or array of strings.
217+
* `config.active`: Boolean to enable/disable the comment.
218+
* `webpackPrefetch`
219+
* `true`: Adds `webpackPrefetch` comments to **all** dynamic imports, so `import('path/to/module')` becomes `import(/* webpackPrefetch: true */ 'path/to/module')`.
220+
* `false`: Disables adding the `webpackPrefetch` comment globally. This is the default.
221+
* `some/glob/**/*.js`|`['/some/globs/**/*.js']`: Adds the comment with a value of `true` to all module filepaths that match the string or array of strings.
222+
* `config.active`: Boolean to enable/disable the comment.

__tests__/comment.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getCommenter } from '../src/comment.js'
2+
3+
describe('getCommenter', () => {
4+
it('creates a function for adding magic comments to imports', () => {
5+
expect(
6+
getCommenter('some/file/path.js', {
7+
webpackChunkName: true
8+
})('import("./some/test/module.js")', '"./some/test/module.js"')
9+
).toBe('import(/* webpackChunkName: "some-test-module" */ "./some/test/module.js")')
10+
11+
expect(
12+
getCommenter('some/file/path.js', {
13+
webpackChunkName: true,
14+
webpackMode: 'eager',
15+
webpackPrefetch: 'some/**/*.js',
16+
webpackPreload: false
17+
})('import("./some/test/module")', '"./some/test/module"')
18+
).toBe(
19+
'import(/* webpackChunkName: "some-test-module", webpackMode: "eager", webpackPrefetch: true */ "./some/test/module")'
20+
)
21+
22+
expect(
23+
getCommenter('some/file/path.js', {
24+
verbose: true,
25+
webpackPreload: true,
26+
webpackChunkName: true
27+
})('import("./some/test/module")', '"./some/test/module"')
28+
).toBe(
29+
'import(/* webpackPreload: true, webpackChunkName: "some-test-module" */ "./some/test/module")'
30+
)
31+
32+
expect(
33+
getCommenter('some/file/path.js', {
34+
webpackIgnore: true
35+
})('import("./some/test/module")', '"./some/test/module"')
36+
).toBe('import(/* webpackIgnore: true */ "./some/test/module")')
37+
38+
expect(
39+
getCommenter('some/file/path.js', {
40+
webpackChunkName: 'some/**/*.js'
41+
})('import("./some/test/module")', '"./some/test/module"')
42+
).toBe('import(/* webpackChunkName: "some-test-module" */ "./some/test/module")')
43+
})
44+
})

__tests__/strategy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ describe('commentFor', () => {
66
expect.objectContaining({
77
webpackChunkName: expect.any(Function),
88
webpackMode: expect.any(Function),
9-
webpackIgnore: expect.any(Function)
9+
webpackIgnore: expect.any(Function),
10+
webpackPreload: expect.any(Function),
11+
webpackPrefetch: expect.any(Function)
1012
})
1113
)
1214
})

__tests__/util.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { filepathIsMatch, getOverrideConfig } from '../src/util.js'
2+
3+
describe('filepathIsMatch', () => {
4+
it('compares a filepath to glob patterns', () => {
5+
expect(filepathIsMatch('some/file/path.js', 'some/**/*.js')).toEqual(true)
6+
expect(
7+
filepathIsMatch('some/file/path', ['some/**/*.js', '!some/file/*.js'])
8+
).toEqual(false)
9+
expect(
10+
filepathIsMatch('some/file/path.js', ['some/**/*.js', '!some/miss/*.js'])
11+
).toEqual(true)
12+
})
13+
})
14+
15+
describe('getOverrideConfig', () => {
16+
const overrides = [
17+
{
18+
files: 'some/**/*.js',
19+
config: {
20+
test: true
21+
}
22+
}
23+
]
24+
25+
it('gets config overrides based on path globs', () => {
26+
expect(
27+
getOverrideConfig(overrides, 'some/file/path.js', { test: false })
28+
).toStrictEqual({
29+
test: true
30+
})
31+
})
32+
33+
it('returns the passed config if filepath is not a match', () => {
34+
const config = { test: 'it' }
35+
36+
expect(getOverrideConfig(overrides, 'miss/file/path.js', config)).toBe(config)
37+
})
38+
})

__tests__/webpackChunkName.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { webpackChunkName } from '../src/webpackChunkName'
1+
import { webpackChunkName } from '../src/webpackChunkName.js'
22

33
describe('webpackChunkName', () => {
44
it('returns a "webpackChunkName" magic comment', () => {
@@ -8,6 +8,12 @@ describe('webpackChunkName', () => {
88
expect(webpackChunkName(testPath, testImportPath, true)).toEqual(
99
'webpackChunkName: "some-import-path"'
1010
)
11+
expect(webpackChunkName(testPath, testImportPath, 'some/**/*.js')).toEqual(
12+
'webpackChunkName: "some-import-path"'
13+
)
14+
expect(
15+
webpackChunkName(testPath, testImportPath, ['some/**/*.js', '!some/test/*.js'])
16+
).toEqual('')
1117
expect(
1218
webpackChunkName(testPath, testImportPath, { config: { basename: true } })
1319
).toEqual('webpackChunkName: "path"')

__tests__/webpackIgnore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { webpackIgnore } from '../src/webpackIgnore'
1+
import { webpackIgnore } from '../src/webpackIgnore.js'
22

33
describe('webpackIgnore', () => {
44
it('returns a "webpackIgnore" magic comment', () => {

__tests__/webpackMode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { webpackMode } from '../src/webpackMode'
1+
import { webpackMode } from '../src/webpackMode.js'
22

33
describe('webpackMode', () => {
44
it('returns a "webpackMode" magic comment', () => {

__tests__/webpackPrefetch.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { webpackPrefetch } from '../src/webpackPrefetch.js'
2+
3+
describe('webpackPrefetch', () => {
4+
it('returns a "webpackPrefetch" magic comment', () => {
5+
const testPath = 'some/test/module.js'
6+
const testImportPath = './some/import/path'
7+
8+
expect(webpackPrefetch(testPath, testImportPath, true)).toEqual(
9+
'webpackPrefetch: true'
10+
)
11+
expect(webpackPrefetch(testPath, testImportPath, 'some/**/*.js')).toEqual(
12+
'webpackPrefetch: true'
13+
)
14+
expect(
15+
webpackPrefetch(testPath, testImportPath, { config: { active: false } })
16+
).toEqual('')
17+
expect(
18+
webpackPrefetch(testPath, testImportPath, {
19+
config: { active: false },
20+
overrides: [
21+
{
22+
files: 'some/**/*.js',
23+
config: {
24+
active: true
25+
}
26+
}
27+
]
28+
})
29+
).toEqual('webpackPrefetch: true')
30+
})
31+
})

__tests__/webpackPreload.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { webpackPreload } from '../src/webpackPreload.js'
2+
3+
describe('webpackPreload', () => {
4+
it('returns a "webpackPreload" magic comment', () => {
5+
const testPath = 'some/test/module.js'
6+
const testImportPath = './some/import/path'
7+
8+
expect(webpackPreload(testPath, testImportPath, true)).toEqual('webpackPreload: true')
9+
expect(webpackPreload(testPath, testImportPath, 'some/**/*.js')).toEqual(
10+
'webpackPreload: true'
11+
)
12+
expect(
13+
webpackPreload(testPath, testImportPath, { config: { active: false } })
14+
).toEqual('')
15+
expect(
16+
webpackPreload(testPath, testImportPath, {
17+
config: { active: false },
18+
overrides: [
19+
{
20+
files: 'some/**/*.js',
21+
config: {
22+
active: true
23+
}
24+
}
25+
]
26+
})
27+
).toEqual('webpackPreload: true')
28+
})
29+
})

0 commit comments

Comments
 (0)