@oroinc/oro-webpack-config-builder 5.1.0-alpha32 → 5.1.0-alpha35

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.
@@ -1,7 +1,9 @@
1
1
  const path = require('path');
2
2
  const wildcard = require('wildcard');
3
3
  const _ = require('underscore');
4
- const messages = require('../messages')
4
+ const {isProdMode} = require('../utils');
5
+ const {assetsValidation} = require('../validation');
6
+ const StylesError = require('../validation/errors/styles-error');
5
7
 
6
8
  /**
7
9
  * @typedef ThemeGroupConfig
@@ -30,21 +32,24 @@ class StyleLoader {
30
32
  getThemeEntryPoints(theme, buildPath) {
31
33
  const {themeConfig, settings = {}} = this._fetchThemeConfig(theme);
32
34
 
35
+ assetsValidation.checkFullSchema(themeConfig, this._configLoader.processedFiles, theme);
36
+
33
37
  const entryPoints = {};
34
38
  const writingOptions = {};
35
39
  for (const [group, config] of Object.entries(themeConfig)) {
36
40
  let {inputs, entries = [], output, auto_rtl_inputs: rtlMasks = []} = config;
37
41
 
38
- if (output === void 0) {
39
- throw new Error(messages.assetsMissedOutput(group, theme));
42
+ if (isProdMode() && output === void 0) {
43
+ throw new StylesError('output', group, theme);
40
44
  }
41
45
 
42
- if (inputs === void 0) {
43
- throw new Error(messages.assetsMissedInput(group, theme));
46
+ if (isProdMode() && inputs === void 0) {
47
+ throw new StylesError('inputs', group, theme);
44
48
  }
45
49
 
46
50
  inputs = this._overrideInputs(inputs);
47
51
  inputs = this._sortInputs(inputs);
52
+ inputs = this._applyInputsBasePathPrefix(inputs);
48
53
 
49
54
  if (settings.rtlSupport) {
50
55
  writingOptions.ignoreRTLInputs = _.difference(inputs, this._matchInputs(rtlMasks, inputs));
@@ -54,7 +59,7 @@ class StyleLoader {
54
59
  const filePath = path.join(buildPath, output);
55
60
  entryPoints[entryPointName] = [
56
61
  ...entries,
57
- this._entryPointFileWriter.write('./../../../', inputs, filePath, writingOptions)
62
+ this._entryPointFileWriter.write('./../../', inputs, filePath, writingOptions)
58
63
  ];
59
64
  }
60
65
  return entryPoints;
@@ -89,7 +94,7 @@ class StyleLoader {
89
94
  const oldInputIndex = mappedInputs.findIndex(item => item === oldInput);
90
95
 
91
96
  if (oldInputIndex === -1) {
92
- // old input does not exists any more
97
+ // old input does not exist anymore
93
98
  mappedInputs.push(newInput);
94
99
  } else if (newInput) {
95
100
  // replace input
@@ -120,11 +125,11 @@ class StyleLoader {
120
125
  inputs.forEach(input => {
121
126
  if (input.indexOf('/settings/primary-settings') > 0) {
122
127
  primarySettingsInputs.push(input);
123
- } else if (input.indexOf('/settings/') > 0) {
128
+ } else if (input.indexOf('/settings/') > 0) {
124
129
  settingsInputs.push(input);
125
- } else if (input.indexOf('/variables/primary-variables') > 0) {
130
+ } else if (input.indexOf('/variables/primary-variables') > 0) {
126
131
  primaryVariablesInputs.push(input);
127
- } else if (input.indexOf('/variables/') > 0) {
132
+ } else if (input.indexOf('/variables/') > 0) {
128
133
  variablesInputs.push(input);
129
134
  } else {
130
135
  restInputs.push(input);
@@ -158,6 +163,26 @@ class StyleLoader {
158
163
 
159
164
  return _.unique(whiteListedInputs);
160
165
  }
166
+
167
+ /**
168
+ * Considering base path as application's root.
169
+ * Bundles based *.scss sources go with '../' prefix.
170
+ *
171
+ * @param {string[]} inputs
172
+ * @private
173
+ */
174
+ _applyInputsBasePathPrefix(inputs) {
175
+ const processedInputs = [];
176
+
177
+ inputs.forEach(input => {
178
+ if (input.indexOf('bundles') === 0) {
179
+ input = '../' + input;
180
+ }
181
+ processedInputs.push(input);
182
+ });
183
+
184
+ return processedInputs;
185
+ }
161
186
  }
