magic-comments-loader 1.5.0 → 1.6.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 +37 -3
- package/dist/booleanComment.js +90 -0
- package/dist/cjs/booleanComment.cjs +97 -0
- package/dist/cjs/comment.cjs +24 -0
- package/dist/cjs/enumComment.cjs +99 -0
- package/dist/cjs/formatter.cjs +45 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/loader.cjs +49 -0
- package/dist/cjs/parser.cjs +55 -0
- package/dist/cjs/regexComment.cjs +101 -0
- package/dist/cjs/schema.cjs +40 -0
- package/dist/cjs/strategy.cjs +27 -0
- package/dist/cjs/util.cjs +66 -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 +18 -0
- package/dist/enumComment.js +93 -0
- package/dist/formatter.js +38 -0
- package/dist/index.js +1 -0
- package/dist/loader.js +43 -0
- package/dist/parser.js +49 -0
- package/dist/regexComment.js +95 -0
- package/dist/schema.js +34 -0
- package/dist/strategy.js +21 -0
- package/dist/util.js +54 -0
- package/dist/webpackChunkName.js +54 -0
- package/dist/webpackExclude.js +6 -0
- package/dist/webpackExports.js +65 -0
- package/dist/webpackFetchPriority.js +6 -0
- package/dist/webpackIgnore.js +11 -0
- package/dist/webpackInclude.js +6 -0
- package/dist/webpackMode.js +6 -0
- package/dist/webpackPrefetch.js +11 -0
- package/dist/webpackPreload.js +11 -0
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|

