magic-comments-loader 1.5.0 → 1.5.1
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/dist/booleanComment.js +90 -0
- package/dist/cjs/booleanComment.cjs +97 -0
- package/dist/cjs/comment.cjs +23 -0
- package/dist/cjs/enumComment.cjs +99 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/loader.cjs +25 -0
- package/dist/cjs/regexComment.cjs +101 -0
- package/dist/cjs/schema.cjs +37 -0
- package/dist/cjs/strategy.cjs +27 -0
- package/dist/cjs/util.cjs +62 -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 +17 -0
- package/dist/enumComment.js +93 -0
- package/dist/index.js +1 -0
- package/dist/loader.js +19 -0
- package/dist/regexComment.js +95 -0
- package/dist/schema.js +31 -0
- package/dist/strategy.js +21 -0
- package/dist/util.js +51 -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 +1 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackExports = exports.schema = void 0;
|
|
7
|
+
var _util = require("./util.cjs");
|
|
8
|
+
const configSchema = {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
active: {
|
|
12
|
+
oneOf: [{
|
|
13
|
+
type: 'boolean'
|
|
14
|
+
}, {
|
|
15
|
+
instanceof: 'Function'
|
|
16
|
+
}]
|
|
17
|
+
},
|
|
18
|
+
exports: {
|
|
19
|
+
instanceof: 'Function'
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
additionalProperties: false
|
|
23
|
+
};
|
|
24
|
+
const schema = {
|
|
25
|
+
oneOf: [{
|
|
26
|
+
instanceof: 'Function'
|
|
27
|
+
}, {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
config: configSchema,
|
|
31
|
+
overrides: (0, _util.getOverrideSchema)(configSchema)
|
|
32
|
+
},
|
|
33
|
+
required: ['config'],
|
|
34
|
+
additionalProperties: false
|
|
35
|
+
}]
|
|
36
|
+
};
|
|
37
|
+
exports.schema = schema;
|
|
38
|
+
const defaultConfig = {
|
|
39
|
+
active: true
|
|
40
|
+
};
|
|
41
|
+
const getConfig = (value, filepath) => {
|
|
42
|
+
if (typeof value === 'function') {
|
|
43
|
+
return {
|
|
44
|
+
...defaultConfig,
|
|
45
|
+
exports: value
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
let config = {
|
|
49
|
+
...defaultConfig,
|
|
50
|
+
...value.config
|
|
51
|
+
};
|
|
52
|
+
if (Array.isArray(value.overrides)) {
|
|
53
|
+
config = (0, _util.getOverrideConfig)(value.overrides, filepath, config);
|
|
54
|
+
}
|
|
55
|
+
return config;
|
|
56
|
+
};
|
|
57
|
+
const webpackExports = (filepath, importPath, value) => {
|
|
58
|
+
let exports = '';
|
|
59
|
+
let configExports = [];
|
|
60
|
+
const config = getConfig(value, filepath);
|
|
61
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
62
|
+
if (!isActive || typeof config.exports !== 'function') {
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
configExports = config.exports(filepath, importPath);
|
|
66
|
+
if (!Array.isArray(configExports)) {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
exports = `["${configExports.reduce((curr, next) => `${curr}", "${next}`)}"]`;
|
|
70
|
+
return `webpackExports: ${exports}`;
|
|
71
|
+
};
|
|
72
|
+
exports.webpackExports = webpackExports;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackFetchPriority = exports.schema = void 0;
|
|
7
|
+
var _enumComment = require("./enumComment.cjs");
|
|
8
|
+
const {
|
|
9
|
+
comment: webpackFetchPriority,
|
|
10
|
+
schema
|
|
11
|
+
} = (0, _enumComment.getEnumComment)('fetchPriority', ['high', 'low', 'auto'], 'auto');
|
|
12
|
+
exports.schema = schema;
|
|
13
|
+
exports.webpackFetchPriority = webpackFetchPriority;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackIgnore = exports.schema = void 0;
|
|
7
|
+
var _booleanComment = require("./booleanComment.cjs");
|
|
8
|
+
const schema = (0, _booleanComment.getSchema)();
|
|
9
|
+
exports.schema = schema;
|
|
10
|
+
const webpackIgnore = (filepath, importPath, value, match) => {
|
|
11
|
+
const config = (0, _booleanComment.getConfig)(value, match, filepath, importPath);
|
|
12
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
13
|
+
if (!isActive) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
return 'webpackIgnore: true';
|
|
17
|
+
};
|
|
18
|
+
exports.webpackIgnore = webpackIgnore;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackInclude = exports.schema = void 0;
|
|
7
|
+
var _regexComment = require("./regexComment.cjs");
|
|
8
|
+
const {
|
|
9
|
+
comment: webpackInclude,
|
|
10
|
+
schema
|
|
11
|
+
} = (0, _regexComment.getRegexComment)('include');
|
|
12
|
+
exports.schema = schema;
|
|
13
|
+
exports.webpackInclude = webpackInclude;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackMode = exports.schema = void 0;
|
|
7
|
+
var _enumComment = require("./enumComment.cjs");
|
|
8
|
+
const {
|
|
9
|
+
comment: webpackMode,
|
|
10
|
+
schema
|
|
11
|
+
} = (0, _enumComment.getEnumComment)('mode', ['lazy', 'lazy-once', 'eager', 'weak'], 'lazy');
|
|
12
|
+
exports.schema = schema;
|
|
13
|
+
exports.webpackMode = webpackMode;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackPrefetch = exports.schema = void 0;
|
|
7
|
+
var _booleanComment = require("./booleanComment.cjs");
|
|
8
|
+
const schema = (0, _booleanComment.getSchema)();
|
|
9
|
+
exports.schema = schema;
|
|
10
|
+
const webpackPrefetch = (filepath, importPath, value, match) => {
|
|
11
|
+
const config = (0, _booleanComment.getConfig)(value, match, filepath, importPath);
|
|
12
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
13
|
+
if (!isActive) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
return 'webpackPrefetch: true';
|
|
17
|
+
};
|
|
18
|
+
exports.webpackPrefetch = webpackPrefetch;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.webpackPreload = exports.schema = void 0;
|
|
7
|
+
var _booleanComment = require("./booleanComment.cjs");
|
|
8
|
+
const schema = (0, _booleanComment.getSchema)();
|
|
9
|
+
exports.schema = schema;
|
|
10
|
+
const webpackPreload = (filepath, importPath, value, match) => {
|
|
11
|
+
const config = (0, _booleanComment.getConfig)(value, match, filepath, importPath);
|
|
12
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
13
|
+
if (!isActive) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
return 'webpackPreload: true';
|
|
17
|
+
};
|
|
18
|
+
exports.webpackPreload = webpackPreload;
|
package/dist/comment.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { commentFor } from './strategy.js';
|
|
2
|
+
const getCommenter = (filepath, options, logger) => (rgxMatch, capturedImportPath) => {
|
|
3
|
+
const importPath = capturedImportPath.trim();
|
|
4
|
+
const bareImportPath = importPath.replace(/['"`]/g, '');
|
|
5
|
+
const {
|
|
6
|
+
verbose,
|
|
7
|
+
match,
|
|
8
|
+
...magicCommentOptions
|
|
9
|
+
} = options;
|
|
10
|
+
const magicComment = Object.keys(magicCommentOptions).map(key => commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
|
|
11
|
+
const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `/* ${magicComment.join(', ')} */ ${importPath}` : `${importPath}`);
|
|
12
|
+
if (verbose) {
|
|
13
|
+
logger.info(`${filepath} : ${magicImport}`);
|
|
14
|
+
}
|
|
15
|
+
return magicImport;
|
|
16
|
+
};
|
|
17
|
+
export { getCommenter };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { getOverrideConfig, getOverrideSchema } from './util.js';
|
|
2
|
+
const getEnumComment = (name, enums, defaultValue) => {
|
|
3
|
+
const configSchema = {
|
|
4
|
+
type: 'object',
|
|
5
|
+
properties: {
|
|
6
|
+
active: {
|
|
7
|
+
oneOf: [{
|
|
8
|
+
type: 'boolean'
|
|
9
|
+
}, {
|
|
10
|
+
instanceof: 'Function'
|
|
11
|
+
}]
|
|
12
|
+
},
|
|
13
|
+
[name]: {
|
|
14
|
+
oneOf: [{
|
|
15
|
+
enum: enums
|
|
16
|
+
}, {
|
|
17
|
+
instanceof: 'Function'
|
|
18
|
+
}]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
additionalProperties: false
|
|
22
|
+
};
|
|
23
|
+
const schema = {
|
|
24
|
+
oneOf: [{
|
|
25
|
+
type: 'boolean'
|
|
26
|
+
}, {
|
|
27
|
+
type: 'string'
|
|
28
|
+
}, {
|
|
29
|
+
instanceof: 'Function'
|
|
30
|
+
}, {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
config: configSchema,
|
|
34
|
+
overrides: getOverrideSchema(configSchema)
|
|
35
|
+
},
|
|
36
|
+
required: ['config'],
|
|
37
|
+
additionalProperties: false
|
|
38
|
+
}]
|
|
39
|
+
};
|
|
40
|
+
const defaultConfig = {
|
|
41
|
+
active: true,
|
|
42
|
+
[name]: defaultValue
|
|
43
|
+
};
|
|
44
|
+
const getConfig = (value, filepath) => {
|
|
45
|
+
if (value === true) {
|
|
46
|
+
return defaultConfig;
|
|
47
|
+
}
|
|
48
|
+
if (value === false) {
|
|
49
|
+
return {
|
|
50
|
+
...defaultConfig,
|
|
51
|
+
active: false
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === 'string' || typeof value === 'function') {
|
|
55
|
+
return {
|
|
56
|
+
...defaultConfig,
|
|
57
|
+
[name]: value
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
let config = {
|
|
61
|
+
...defaultConfig,
|
|
62
|
+
...value.config
|
|
63
|
+
};
|
|
64
|
+
if (Array.isArray(value.overrides)) {
|
|
65
|
+
config = getOverrideConfig(value.overrides, filepath, config);
|
|
66
|
+
}
|
|
67
|
+
return config;
|
|
68
|
+
};
|
|
69
|
+
const comment = (filepath, importPath, value) => {
|
|
70
|
+
let enumValue = '';
|
|
71
|
+
const commentSuffix = `${name[0].toUpperCase()}${name.slice(1)}`;
|
|
72
|
+
const config = getConfig(value, filepath);
|
|
73
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
74
|
+
if (!isActive) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
if (typeof config[name] === 'function') {
|
|
78
|
+
enumValue = config[name](filepath, importPath);
|
|
79
|
+
}
|
|
80
|
+
if (typeof config[name] === 'string') {
|
|
81
|
+
enumValue = config[name];
|
|
82
|
+
}
|
|
83
|
+
if (!enums.includes(enumValue)) {
|
|
84
|
+
return '';
|
|
85
|
+
}
|
|
86
|
+
return `webpack${commentSuffix}: "${enumValue}"`;
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
comment,
|
|
90
|
+
schema
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
export { getEnumComment };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { loader as default } from './loader.js';
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { validate } from 'schema-utils';
|
|
2
|
+
import { schema } from './schema.js';
|
|
3
|
+
import { getCommenter } from './comment.js';
|
|
4
|
+
import { dynamicImportsWithoutComments } from './util.js';
|
|
5
|
+
const loader = function (source, map, meta) {
|
|
6
|
+
const options = this.getOptions();
|
|
7
|
+
const optionKeys = Object.keys(options);
|
|
8
|
+
const logger = this.getLogger('MCL');
|
|
9
|
+
validate(schema, options, {
|
|
10
|
+
name: 'magic-comments-loader'
|
|
11
|
+
});
|
|
12
|
+
const filepath = this.utils.contextify(this.rootContext, this.resourcePath);
|
|
13
|
+
const magicComments = getCommenter(filepath.replace(/^\.\/?/, ''), optionKeys.length > 0 ? options : {
|
|
14
|
+
match: 'module',
|
|
15
|
+
webpackChunkName: true
|
|
16
|
+
}, logger);
|
|
17
|
+
this.callback(null, source.replace(dynamicImportsWithoutComments, magicComments), map, meta);
|
|
18
|
+
};
|
|
19
|
+
export { loader };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { getOverrideConfig, getOverrideSchema } from './util.js';
|
|
2
|
+
const getRegexComment = type => {
|
|
3
|
+
const configSchema = {
|
|
4
|
+
type: 'object',
|
|
5
|
+
properties: {
|
|
6
|
+
active: {
|
|
7
|
+
oneOf: [{
|
|
8
|
+
type: 'boolean'
|
|
9
|
+
}, {
|
|
10
|
+
instanceof: 'Function'
|
|
11
|
+
}]
|
|
12
|
+
},
|
|
13
|
+
include: {
|
|
14
|
+
oneOf: [{
|
|
15
|
+
instanceof: 'Function'
|
|
16
|
+
}, {
|
|
17
|
+
instanceof: 'RegExp'
|
|
18
|
+
}]
|
|
19
|
+
},
|
|
20
|
+
exclude: {
|
|
21
|
+
oneOf: [{
|
|
22
|
+
instanceof: 'Function'
|
|
23
|
+
}, {
|
|
24
|
+
instanceof: 'RegExp'
|
|
25
|
+
}]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
additionalProperties: false
|
|
29
|
+
};
|
|
30
|
+
const schema = {
|
|
31
|
+
oneOf: [{
|
|
32
|
+
instanceof: 'Function'
|
|
33
|
+
}, {
|
|
34
|
+
instanceof: 'RegExp'
|
|
35
|
+
}, {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
config: configSchema,
|
|
39
|
+
overrides: getOverrideSchema(configSchema)
|
|
40
|
+
},
|
|
41
|
+
required: ['config'],
|
|
42
|
+
additionalProperties: false
|
|
43
|
+
}]
|
|
44
|
+
};
|
|
45
|
+
const getConfig = (value, filepath) => {
|
|
46
|
+
if (typeof value === 'function' || value instanceof RegExp) {
|
|
47
|
+
return {
|
|
48
|
+
active: true,
|
|
49
|
+
[type]: value
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
let config = {
|
|
53
|
+
active: true,
|
|
54
|
+
...value.config
|
|
55
|
+
};
|
|
56
|
+
if (Array.isArray(value.overrides)) {
|
|
57
|
+
config = getOverrideConfig(value.overrides, filepath, config);
|
|
58
|
+
}
|
|
59
|
+
return config;
|
|
60
|
+
};
|
|
61
|
+
const comment = (filepath, importPath, value) => {
|
|
62
|
+
const config = getConfig(value, filepath);
|
|
63
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
64
|
+
let regex = null;
|
|
65
|
+
if (!isActive) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
if (typeof config[type] === 'function') {
|
|
69
|
+
regex = config[type](filepath, importPath);
|
|
70
|
+
}
|
|
71
|
+
if (config[type] instanceof RegExp) {
|
|
72
|
+
regex = config[type];
|
|
73
|
+
}
|
|
74
|
+
if (!(regex instanceof RegExp)) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
const source = regex.source;
|
|
78
|
+
const typeName = `${type[0].toUpperCase()}${type.slice(1)}`;
|
|
79
|
+
/**
|
|
80
|
+
* Check if the provided RegExp ends in one or more '*'
|
|
81
|
+
* and if so be sure to escape the ending '/' in the
|
|
82
|
+
* comments regular expression so as not to break the
|
|
83
|
+
* comment and cause a SyntaxError.
|
|
84
|
+
*/
|
|
85
|
+
if (/(\*+)$/.test(source)) {
|
|
86
|
+
return `webpack${typeName}: /${source}\\/${regex.flags}`;
|
|
87
|
+
}
|
|
88
|
+
return `webpack${typeName}: /${source}/${regex.flags}`;
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
comment,
|
|
92
|
+
schema
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
export { getRegexComment };
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { schema as webpackChunkNameSchema } from './webpackChunkName.js';
|
|
2
|
+
import { schema as webpackFetchPrioritySchema } from './webpackFetchPriority.js';
|
|
3
|
+
import { schema as webpackModeSchema } from './webpackMode.js';
|
|
4
|
+
import { schema as webpackIgnoreSchema } from './webpackIgnore.js';
|
|
5
|
+
import { schema as webpackPrefetchSchema } from './webpackPrefetch.js';
|
|
6
|
+
import { schema as webpackPreloadSchema } from './webpackPreload.js';
|
|
7
|
+
import { schema as webpackExportsSchema } from './webpackExports.js';
|
|
8
|
+
import { schema as webpackIncludeSchema } from './webpackInclude.js';
|
|
9
|
+
import { schema as webpackExcludeSchema } from './webpackExclude.js';
|
|
10
|
+
const schema = {
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: {
|
|
13
|
+
verbose: {
|
|
14
|
+
type: 'boolean'
|
|
15
|
+
},
|
|
16
|
+
match: {
|
|
17
|
+
enum: ['module', 'import']
|
|
18
|
+
},
|
|
19
|
+
webpackChunkName: webpackChunkNameSchema,
|
|
20
|
+
webpackFetchPriority: webpackFetchPrioritySchema,
|
|
21
|
+
webpackMode: webpackModeSchema,
|
|
22
|
+
webpackIgnore: webpackIgnoreSchema,
|
|
23
|
+
webpackPrefetch: webpackPrefetchSchema,
|
|
24
|
+
webpackPreload: webpackPreloadSchema,
|
|
25
|
+
webpackExports: webpackExportsSchema,
|
|
26
|
+
webpackInclude: webpackIncludeSchema,
|
|
27
|
+
webpackExclude: webpackExcludeSchema
|
|
28
|
+
},
|
|
29
|
+
additionalProperties: false
|
|
30
|
+
};
|
|
31
|
+
export { schema };
|
package/dist/strategy.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { webpackChunkName } from './webpackChunkName.js';
|
|
2
|
+
import { webpackFetchPriority } from './webpackFetchPriority.js';
|
|
3
|
+
import { webpackMode } from './webpackMode.js';
|
|
4
|
+
import { webpackIgnore } from './webpackIgnore.js';
|
|
5
|
+
import { webpackPreload } from './webpackPreload.js';
|
|
6
|
+
import { webpackPrefetch } from './webpackPrefetch.js';
|
|
7
|
+
import { webpackExports } from './webpackExports.js';
|
|
8
|
+
import { webpackInclude } from './webpackInclude.js';
|
|
9
|
+
import { webpackExclude } from './webpackExclude.js';
|
|
10
|
+
const commentFor = {
|
|
11
|
+
webpackChunkName,
|
|
12
|
+
webpackFetchPriority,
|
|
13
|
+
webpackMode,
|
|
14
|
+
webpackIgnore,
|
|
15
|
+
webpackPreload,
|
|
16
|
+
webpackPrefetch,
|
|
17
|
+
webpackExports,
|
|
18
|
+
webpackInclude,
|
|
19
|
+
webpackExclude
|
|
20
|
+
};
|
|
21
|
+
export { commentFor };
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import micromatch from 'micromatch';
|
|
2
|
+
const pathIsMatch = (path, files) => {
|
|
3
|
+
const globs = [];
|
|
4
|
+
const notglobs = [];
|
|
5
|
+
if (!Array.isArray(files)) {
|
|
6
|
+
files = [files];
|
|
7
|
+
}
|
|
8
|
+
files.forEach(file => {
|
|
9
|
+
if (/^!/.test(file)) {
|
|
10
|
+
notglobs.push(file);
|
|
11
|
+
} else {
|
|
12
|
+
globs.push(file);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return (globs.length === 0 || globs.some(glob => micromatch.isMatch(path, glob))) && notglobs.every(notglob => micromatch.isMatch(path, notglob));
|
|
16
|
+
};
|
|
17
|
+
const getOverrideSchema = commentSchema => ({
|
|
18
|
+
type: 'array',
|
|
19
|
+
items: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: {
|
|
22
|
+
config: commentSchema,
|
|
23
|
+
files: {
|
|
24
|
+
oneOf: [{
|
|
25
|
+
type: 'string'
|
|
26
|
+
}, {
|
|
27
|
+
type: 'array',
|
|
28
|
+
items: {
|
|
29
|
+
type: 'string'
|
|
30
|
+
}
|
|
31
|
+
}]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: false
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const getOverrideConfig = (overrides, filepath, config) => {
|
|
38
|
+
const length = overrides.length;
|
|
39
|
+
for (let i = 0; i < length; i++) {
|
|
40
|
+
if (pathIsMatch(filepath, overrides[i].files)) {
|
|
41
|
+
return {
|
|
42
|
+
...config,
|
|
43
|
+
...overrides[i].config
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return config;
|
|
48
|
+
};
|
|
49
|
+
const importPrefix = /^(?:(\.{1,2}\/)+)|^\/|^.+:\/\/\/?[.-\w]+\//;
|
|
50
|
+
const dynamicImportsWithoutComments = /(?<![\w.]|#!|(?:\/{2}.+\n?)+|\/\*[\s\w]*?|\*.+?|['"`][^)$,\n]*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?!\s*?\*\/)/gm;
|
|
51
|
+
export { getOverrideConfig, getOverrideSchema, pathIsMatch, importPrefix, dynamicImportsWithoutComments };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { parse } from 'path';
|
|
2
|
+
import { getSchema, getConfig } from './booleanComment.js';
|
|
3
|
+
const schema = getSchema({
|
|
4
|
+
type: 'object',
|
|
5
|
+
properties: {
|
|
6
|
+
active: {
|
|
7
|
+
oneOf: [{
|
|
8
|
+
type: 'boolean'
|
|
9
|
+
}, {
|
|
10
|
+
instanceof: 'Function'
|
|
11
|
+
}]
|
|
12
|
+
},
|
|
13
|
+
basename: {
|
|
14
|
+
type: 'boolean'
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
additionalProperties: false
|
|
18
|
+
});
|
|
19
|
+
const webpackChunkName = (filepath, importPath, value, match) => {
|
|
20
|
+
const config = getConfig(value, match, filepath, importPath, {
|
|
21
|
+
active: true,
|
|
22
|
+
basename: false
|
|
23
|
+
});
|
|
24
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
25
|
+
if (!isActive) {
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
if (typeof config.dynamic === 'string') {
|
|
29
|
+
return `webpackChunkName: "${config.dynamic}"`;
|
|
30
|
+
}
|
|
31
|
+
const {
|
|
32
|
+
basename
|
|
33
|
+
} = config;
|
|
34
|
+
const {
|
|
35
|
+
dir,
|
|
36
|
+
name
|
|
37
|
+
} = parse(importPath);
|
|
38
|
+
const segments = `${dir}/${name}`.split('/').filter(segment => /\w/.test(segment));
|
|
39
|
+
const chunkName = basename ? name : segments.reduce((prev, curr) => {
|
|
40
|
+
/**
|
|
41
|
+
* Check for dynamic expressions in imports.
|
|
42
|
+
* If it exists, it has to be at least the second path
|
|
43
|
+
* segment or greater, i.e. can not be the first.
|
|
44
|
+
*
|
|
45
|
+
* @see https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import
|
|
46
|
+
*/
|
|
47
|
+
if (/^\${/.test(curr)) {
|
|
48
|
+
return prev ? `${prev}-[request]` : '[request]';
|
|
49
|
+
}
|
|
50
|
+
return prev ? `${prev}-${curr}` : curr;
|
|
51
|
+
}, '');
|
|
52
|
+
return `webpackChunkName: "${chunkName}"`;
|
|
53
|
+
};
|
|
54
|
+
export { webpackChunkName, schema };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getOverrideConfig, getOverrideSchema } from './util.js';
|
|
2
|
+
const configSchema = {
|
|
3
|
+
type: 'object',
|
|
4
|
+
properties: {
|
|
5
|
+
active: {
|
|
6
|
+
oneOf: [{
|
|
7
|
+
type: 'boolean'
|
|
8
|
+
}, {
|
|
9
|
+
instanceof: 'Function'
|
|
10
|
+
}]
|
|
11
|
+
},
|
|
12
|
+
exports: {
|
|
13
|
+
instanceof: 'Function'
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
additionalProperties: false
|
|
17
|
+
};
|
|
18
|
+
const schema = {
|
|
19
|
+
oneOf: [{
|
|
20
|
+
instanceof: 'Function'
|
|
21
|
+
}, {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
config: configSchema,
|
|
25
|
+
overrides: getOverrideSchema(configSchema)
|
|
26
|
+
},
|
|
27
|
+
required: ['config'],
|
|
28
|
+
additionalProperties: false
|
|
29
|
+
}]
|
|
30
|
+
};
|
|
31
|
+
const defaultConfig = {
|
|
32
|
+
active: true
|
|
33
|
+
};
|
|
34
|
+
const getConfig = (value, filepath) => {
|
|
35
|
+
if (typeof value === 'function') {
|
|
36
|
+
return {
|
|
37
|
+
...defaultConfig,
|
|
38
|
+
exports: value
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
let config = {
|
|
42
|
+
...defaultConfig,
|
|
43
|
+
...value.config
|
|
44
|
+
};
|
|
45
|
+
if (Array.isArray(value.overrides)) {
|
|
46
|
+
config = getOverrideConfig(value.overrides, filepath, config);
|
|
47
|
+
}
|
|
48
|
+
return config;
|
|
49
|
+
};
|
|
50
|
+
const webpackExports = (filepath, importPath, value) => {
|
|
51
|
+
let exports = '';
|
|
52
|
+
let configExports = [];
|
|
53
|
+
const config = getConfig(value, filepath);
|
|
54
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
55
|
+
if (!isActive || typeof config.exports !== 'function') {
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
configExports = config.exports(filepath, importPath);
|
|
59
|
+
if (!Array.isArray(configExports)) {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
exports = `["${configExports.reduce((curr, next) => `${curr}", "${next}`)}"]`;
|
|
63
|
+
return `webpackExports: ${exports}`;
|
|
64
|
+
};
|
|
65
|
+
export { webpackExports, schema };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getSchema, getConfig } from './booleanComment.js';
|
|
2
|
+
const schema = getSchema();
|
|
3
|
+
const webpackIgnore = (filepath, importPath, value, match) => {
|
|
4
|
+
const config = getConfig(value, match, filepath, importPath);
|
|
5
|
+
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
|
|
6
|
+
if (!isActive) {
|
|
7
|
+
return '';
|
|
8
|
+
}
|
|
9
|
+
return 'webpackIgnore: true';
|
|
10
|
+
};
|
|
11
|
+
export { webpackIgnore, schema };
|