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.
@@ -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,23 @@
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
+ const getCommenter = (filepath, options, logger) => (rgxMatch, capturedImportPath) => {
9
+ const importPath = capturedImportPath.trim();
10
+ const bareImportPath = importPath.replace(/['"`]/g, '');
11
+ const {
12
+ verbose,
13
+ match,
14
+ ...magicCommentOptions
15
+ } = options;
16
+ const magicComment = Object.keys(magicCommentOptions).map(key => _strategy.commentFor[key](filepath, bareImportPath, magicCommentOptions[key], match)).filter(Boolean);
17
+ const magicImport = rgxMatch.replace(capturedImportPath, magicComment.length > 0 ? `/* ${magicComment.join(', ')} */ ${importPath}` : `${importPath}`);
18
+ if (verbose) {
19
+ logger.info(`${filepath} : ${magicImport}`);
20
+ }
21
+ return magicImport;
22
+ };
23
+ 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,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "default", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _loader.loader;
10
+ }
11
+ });
12
+ var _loader = require("./loader.cjs");
@@ -0,0 +1,25 @@
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 _comment = require("./comment.cjs");
10
+ var _util = require("./util.cjs");
11
+ const loader = function (source, map, meta) {
12
+ const options = this.getOptions();
13
+ const optionKeys = Object.keys(options);
14
+ const logger = this.getLogger('MCL');
15
+ (0, _schemaUtils.validate)(_schema.schema, options, {
16
+ name: 'magic-comments-loader'
17
+ });
18
+ const filepath = this.utils.contextify(this.rootContext, this.resourcePath);
19
+ const magicComments = (0, _comment.getCommenter)(filepath.replace(/^\.\/?/, ''), optionKeys.length > 0 ? options : {
20
+ match: 'module',
21
+ webpackChunkName: true
22
+ }, logger);
23
+ this.callback(null, source.replace(_util.dynamicImportsWithoutComments, magicComments), map, meta);
24
+ };
25
+ exports.loader = loader;
@@ -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,37 @@
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
+ match: {
23
+ enum: ['module', 'import']
24
+ },
25
+ webpackChunkName: _webpackChunkName.schema,
26
+ webpackFetchPriority: _webpackFetchPriority.schema,
27
+ webpackMode: _webpackMode.schema,
28
+ webpackIgnore: _webpackIgnore.schema,
29
+ webpackPrefetch: _webpackPrefetch.schema,
30
+ webpackPreload: _webpackPreload.schema,
31
+ webpackExports: _webpackExports.schema,
32
+ webpackInclude: _webpackInclude.schema,
33
+ webpackExclude: _webpackExclude.schema
34
+ },
35
+ additionalProperties: false
36
+ };
37
+ exports.schema = schema;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.commentFor = 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 _webpackPreload = require("./webpackPreload.cjs");
12
+ var _webpackPrefetch = require("./webpackPrefetch.cjs");
13
+ var _webpackExports = require("./webpackExports.cjs");
14
+ var _webpackInclude = require("./webpackInclude.cjs");
15
+ var _webpackExclude = require("./webpackExclude.cjs");
16
+ const commentFor = {
17
+ webpackChunkName: _webpackChunkName.webpackChunkName,
18
+ webpackFetchPriority: _webpackFetchPriority.webpackFetchPriority,
19
+ webpackMode: _webpackMode.webpackMode,
20
+ webpackIgnore: _webpackIgnore.webpackIgnore,
21
+ webpackPreload: _webpackPreload.webpackPreload,
22
+ webpackPrefetch: _webpackPrefetch.webpackPrefetch,
23
+ webpackExports: _webpackExports.webpackExports,
24
+ webpackInclude: _webpackInclude.webpackInclude,
25
+ webpackExclude: _webpackExclude.webpackExclude
26
+ };
27
+ exports.commentFor = commentFor;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.pathIsMatch = exports.importPrefix = exports.getOverrideSchema = exports.getOverrideConfig = exports.dynamicImportsWithoutComments = void 0;
7
+ var _micromatch = _interopRequireDefault(require("micromatch"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ const pathIsMatch = (path, files) => {
10
+ const globs = [];
11
+ const notglobs = [];
12
+ if (!Array.isArray(files)) {
13
+ files = [files];
14
+ }
15
+ files.forEach(file => {
16
+ if (/^!/.test(file)) {
17
+ notglobs.push(file);
18
+ } else {
19
+ globs.push(file);
20
+ }
21
+ });
22
+ return (globs.length === 0 || globs.some(glob => _micromatch.default.isMatch(path, glob))) && notglobs.every(notglob => _micromatch.default.isMatch(path, notglob));
23
+ };
24
+ exports.pathIsMatch = pathIsMatch;
25
+ const getOverrideSchema = commentSchema => ({
26
+ type: 'array',
27
+ items: {
28
+ type: 'object',
29
+ properties: {
30
+ config: commentSchema,
31
+ files: {
32
+ oneOf: [{
33
+ type: 'string'
34
+ }, {
35
+ type: 'array',
36
+ items: {
37
+ type: 'string'
38
+ }
39
+ }]
40
+ }
41
+ },
42
+ additionalProperties: false
43
+ }
44
+ });
45
+ exports.getOverrideSchema = getOverrideSchema;
46
+ const getOverrideConfig = (overrides, filepath, config) => {
47
+ const length = overrides.length;
48
+ for (let i = 0; i < length; i++) {
49
+ if (pathIsMatch(filepath, overrides[i].files)) {
50
+ return {
51
+ ...config,
52
+ ...overrides[i].config
53
+ };
54
+ }
55
+ }
56
+ return config;
57
+ };
58
+ exports.getOverrideConfig = getOverrideConfig;
59
+ const importPrefix = /^(?:(\.{1,2}\/)+)|^\/|^.+:\/\/\/?[.-\w]+\//;
60
+ exports.importPrefix = importPrefix;
61
+ const dynamicImportsWithoutComments = /(?<![\w.]|#!|(?:\/{2}.+\n?)+|\/\*[\s\w]*?|\*.+?|['"`][^)$,\n]*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?!\s*?\*\/)/gm;
62
+ exports.dynamicImportsWithoutComments = dynamicImportsWithoutComments;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.webpackChunkName = exports.schema = void 0;
7
+ var _path = require("path");
8
+ var _booleanComment = require("./booleanComment.cjs");
9
+ const schema = (0, _booleanComment.getSchema)({
10
+ type: 'object',
11
+ properties: {
12
+ active: {
13
+ oneOf: [{
14
+ type: 'boolean'
15
+ }, {
16
+ instanceof: 'Function'
17
+ }]
18
+ },
19
+ basename: {
20
+ type: 'boolean'
21
+ }
22
+ },
23
+ additionalProperties: false
24
+ });
25
+ exports.schema = schema;
26
+ const webpackChunkName = (filepath, importPath, value, match) => {
27
+ const config = (0, _booleanComment.getConfig)(value, match, filepath, importPath, {
28
+ active: true,
29
+ basename: false
30
+ });
31
+ const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active;
32
+ if (!isActive) {
33
+ return '';
34
+ }
35
+ if (typeof config.dynamic === 'string') {
36
+ return `webpackChunkName: "${config.dynamic}"`;
37
+ }
38
+ const {
39
+ basename
40
+ } = config;
41
+ const {
42
+ dir,
43
+ name
44
+ } = (0, _path.parse)(importPath);
45
+ const segments = `${dir}/${name}`.split('/').filter(segment => /\w/.test(segment));
46
+ const chunkName = basename ? name : segments.reduce((prev, curr) => {
47
+ /**
48
+ * Check for dynamic expressions in imports.
49
+ * If it exists, it has to be at least the second path
50
+ * segment or greater, i.e. can not be the first.
51
+ *
52
+ * @see https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import
53
+ */
54
+ if (/^\${/.test(curr)) {
55
+ return prev ? `${prev}-[request]` : '[request]';
56
+ }
57
+ return prev ? `${prev}-${curr}` : curr;
58
+ }, '');
59
+ return `webpackChunkName: "${chunkName}"`;
60
+ };
61
+ exports.webpackChunkName = webpackChunkName;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.webpackExclude = exports.schema = void 0;
7
+ var _regexComment = require("./regexComment.cjs");
8
+ const {
9
+ comment: webpackExclude,
10
+ schema
11
+ } = (0, _regexComment.getRegexComment)('exclude');
12
+ exports.schema = schema;
13
+ exports.webpackExclude = webpackExclude;