|
|
4
4
|
[](https://codecov.io/gh/morganney/magic-comments-loader)
|
|
5
5
|
|
|
6
|
-
Keep your source code clean, add [magic
|
|
6
|
+
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
7
|
|
|
8
8
|
## Getting Started
|
|
9
9
|
|
|
@@ -19,7 +19,7 @@ Next add the loader to your `webpack.config.js` file:
|
|
|
19
19
|
module: {
|
|
20
20
|
rules: [
|
|
21
21
|
{
|
|
22
|
-
test: /\.
|
|
22
|
+
test: /\.jsx?$/,
|
|
23
23
|
use: ['magic-comments-loader']
|
|
24
24
|
}
|
|
25
25
|
]
|
|
@@ -45,6 +45,7 @@ The `webpackChunkName` comment is added by default when registering the loader.
|
|
|
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
46
|
|
|
47
47
|
* [`verbose`](#verbose)
|
|
48
|
+
* [`mode`](#mode)
|
|
48
49
|
* [`match`](#match)
|
|
49
50
|
* [`webpackChunkName`](#webpackchunkname)
|
|
50
51
|
* [`webpackFetchPriority`](#webpackfetchpriority)
|
|
@@ -95,6 +96,15 @@ boolean
|
|
|
95
96
|
|
|
96
97
|
Prints console statements of the module filepath and updated `import()` during the webpack build. Useful for debugging.
|
|
97
98
|
|
|
99
|
+
### `mode`
|
|
100
|
+
**type**
|
|
101
|
+
```ts
|
|
102
|
+
'parser' | 'regexp'
|
|
103
|
+
```
|
|
104
|
+
**default** `'parser'`
|
|
105
|
+
|
|
106
|
+
Sets how the loader finds dynamic import expressions in your source code, either using an [ECMAScript parser](https://github.com/acornjs/acorn), or a regular expression. Your mileage may vary when using `'regexp'`.
|
|
107
|
+
|
|
98
108
|
### `match`
|
|
99
109
|
**type**
|
|
100
110
|
```ts
|
|
@@ -525,7 +535,7 @@ When using a [`CommentConfig`](#commentconfig) object, you can override the conf
|
|
|
525
535
|
}
|
|
526
536
|
```
|
|
527
537
|
|
|
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).
|
|
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).
|
|
529
539
|
|
|
530
540
|
Here's a more complete example of how overrides can be applied:
|
|
531
541
|
|
|
@@ -581,3 +591,27 @@ import(/* webpackMode: "lazy" */ './folder/module.js')
|
|
|
581
591
|
import(/* webpackMode: "eager" */ './eager/module.js')
|
|
582
592
|
import(/* webpackChunkName: "locales-[request]", webpackMode: "lazy-once" */ `./locales/${lang}.json`)
|
|
583
593
|
```
|
|
594
|
+
|
|
595
|
+
### TypeScript
|
|
596
|
+
|
|
597
|
+
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.
|
|
598
|
+
|
|
599
|
+
For example, if your project source code is written in TypeScript, and you use `babel-loader` to transpile and remove type annotations via `@babel/preset-typescript`, while `tsc` is used for type-checking only, chain loaders like this:
|
|
600
|
+
|
|
601
|
+
**config**
|
|
602
|
+
```js
|
|
603
|
+
module: {
|
|
604
|
+
rules: [
|
|
605
|
+
{
|
|
606
|
+
test: /\.[jt]sx?$/,
|
|
607
|
+
// Webpack loader chains are processed in reverse order, i.e. last comes first.
|
|
608
|
+
use: [
|
|
609
|
+
'magic-comments-loader',
|
|
610
|
+
'babel-loader'
|
|
611
|
+
]
|
|
612
|
+
}
|
|
613
|
+
]
|
|
614
|
+
}
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
You would configure `ts-loader` similarly, or any other loader that transpiles your source code into spec compliant ECMAScript.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { getOverrideSchema, pathIsMatch, importPrefix } from './util.js';
|
|
2
|
+
const defaultSchema = {
|
|
3
|
+
type: 'object',
|
|
4
|
+
properties: {
|
|
5
|
+
active: {
|
|
6
|
+
oneOf: [{
|
|
7
|
+
type: 'boolean'
|
|
8
|
+
}, {
|
|
9
|
+
instanceof: 'Function'
|
|
10
|
+
}]
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
additionalProperties: false
|
|
14
|
+
};
|
|
15
|
+
const getSchema = (commentSchema = defaultSchema) => ({
|
|
16
|
+
oneOf: [{
|
|
17
|
+
type: 'boolean'
|
|
18
|
+
}, {
|
|
19
|
+
type: 'string'
|
|
20
|
+
}, {
|
|
21
|
+
type: 'array',
|
|
22
|
+
items: {
|
|
23
|
+
type: 'string'
|
|
24
|
+
}
|
|
25
|
+
}, {
|
|
26
|
+
instanceof: 'Function'
|
|
27
|
+
}, {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
config: commentSchema,
|
|
31
|
+
overrides: getOverrideSchema(commentSchema)
|
|
32
|
+
},
|
|
33
|
+
required: ['config'],
|
|
34
|
+
additionalProperties: false
|
|
35
|
+
}]
|
|
36
|
+
});
|
|
37
|
+
const getConfig = (value, match, filepath, importPath, defaultConfig = {
|
|
38
|
+
active: true
|
|
39
|
+
}) => {
|
|
40
|
+
const path = match === 'import' ? importPath.replace(importPrefix, '') : filepath;
|
|
41
|
+
if (value === true) {
|
|
42
|
+
return defaultConfig;
|
|
43
|
+
}
|
|
44
|
+
if (value === false) {
|
|
45
|
+
return {
|
|
46
|
+
...defaultConfig,
|
|
47
|
+
active: false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (Array.isArray(value) || typeof value === 'string') {
|
|
51
|
+
return {
|
|
52
|
+
...defaultConfig,
|
|
53
|
+
active: pathIsMatch(path, value)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === 'function') {
|
|
57
|
+
const configValue = value(filepath, importPath);
|
|
58
|
+
if (configValue) {
|
|
59
|
+
return {
|
|
60
|
+
...defaultConfig,
|
|
61
|
+
active: true,
|
|
62
|
+
dynamic: configValue
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
...defaultConfig,
|
|
67
|
+
active: false
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
let config = {
|
|
71
|
+
...defaultConfig,
|
|
72
|
+
...value.config
|
|
73
|
+
};
|
|
74
|
+
if (Array.isArray(value.overrides)) {
|
|
75
|
+
const {
|
|
76
|
+
overrides
|
|
77
|
+
} = value;
|
|
78
|
+
const length = overrides.length;
|
|
79
|
+
for (let i = 0; i < length; i++) {
|
|
80
|
+
if (pathIsMatch(path, overrides[i].files)) {
|
|
81
|
+
return {
|
|
82
|
+
...config,
|
|
83
|
+
...overrides[i].config
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return config;
|
|
89
|
+
};
|
|
90
|
+
export { getSchema, getConfig };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getSchema = exports.getConfig = void 0;
|
|
7
|
+
var _util = require("./util.cjs");
|
|
8
|
+
const defaultSchema = {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
active: {
|
|
12
|
+
oneOf: [{
|
|
13
|
+
type: 'boolean'
|
|
14
|
+
}, {
|
|
15
|
+
instanceof: 'Function'
|
|
16
|
+
}]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
additionalProperties: false
|
|
20
|
+
};
|
|
21
|
+
const getSchema = (commentSchema = defaultSchema) => ({
|
|
22
|
+
oneOf: [{
|
|
23
|
+
type: 'boolean'
|
|
24
|
+
}, {
|
|
25
|
+
type: 'string'
|
|
26
|
+
}, {
|
|
27
|
+
type: 'array',
|
|
28
|
+
items: {
|
|
29
|
+
type: 'string'
|
|
30
|
+
}
|
|
31
|
+
}, {
|
|
32
|
+
instanceof: 'Function'
|
|
33
|
+
}, {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
config: commentSchema,
|
|
37
|
+
overrides: (0, _util.getOverrideSchema)(commentSchema)
|
|
38
|
+
},
|
|
39
|
+
required: ['config'],
|
|
40
|
+
additionalProperties: false
|
|
41
|
+
}]
|
|
42
|
+
});
|
|
43
|
+
exports.getSchema = getSchema;
|
|
44
|
+
const getConfig = (value, match, filepath, importPath, defaultConfig = {
|
|
45
|
+
active: true
|
|
46
|
+
}) => {
|
|
47
|
+
const path = match === 'import' ? importPath.replace(_util.importPrefix, '') : filepath;
|
|
48
|
+
if (value === true) {
|
|
49
|
+
return defaultConfig;
|
|
50
|
+
}
|
|
51
|
+
if (value === false) {
|
|
52
|
+
return {
|
|
53
|
+
...defaultConfig,
|
|
54
|
+
active: false
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(value) || typeof value === 'string') {
|
|
58
|
+
return {
|
|
59
|
+
...defaultConfig,
|
|
60
|
+
active: (0, _util.pathIsMatch)(path, value)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === 'function') {
|
|
64
|
+
const configValue = value(filepath, importPath);
|
|
65
|
+
if (configValue) {
|
|
66
|
+
return {
|
|
67
|
+
...defaultConfig,
|
|
68
|
+
active: true,
|
|
69
|
+
dynamic: configValue
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
...defaultConfig,
|
|
74
|
+
active: false
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
let config = {
|
|
78
|
+
...defaultConfig,
|
|
79
|
+
...value.config
|
|
80
|
+
};
|
|
81
|
+
if (Array.isArray(value.overrides)) {
|
|
82
|
+
const {
|
|
83
|
+
overrides
|
|
84
|
+
} = value;
|
|
85
|
+
const length = overrides.length;
|
|
86
|
+
for (let i = 0; i < length; i++) {
|
|
87
|
+
if ((0, _util.pathIsMatch)(path, overrides[i].files)) {
|
|
88
|
+
return {
|
|
89
|
+
...config,
|
|
90
|
+
...overrides[i].config
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return config;
|
|
96
|
+
};
|
|
97
|
+
exports.getConfig = getConfig;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getCommenter = void 0;
|
|
7
|
+
var _strategy = require("./strategy.cjs");
|
|
8
|
+
var _util = require("./util.cjs");
|
|
9
|
+
const getCommenter = (filepath, options, logger) => (rgxMatch, capturedImportPath) => {
|
|
10
|
+
const importPath = capturedImportPath.trim();
|
|
11
|
+
const bareImportPath = (0, _util.getBareImportSpecifier)(importPath);
|
|
12
|
+
const {
|
|
13
|
+
verbose,
|
|
14
|
+
match,
|
|
15
|
+
magicCommentOptions
|
|
16
|
+
} = 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);
|
|
19
|
+
if (verbose) {
|
|
20
|
+
logger.info(`${filepath} : ${magicImport}`);
|
|
21
|
+
}
|
|
22
|
+
return magicImport;
|
|
23
|
+
};
|
|
24
|
+
exports.getCommenter = getCommenter;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getEnumComment = void 0;
|
|
7
|
+
var _util = require("./util.cjs");
|
|
8
|
+
const getEnumComment = (name, enums, defaultValue) => {
|
|
9
|
+
const configSchema = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
active: {
|
|
13
|
+
oneOf: [{
|
|
14
|
+
type: 'boolean'
|
|
15
|
+
}, {
|
|
16
|
+
instanceof: 'Function'
|
|
17
|
+
}]
|
|
18
|
+
},
|
|
19
|
+
[name]: {
|
|
20
|
+
oneOf: [{
|
|
21
|
+
enum: enums
|
|
22
|
+
}, {
|
|
23
|
+
instanceof: 'Function'
|
|
24
|
+
}]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
additionalProperties: false
|
|
28
|
+
};
|
|
29
|
+
const schema = {
|
|
30
|
+
oneOf: [{
|
|
31
|
+
type: 'boolean'
|
|
32
|
+
}, {
|
|
33
|
+
type: 'string'
|
|
34
|
+
}, {
|
|
35
|
+
instanceof: 'Function'
|
|
36
|
+
}, {
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
config: configSchema,
|
|
40
|
+
overrides: (0, _util.getOverrideSchema)(configSchema)
|
|
41
|
+
},
|
|
42
|
+
required: ['config'],
|
|
43
|
+
additionalProperties: false
|
|
44
|
+
}]
|
|
45
|
+
};
|
|
46
|
+
const defaultConfig = {
|
|
47
|
+
active: true,
|
|
48
|
+
[name]: defaultValue
|
|
49
|
+
};
|
|
50
|
+
const getConfig = (value, filepath) => {
|
|
51
|
+
if (value === true) {
|
|
52
|
+
return defaultConfig;
|
|
53
|
+
}
|
|
54
|
+
if (value === false) {
|
|
55
|
+
return {
|
|
56
|
+
...defaultConfig,
|
|
57
|
+
active: false
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === 'string' || typeof value === 'function') {
|
|
61
|
+
return {
|
|
62
|
+
...defaultConfig,
|
|
63
|
+
[name]: value
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
let config = {
|
|
67
|
+
...defaultConfig,
|
|
68
|
+
...value.config
|
|
69
|
+
};
|
|
70
|
+
if (Array.isArray(value.overrides)) {
|
|
71
|
+
config = (0, _util.getOverrideConfig)(value.overrides, filepath, config);
|
|
72
|
+
}
|
|
73
|
+
return config;
|
|
74
|
+
};
|
|
75
|
+
const comment = (filepath, importPath, value) => {
|
|
76
|
+
let enumValue = '';
|
|
77
|
+
const commentSuffix = `${name[0].toUpperCase()}${name.slice(1)}`;
|
|
78
|
+
const config = getConfig(value, filepath);
|
|
79
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
80
|
+
if (!isActive) {
|
|
81
|
+
return '';
|
|
82
|
+
}
|
|
83
|
+
if (typeof config[name] === 'function') {
|
|
84
|
+
enumValue = config[name](filepath, importPath);
|
|
85
|
+
}
|
|
86
|
+
if (typeof config[name] === 'string') {
|
|
87
|
+
enumValue = config[name];
|
|
88
|
+
}
|
|
89
|
+
if (!enums.includes(enumValue)) {
|
|
90
|
+
return '';
|
|
91
|
+
}
|
|
92
|
+
return `webpack${commentSuffix}: "${enumValue}"`;
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
comment,
|
|
96
|
+
schema
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
exports.getEnumComment = getEnumComment;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.format = void 0;
|
|
7
|
+
var _magicString = _interopRequireDefault(require("magic-string"));
|
|
8
|
+
var _strategy = require("./strategy.cjs");
|
|
9
|
+
var _util = require("./util.cjs");
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
const format = ({
|
|
12
|
+
match,
|
|
13
|
+
source,
|
|
14
|
+
filepath,
|
|
15
|
+
comments,
|
|
16
|
+
magicCommentOptions,
|
|
17
|
+
importExpressionNodes
|
|
18
|
+
}) => {
|
|
19
|
+
const magicImports = [];
|
|
20
|
+
const step = 'import('.length;
|
|
21
|
+
const cmts = [...comments];
|
|
22
|
+
const src = new _magicString.default(source);
|
|
23
|
+
const hasComment = node => {
|
|
24
|
+
const idx = cmts.findIndex(cmt => cmt.start > node.start && cmt.end < node.end);
|
|
25
|
+
const wasFound = idx > -1;
|
|
26
|
+
if (wasFound) {
|
|
27
|
+
cmts.splice(idx, 1);
|
|
28
|
+
}
|
|
29
|
+
return wasFound;
|
|
30
|
+
};
|
|
31
|
+
for (const node of importExpressionNodes) {
|
|
32
|
+
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}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [src.toString(), magicImports];
|
|
44
|
+
};
|
|
45
|
+
exports.format = format;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.loader = void 0;
|
|
7
|
+
var _schemaUtils = require("schema-utils");
|
|
8
|
+
var _schema = require("./schema.cjs");
|
|
9
|
+
var _parser = require("./parser.cjs");
|
|
10
|
+
var _formatter = require("./formatter.cjs");
|
|
11
|
+
var _comment = require("./comment.cjs");
|
|
12
|
+
var _util = require("./util.cjs");
|
|
13
|
+
const loader = function (source) {
|
|
14
|
+
const options = this.getOptions();
|
|
15
|
+
const logger = this.getLogger('MCL');
|
|
16
|
+
(0, _schemaUtils.validate)(_schema.schema, options, {
|
|
17
|
+
name: 'magic-comments-loader'
|
|
18
|
+
});
|
|
19
|
+
const {
|
|
20
|
+
mode = 'parser',
|
|
21
|
+
match = 'module',
|
|
22
|
+
verbose = false,
|
|
23
|
+
...rest
|
|
24
|
+
} = options;
|
|
25
|
+
const magicCommentOptions = Object.keys(rest).length ? rest : {
|
|
26
|
+
webpackChunkName: true
|
|
27
|
+
};
|
|
28
|
+
const filepath = this.utils.contextify(this.rootContext, this.resourcePath).replace(/^\.\/?/, '');
|
|
29
|
+
if (mode === 'parser') {
|
|
30
|
+
const [magicSource, magicImports] = (0, _formatter.format)({
|
|
31
|
+
...(0, _parser.parse)(source),
|
|
32
|
+
match,
|
|
33
|
+
filepath,
|
|
34
|
+
magicCommentOptions
|
|
35
|
+
});
|
|
36
|
+
if (verbose) {
|
|
37
|
+
magicImports.forEach(magicImport => {
|
|
38
|
+
logger.info(`${filepath} : ${magicImport}`);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return magicSource;
|
|
42
|
+
}
|
|
43
|
+
return source.replace(_util.dynamicImportsWithoutComments, (0, _comment.getCommenter)(filepath, {
|
|
44
|
+
verbose,
|
|
45
|
+
match,
|
|
46
|
+
magicCommentOptions
|
|
47
|
+
}, logger));
|
|
48
|
+
};
|
|
49
|
+
exports.loader = loader;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parse = void 0;
|
|
7
|
+
var _acorn = require("acorn");
|
|
8
|
+
var _acornWalk = require("acorn-walk");
|
|
9
|
+
var _acornJsxWalk = require("acorn-jsx-walk");
|
|
10
|
+
var _acornJsx = _interopRequireDefault(require("acorn-jsx"));
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/**
|
|
13
|
+
* NOTE: Side-effect of importing this module's exports.
|
|
14
|
+
*
|
|
15
|
+
* Extend acorn-walk's base with missing JSX nodes.
|
|
16
|
+
* @see https://github.com/acornjs/acorn/issues/829
|
|
17
|
+
*
|
|
18
|
+
* Consider another parser that supports more syntaxes out-of-the-box.
|
|
19
|
+
* That would enable less requirements on loader chaining.
|
|
20
|
+
*/
|
|
21
|
+
(0, _acornJsxWalk.extend)(_acornWalk.base);
|
|
22
|
+
const jsxParser = _acorn.Parser.extend((0, _acornJsx.default)());
|
|
23
|
+
const parse = source => {
|
|
24
|
+
const comments = [];
|
|
25
|
+
const importExpressionNodes = [];
|
|
26
|
+
const ast = jsxParser.parse(source, {
|
|
27
|
+
locations: false,
|
|
28
|
+
ecmaVersion: 2023,
|
|
29
|
+
sourceType: 'module',
|
|
30
|
+
allowAwaitOutsideFunction: true,
|
|
31
|
+
allowReturnOutsideFunction: true,
|
|
32
|
+
allowImportExportEverywhere: true,
|
|
33
|
+
onComment: (isBlock, commentText, start, end) => {
|
|
34
|
+
if (isBlock) {
|
|
35
|
+
comments.push({
|
|
36
|
+
start,
|
|
37
|
+
end,
|
|
38
|
+
commentText
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
(0, _acornWalk.simple)(ast, {
|
|
44
|
+
ImportExpression(node) {
|
|
45
|
+
importExpressionNodes.push(node);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
ast,
|
|
50
|
+
comments,
|
|
51
|
+
importExpressionNodes,
|
|
52
|
+
source
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.parse = parse;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getRegexComment = void 0;
|
|
7
|
+
var _util = require("./util.cjs");
|
|
8
|
+
const getRegexComment = type => {
|
|
9
|
+
const configSchema = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
active: {
|
|
13
|
+
oneOf: [{
|
|
14
|
+
type: 'boolean'
|
|
15
|
+
}, {
|
|
16
|
+
instanceof: 'Function'
|
|
17
|
+
}]
|
|
18
|
+
},
|
|
19
|
+
include: {
|
|
20
|
+
oneOf: [{
|
|
21
|
+
instanceof: 'Function'
|
|
22
|
+
}, {
|
|
23
|
+
instanceof: 'RegExp'
|
|
24
|
+
}]
|
|
25
|
+
},
|
|
26
|
+
exclude: {
|
|
27
|
+
oneOf: [{
|
|
28
|
+
instanceof: 'Function'
|
|
29
|
+
}, {
|
|
30
|
+
instanceof: 'RegExp'
|
|
31
|
+
}]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: false
|
|
35
|
+
};
|
|
36
|
+
const schema = {
|
|
37
|
+
oneOf: [{
|
|
38
|
+
instanceof: 'Function'
|
|
39
|
+
}, {
|
|
40
|
+
instanceof: 'RegExp'
|
|
41
|
+
}, {
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
config: configSchema,
|
|
45
|
+
overrides: (0, _util.getOverrideSchema)(configSchema)
|
|
46
|
+
},
|
|
47
|
+
required: ['config'],
|
|
48
|
+
additionalProperties: false
|
|
49
|
+
}]
|
|
50
|
+
};
|
|
51
|
+
const getConfig = (value, filepath) => {
|
|
52
|
+
if (typeof value === 'function' || value instanceof RegExp) {
|
|
53
|
+
return {
|
|
54
|
+
active: true,
|
|
55
|
+
[type]: value
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
let config = {
|
|
59
|
+
active: true,
|
|
60
|
+
...value.config
|
|
61
|
+
};
|
|
62
|
+
if (Array.isArray(value.overrides)) {
|
|
63
|
+
config = (0, _util.getOverrideConfig)(value.overrides, filepath, config);
|
|
64
|
+
}
|
|
65
|
+
return config;
|
|
66
|
+
};
|
|
67
|
+
const comment = (filepath, importPath, value) => {
|
|
68
|
+
const config = getConfig(value, filepath);
|
|
69
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
70
|
+
let regex = null;
|
|
71
|
+
if (!isActive) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
if (typeof config[type] === 'function') {
|
|
75
|
+
regex = config[type](filepath, importPath);
|
|
76
|
+
}
|
|
77
|
+
if (config[type] instanceof RegExp) {
|
|
78
|
+
regex = config[type];
|
|
79
|
+
}
|
|
80
|
+
if (!(regex instanceof RegExp)) {
|
|
81
|
+
return '';
|
|
82
|
+
}
|
|
83
|
+
const source = regex.source;
|
|
84
|
+
const typeName = `${type[0].toUpperCase()}${type.slice(1)}`;
|
|
85
|
+
/**
|
|
86
|
+
* Check if the provided RegExp ends in one or more '*'
|
|
87
|
+
* and if so be sure to escape the ending '/' in the
|
|
88
|
+
* comments regular expression so as not to break the
|
|
89
|
+
* comment and cause a SyntaxError.
|
|
90
|
+
*/
|
|
91
|
+
if (/(\*+)$/.test(source)) {
|
|
92
|
+
return `webpack${typeName}: /${source}\\/${regex.flags}`;
|
|
93
|
+
}
|
|
94
|
+
return `webpack${typeName}: /${source}/${regex.flags}`;
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
comment,
|
|
98
|
+
schema
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
exports.getRegexComment = getRegexComment;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
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");
|
|
16
|
+
const schema = {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
verbose: {
|
|
20
|
+
type: 'boolean'
|
|
21
|
+
},
|
|
22
|
+
mode: {
|
|
23
|
+
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
|
|
37
|
+
},
|
|
38
|
+
additionalProperties: false
|
|
39
|
+
};
|
|
40
|
+
exports.schema = schema;
|