162
187
 
163
188
  module.exports = StyleLoader;
@@ -1,4 +1,4 @@
1
- const messages = require('./messages');
1
+ const JSModulesExtraModulesError = require('./validation/errors/jsmodules-extra-modules-error');
2
2
 
3
3
  class ThemeConfigFactory {
4
4
  /**
@@ -19,11 +19,7 @@ class ThemeConfigFactory {
19
19
  * @return {Object} Merged Configs loaded from all the bundles Yaml files matched by filePath
20
20
  */
21
21
  loadConfig(theme, configFilepath) {
22
- try {
23
- return this._configLoader.loadConfig(theme, configFilepath)
24
- } catch (e) {
25
- throw new Error(messages.jsModulesError(theme));
26
- }
22
+ return this._configLoader.loadConfig(theme, configFilepath);
27
23
  }
28
24
 
29
25
  extendConfig(baseConfig, extraConfig) {
@@ -31,7 +27,7 @@ class ThemeConfigFactory {
31
27
  aliases = {},
32
28
  configs,
33
29
  map = {},
34
- shim = {},
30
+ shim = {}
35
31
  } = baseConfig;
36
32
 
37
33
  const {
@@ -43,7 +39,7 @@ class ThemeConfigFactory {
43
39
 
44
40
  const beyondKeys = Object.keys(rest);
45
41
  if (beyondKeys.length) {
46
- throw new Error( messages.jsExtraModulesError(beyondKeys));
42
+ throw new JSModulesExtraModulesError(beyondKeys);
47
43
  }
48
44
 
49
45
  return {
@@ -69,7 +65,7 @@ class ThemeConfigFactory {
69
65
  shim = {},
70
66
  configs,
71
67
  aliases = {},
72
- ['app-modules']: appModules,
68
+ ['app-modules']: appModules = [],
73
69
  ['dynamic-imports']: dynamicImports
74
70
  } = jsModulesConfig;
75
71
 
package/utils.js ADDED
@@ -0,0 +1,30 @@
1
+ let prodMode;
2
+ let verboseMode;
3
+
4
+ module.exports = {
5
+ isVerboseMode() {
6
+ if (verboseMode === void 0) {
7
+ verboseMode = process.argv.filter(arg => {
8
+ // Depending on how the command was run (-v, -vv, -vvv, --verbose) -
9
+ // arguments can be present in different formats: stats=normal, stats=detailed, stats=verbose, verbose
10
+ return arg.search(/^verbose|(^stats=(normal|detailed|verbose))/) !== -1;
11
+ }).length > 0;
12
+ }
13
+
14
+ return verboseMode;
15
+ },
16
+ isProdMode() {
17
+ if (prodMode !== void 0) {
18
+ return prodMode;
19
+ }
20
+
21
+ process.argv.forEach(arg => {
22
+ if (prodMode) {
23
+ return;
24
+ }
25
+ prodMode = ['--mode=production', '--env=prod'].some(item => item === arg);
26
+ });
27
+
28
+ return prodMode;
29
+ }
30
+ };
@@ -0,0 +1,104 @@
1
+ const {isProdMode} = require('../utils');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const schema = require('./schemas/assets-schema');
5
+ const fullSchema = require('./schemas/assets-schema-full');
6
+ const schemaValidator = require('./schema-validator');
7
+ const EventEmitter = require('events');
8
+ const emitter = new EventEmitter();
9
+ const AssetsSchemaError = require('./errors/assets-schema-error');
10
+ const AssetsInputFileError = require('./errors/assets-input-file-error');
11
+
12
+ module.exports = Object.assign({}, schemaValidator, {
13
+ emitter,
14
+
15
+ /**
16
+ * @param {string} filePath
17
+ * @param {Object} doc
18
+ * @param {string} theme
19
+ * @returns {boolean|undefined}
20
+ */
21
+ checkSchema(filePath, doc, theme) {
22
+ if (isProdMode()) {
23
+ return;
24
+ }
25
+ const result = this.validateSchema(schema, doc);
26
+
27
+ if (!result.valid) {
28
+ const error = new AssetsSchemaError(result.formattedError, [filePath], theme);
29
+
30
+ this.emitter.emit('error', error);
31
+ }
32
+
33
+ return result.valid;
34
+ },
35
+
36
+ /**
37
+ * @param {Object} doc
38
+ * @param {Array} files
39
+ * @param {string} theme
40
+ * @returns {boolean|undefined}
41
+ */
42
+ checkFullSchema(doc, files = [], theme) {
43
+ if (isProdMode()) {
44
+ return;
45
+ }
46
+ const result = this.validateSchema(fullSchema, doc);
47
+
48
+ if (!result.valid) {
49
+ const error = new AssetsSchemaError(result.formattedError, files, theme);
50
+
51
+ this.emitter.emit('error', error);
52
+ }
53
+
54
+ return result.valid;
55
+ },
56
+
57
+ /**
58
+ * @param {string} filePath
59
+ * @param {Object} doc
60
+ * @param {string} theme
61
+ * @returns {boolean|undefined}
62
+ */
63
+ checkInputsExist(filePath, doc, theme) {
64
+ if (isProdMode()) {
65
+ return;
66
+ }
67
+
68
+ let valid = true;
69
+ for (const [, props] of Object.entries(doc)) {
70
+ if (!Array.isArray(props.inputs)) {
71
+ continue;
72
+ }
73
+
74
+ const files = [];
75
+ props.inputs.forEach(input => {
76
+ if (typeof input === 'object') {
77
+ const newPath = Object.values(input)[0];
78
+
79
+ if (newPath === '~') {
80
+ return;
81
+ }
82
+ input = newPath;
83
+ }
84
+
85
+ const fullBasePath = path.resolve(input);
86
+ const fullPath = path.resolve(this._publicPath + input);
87
+ // skip the path to global node modules,
88
+ // e.g. '~bootstrap/scss/bootstrap'
89
+ if (!input.startsWith('~') && !fs.existsSync(fullPath) && !fs.existsSync(fullBasePath)) {
90
+ files.push(input);
91
+ }
92
+ });
93
+ if (files.length) {
94
+ valid = false;
95
+ files.forEach(notExistFile => {
96
+ const error = new AssetsInputFileError(notExistFile, filePath);
97
+
98
+ this.emitter.emit('error', error);
99
+ });
100
+ }
101
+ }
102
+ return valid;
103
+ }
104
+ });
@@ -0,0 +1,24 @@
1
+ const BaseError = require('./base-error');
2
+
3
+ class AssetsInputFileError extends BaseError {
4
+ /**
5
+ * @example
6
+ * Invalid styles config: input file does not exist /scss/test.scss
7
+ * at /config/assets.yml
8
+ * Please, find more information in the documentation https://doc.oroinc.com/...
9
+ *
10
+ * @param {string} notExistFile
11
+ * @param {string} path
12
+ */
13
+ constructor(notExistFile, path) {
14
+ const msg = `input file does not exist: ${notExistFile}\nat ${path}:1`;
15
+
16
+ super(msg);
17
+
18
+ this.name = 'Invalid styles config';
19
+ this.docLink =
20
+ 'https://doc.oroinc.com/backend/bundles/platform/AssetBundle/#load-scss-or-css-files-from-the-bundle';
21
+ }
22
+ }
23
+
24
+ module.exports = AssetsInputFileError;
@@ -0,0 +1,40 @@
1
+ const BaseError = require('./base-error');
2
+
3
+ class AssetsSchemaError extends BaseError {
4
+ /**
5
+ * @example
6
+ * Invalid styles config: the "assets.yml" in the "default" theme file does not match the API schema.
7
+ * styles.inputs[0] should be: string | object
8
+ * at /config/assets.yml
9
+ * Please, find more information in the documentation https://doc.oroinc.com/...
10
+ *
11
+ * @example
12
+ * Invalid styles config: the "assets.yml" file the "default" theme does not match the API schema.
13
+ * styles misses the property 'output'.
14
+ * List of processed files for the "default" theme:
15
+ * /default/config/assets.yml
16
+ * /default/config/assets.yml
17
+ * Please, find more information in the documentation https://doc.oroinc.com/...
18
+ *
19
+ * @param {string} reason
20
+ * @param {Array} files
21
+ * @param {string} theme
22
+ */
23
+ constructor(reason, files, theme) {
24
+ let msg = `the "assets.yml" file in the "${theme}" theme does not match the API schema.\n${reason}`;
25
+
26
+ const filesListMsg = BaseError.generateFilesListMsg(files, theme);
27
+
28
+ if (filesListMsg.length) {
29
+ msg = `${msg}\n${filesListMsg}`;
30
+ }
31
+
32
+ super(msg);
33
+
34
+ this.name = 'Invalid styles config';
35
+ this.docLink =
36
+ 'https://doc.oroinc.com/backend/bundles/platform/AssetBundle/#load-scss-or-css-files-from-the-bundle';
37
+ }
38
+ }
39
+
40
+ module.exports = AssetsSchemaError;
@@ -0,0 +1,37 @@
1
+ const {isVerboseMode} = require('../../utils');
2
+
3
+ class BaseError extends Error {
4
+ /**
5
+ * Prepares documentation phrase
6
+ * @returns {string}
7
+ */
8
+ get extra() {
9
+ return this.docLink ? `Please, find more information in the documentation ${this.docLink}` : '';
10
+ }
11
+
12
+ /**
13
+ * Creates an instance of BaseError
14
+ * @param {string} message error message
15
+ */
16
+ constructor(message) {
17
+ super(message);
18
+ }
19
+
20
+ /**
21
+ * Creates a part of message about processed files
22
+ * @param {Array} files
23
+ * @param {string} theme
24
+ * @returns {string}
25
+ */
26
+ static generateFilesListMsg(files, theme) {
27
+ if (files.length === 1) {
28
+ return `at ${files[0]}:1`;
29
+ } else if (isVerboseMode()) {
30
+ return `List of processed files for the "${theme}" theme:\n ${files.join('\n ')}`;
31
+ }
32
+
33
+ return '';
34
+ }
35
+ }
36
+
37
+ module.exports = BaseError;
@@ -0,0 +1,22 @@
1
+ const BaseError = require('./base-error');
2
+
3
+ class JsmodulesExtraModulesError extends BaseError {
4
+ /**
5
+ * @example
6
+ * Failed assembly JS: sections ["shim"] are not allowed in extra js build definition.
7
+ * Please, find more information in the documentation https://doc.oroinc.com/...
8
+ *
9
+ * @param {Array} parts
10
+ */
11
+ constructor(parts) {
12
+ const msg = `sections ["${parts.join('", "')}"] are not allowed in extra js build definition.`;
13
+
14
+ super(msg);
15
+
16
+ this.name = 'Failed assembly JS';
17
+ this.docLink =
18
+ 'https://doc.oroinc.com/master/frontend/storefront/how-to/how-to-create-extra-js-build-for-landing-page';
19
+ }
20
+ }
21
+
22
+ module.exports = JsmodulesExtraModulesError;
@@ -0,0 +1,40 @@
1
+ const BaseError = require('./base-error');
2
+
3
+ class JSModulesSchemaError extends BaseError {
4
+ /**
5
+ * @example
6
+ * Invalid JS config: the "jsmodules.yml" files in the "default" theme do not match the API schema.
7
+ * has an unknown property 'rest'.
8
+ * at default/config/jsmodules.yml
9
+ * Please, find more information in the documentation https://doc.oroinc.com/...
10
+ *
11
+ * @example
12
+ * Invalid JS config: the "jsmodules.yml" files in the "default" theme do not match the API schema.
13
+ * misses the property 'entry'.
14
+ * List of processed files for the "default" theme:
15
+ * /default/config/jsmodules.yml
16
+ * /default/config/jsmodules.yml
17
+ * Please, find more information in the documentation https://doc.oroinc.com/...
18
+ *
19
+ * @param {string} reason
20
+ * @param {Array} files
21
+ * @param {string} theme
22
+ */
23
+ constructor(reason, files, theme) {
24
+ let msg = `the "jsmodules.yml" files in the "${theme}" theme do not match the API schema.\n${reason}`;
25
+
26
+ const filesListMsg = BaseError.generateFilesListMsg(files, theme);
27
+
28
+ if (filesListMsg.length) {
29
+ msg = `${msg}\n${filesListMsg}`;
30
+ }
31
+
32
+ super(msg);
33
+
34
+ this.name = 'Invalid JS config';
35
+ this.docLink =
36
+ 'https://doc.oroinc.com/backend/bundles/platform/AssetBundle/#create-jsmodules-yml-configuration';
37
+ }
38
+ }
39
+
40
+ module.exports = JSModulesSchemaError;
@@ -0,0 +1,24 @@
1
+ const BaseError = require('./base-error');
2
+
3
+ class StylesError extends BaseError {
4
+ /**
5
+ * @example
6
+ * Failed assembly styles: the "output" for "styles" entry point in "default" theme is not defined.
7
+ * Please, find more information in the documentation https://doc.oroinc.com/...
8
+ *
9
+ * @param {string} key
10
+ * @param {string} group
11
+ * @param {string} theme
12
+ */
13
+ constructor(key, group, theme) {
14
+ const msg = `the "${key}" for "${group}" entry point in "${theme}" theme is not defined.`;
15
+
16
+ super(msg);
17
+
18
+ this.name = 'Failed assembly styles';
19
+ this.docLink =
20
+ 'https://doc.oroinc.com/backend/bundles/platform/AssetBundle/#load-scss-or-css-files-from-the-bundle';
21
+ }
22
+ }
23
+
24
+ module.exports = StylesError;
@@ -0,0 +1,36 @@
1
+ const assetsValidation = require('./assets-validator');
2
+ const jsmodulesValidator = require('./jsmodules-validator');
3
+
4
+ const isJSModulesPath = path => /jsmodules([-a-zA-Z\d]*)\.yml$/.test(path);
5
+ const isAssetsPath = path => /assets\.yml$/.test(path);
6
+
7
+ module.exports = {
8
+ assetsValidation,
9
+ jsmodulesValidator,
10
+
11
+ /**
12
+ * set public path for all validators
13
+ * {string} path
14
+ */
15
+ setPublicPath(path) {
16
+ [assetsValidation, jsmodulesValidator].forEach(validator => validator.setPublicPath(path));
17
+ },
18
+
19
+ /**
20
+ * Run appropriate validator
21
+ * {string} filePath
22
+ * {Object} doc
23
+ * {string} theme
24
+ */
25
+ checkSchema(filePath, doc, theme) {
26
+ if (isJSModulesPath(filePath)) {
27
+ jsmodulesValidator.checkSchema(filePath, doc, theme);
28
+ } else if (isAssetsPath(filePath)) {
29
+ const validSchema = assetsValidation.checkSchema(filePath, doc, theme);
30
+
31
+ if (validSchema) {
32
+ assetsValidation.checkInputsExist(filePath, doc, theme);
33
+ }
34
+ }
35
+ }
36
+ };
@@ -0,0 +1,53 @@
1
+ const schema = require('./schemas/jsmodules-schema');
2
+ const fullSchema = require('./schemas/jsmodules-schema-full');
3
+ const schemaValidator = require('./schema-validator');
4
+ const {isProdMode} = require('../utils');
5
+ const EventEmitter = require('events');
6
+ const emitter = new EventEmitter();
7
+ const JSModulesSchemaError = require('./errors/jsmodules-schema-error');
8
+
9
+ module.exports = Object.assign({}, schemaValidator, {
10
+ emitter,
11
+
12
+ /**
13
+ * @param {string} filePath
14
+ * @param {Object} doc
15
+ * @param {string} theme
16
+ * @returns {boolean|undefined}
17
+ */
18
+ checkSchema(filePath, doc, theme) {
19
+ if (isProdMode()) {
20
+ return;
21
+ }
22
+ const result = this.validateSchema(schema, doc);
23
+
24
+ if (!result.valid) {
25
+ const error = new JSModulesSchemaError(result.formattedError, [filePath], theme);
26
+
27
+ this.emitter.emit('error', error);
28
+ }
29
+
30
+ return result.valid;
31
+ },
32
+
33
+ /**
34
+ * @param {Object} doc
35
+ * @param {Array} files
36
+ * @param {string} theme
37
+ * @returns {boolean|undefined}
38
+ */
39
+ checkFullSchema(doc, files = [], theme) {
40
+ if (isProdMode()) {
41
+ return;
42
+ }
43
+ const result = this.validateSchema(fullSchema, doc);
44
+
45
+ if (!result.valid) {
46
+ const error = new JSModulesSchemaError(result.formattedError, files, theme);
47
+
48
+ this.emitter.emit('error', error);
49
+ }
50
+
51
+ return result.valid;
52
+ }
53
+ });
@@ -0,0 +1,62 @@
1
+ const {validate} = require('schema-utils');
2
+
3
+ module.exports = {
4
+ /**
5
+ * Symfony public directory path related to application root folder
6
+ * {string}
7
+ */
8
+ _publicPath: 'public/',
9
+
10
+ setPublicPath(path) {
11
+ this._publicPath = path;
12
+ },
13
+
14
+ /**
15
+ * Validates data according to the schema
16
+ * @param {Object} scheme
17
+ * @param {Object} data
18
+ * @param {Object} [options]
19
+ * @returns {Object}
20
+ */
21
+ validateSchema(scheme, data, options) {
22
+ const result = {
23
+ valid: true
24
+ };
25
+
26
+ try {
27
+ const baseDataPath = 'configuration';
28
+ const basePathReg = /^configuration\./;
29
+ validate(scheme, data, {
30
+ baseDataPath,
31
+ ...options || {},
32
+ postFormatter(formattedError, error) {
33
+ if (result.formattedError === void 0) {
34
+ result.formattedError = [];
35
+ }
36
+ result.error = error;
37
+
38
+ if (
39
+ Array.isArray(error.params.type) ||
40
+ error.params.missingProperty
41
+ ) {
42
+ // remove an excess line break
43
+ formattedError = formattedError.replace(/\n/g, ' ');
44
+ }
45
+
46
+ formattedError = formattedError.replace(basePathReg, '');
47
+ result.formattedError.push(formattedError.trim());
48
+ return formattedError;
49
+ }
50
+ });
51
+ } catch (e) {
52
+ result.valid = false;
53
+ result.errorMessage = e.message;
54
+ }
55
+
56
+ if (result.formattedError) {
57
+ result.formattedError = result.formattedError.join('\n');
58
+ }
59
+
60
+ return result;
61
+ }
62
+ };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Schema to validate assets.yml files
3
+ *
4
+ * @description
5
+ * Full scheme complements "assets-schema" one.
6
+ * It should have only rules which are not defined in "assets-schema" schema due to avoid duplicates in error messages
7
+ */
8
+ module.exports = {
9
+ type: 'object',
10
+ patternProperties: {
11
+ '.*': {
12
+ type: 'object',
13
+ properties: {
14
+ inputs: {
15
+ description: 'The "inputs" property is an array of files to load.',
16
+ minItems: 1
17
+ }
18
+ },
19
+ required: ['inputs', 'output']
20
+ }
21
+ }
22
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Schema to validate assets.yml files
3
+ */
4
+ module.exports = {
5
+ type: 'object',
6
+ patternProperties: {
7
+ '.*': {
8
+ type: 'object',
9
+ properties: {
10
+ inputs: {
11
+ description: 'The "inputs" property is an array of files to load.',
12
+ type: 'array',
13
+ items: {
14
+ type: ['string', 'object']
15
+ }
16
+ },
17
+ output: {
18
+ description: 'Output file path inside "public/" directory for the entry point',
19
+ type: 'string'
20
+ },
21
+ auto_rtl_inputs: {
22
+ description: 'List of wildcard file masks for inputs that has to be processed with RTL plugin.',
23
+ type: 'array',
24
+ items: {
25
+ type: 'string'
26
+ }
27
+ }
28
+ },
29
+ additionalProperties: false
30
+ }
31
+ }
32
+ };