@oroinc/oro-webpack-config-builder 6.1.0-lts04 → 6.1.0-lts06

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/error-handler.js CHANGED
@@ -1,15 +1,7 @@
1
1
  const BaseError = require('./validation/errors/base-error');
2
- const {isVerboseMode} = require('./utils');
2
+ const {isVerboseMode, multiline} = require('./utils');
3
3
  const {red, yellow, green, bgRed} = require('colorette');
4
4
 
5
- const emptyLine = length => new Array(length).fill(' ').join('');
6
- const multiline = (color, msg) => {
7
- msg = ` ${msg}`;
8
- msg = msg + emptyLine(120 - msg.length);
9
-
10
- return `\n${color(emptyLine(msg.length))}\n${color(msg)}\n${color(emptyLine(msg.length))}`;
11
- };
12
-
13
5
  class ErrorHandler {
14
6
  constructor() {
15
7
  this.failedThemes = [];
@@ -9,8 +9,6 @@ const EntryPointFileWriter = require('./writer/scss-entry-point-file-writer');
9
9
  const LayoutModulesConfigLoader = require('./modules-config/layout-modules-config-loader');
10
10
  const LayoutStyleLoader = require('./style/layout-style-loader');
11
11
  const MapModulesPlugin = require('./plugin/map/map-modules-plugin');
12
- const IntegrityFilePlugin = require('./plugin/integrity/integrity-file-plugin');
13
- const {SubresourceIntegrityPlugin} = require('webpack-subresource-integrity');
14
12
  const MiniCssExtractPlugin = require('mini-css-extract-plugin');
15
13
  const ModulesConfigLoader = require('./modules-config/modules-config-loader');
16
14
  const DynamicImportsFileWriter = require('./writer/dynamic-imports-file-writer');
@@ -21,6 +19,7 @@ const ThemeConfigFactory = require('./theme-config-factory');
21
19
  const path = require('path');
22
20
  const fs = require('fs');
23
21
  const prepareModulesMap = require('./plugin/map/prepare-modules-map');
22
+ const CssToJsonPlugin = require('./plugin/css-to-json');
24
23
  const resolve = require('enhanced-resolve');
25
24
  const {merge: webpackMerge} = require('webpack-merge');
26
25
  const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
@@ -32,7 +31,6 @@ const SVGSprite = require('./svg-sprite');
32
31
  const TerserPlugin = require('terser-webpack-plugin');
33
32
  require('resolve-url-loader');
34
33
  require('lezer-loader');
35
-
36
34
  class ConfigBuilder {
37
35
  constructor() {
38
36
  this._projectPath = '';
@@ -283,8 +281,7 @@ class ConfigBuilder {
283
281
  output: {
284
282
  filename: '[name].js',
285
283
  // Because we use third party libraries 'chunkFilename' should include only [name]
286
- chunkFilename: this._getVersionedPath('chunk/[name].js', this.assetVersion),
287
- crossOriginLoading: "anonymous"
284
+ chunkFilename: this._getVersionedPath('chunk/[name].js', this.assetVersion)
288
285
  },
289
286
  devtool: !env.skipSourcemap && 'inline-cheap-module-source-map',
290
287
  mode: 'development',
@@ -391,10 +388,6 @@ class ConfigBuilder {
391
388
  new webpack.optimize.MinChunkSizePlugin({
392
389
  minChunkSize: 30000 // Minimum number of characters
393
390
  }),
394
- new SubresourceIntegrityPlugin(),
395
- new IntegrityFilePlugin({
396
- publicPath: this.resolvedPublicPath
397
- }),
398
391
  new AfterWebpackLogsPlugin(
399
392
  stats => this.emitter.emit('build:complete', stats)
400
393
  )
@@ -541,6 +534,10 @@ class ConfigBuilder {
541
534
  }));
542
535
  }
543
536
 
537
+ if (!skipCSS) {
538
+ plugins.push(new CssToJsonPlugin());
539
+ }
540
+
544
541
  const cssEntryPoints = !skipCSS ? this._getCssEntryPoints(buildName, buildPublicPath) : {};
545
542
  const jsEntryPoints = !skipJS ? jsBuildConfig.entry : {};
546
543
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oroinc/oro-webpack-config-builder",
3
- "version": "6.1.0-lts04",
3
+ "version": "6.1.0-lts06",
4
4
  "author": "Oro, Inc. (https://oroinc.com)",
5
5
  "license": "MIT",
6
6
  "description": "An integration of OroPlatform based applications with the Webpack.",
@@ -16,9 +16,9 @@
16
16
  "css-loader": "^6.8.1",
17
17
  "css-minimizer-webpack-plugin": "~5.0.0",
18
18
  "deepmerge": "~4.3.1",
19
+ "esbuild-loader": "^4.3.0",
19
20
  "exports-loader": "~4.0.0",
20
21
  "expose-loader": "~4.1.0",
21
- "esbuild-loader": "^4.3.0",
22
22
  "file-loader": "~6.2.0",
23
23
  "html-webpack-plugin": "~5.5.0",
24
24
  "imports-loader": "~4.0.1",
@@ -30,6 +30,7 @@
30
30
  "path": "0.12.7",
31
31
  "postcss": "<8.4.33",
32
32
  "postcss-loader": "~7.3.4",
33
+ "postcss-safe-parser": "^7.0.1",
33
34
  "printf": "~0.6.0",
34
35
  "resolve-url-loader": "^5.0.0",
35
36
  "rtlcss-webpack-plugin": "~4.0.6",
@@ -49,7 +50,6 @@
49
50
  "webpack-cli": "~6.0.0",
50
51
  "webpack-dev-server": "^5.2.0",
51
52
  "webpack-merge": "~5.8.0",
52
- "webpack-subresource-integrity": "^5.2.0-rc.1",
53
53
  "wildcard": "~2.0.0"
54
54
  }
55
55
  }
@@ -0,0 +1,66 @@
1
+ const path = require('path');
2
+ const webpack = require('webpack');
3
+ const jsonTransform = require('./json-transformer');
4
+ const {multiline} = require('./../../utils');
5
+ const {bgRed} = require('colorette');
6
+
7
+ const isExportCssFile = filename => {
8
+ return path.extname(filename) === '.css' && /_json(\.rtl)?$/.test(path.basename(filename, '.css'));
9
+ };
10
+
11
+ class CssToJsonPlugin {
12
+ constructor(options) {}
13
+
14
+ processAssets = (compilation, callback) => {
15
+ const chunks = Array.from(compilation.chunks);
16
+
17
+ chunks.forEach(chunk => {
18
+ const files = Array.from(chunk.files);
19
+
20
+ files.filter(isExportCssFile).forEach(filename => {
21
+ const src = compilation.assets[filename].source();
22
+ const json = jsonTransform(src);
23
+
24
+ try {
25
+ JSON.parse(json);
26
+ } catch (e) {
27
+ console.error(
28
+ multiline(bgRed, `[ERROR] The JSON generated from the CSS file "${filename}" is invalid.`)
29
+ );
30
+ console.error(e.message);
31
+ return;
32
+ }
33
+
34
+ let jsonFileName = '[name].json';
35
+
36
+ if (filename.includes('rtl')) {
37
+ jsonFileName = '[name].rtl.json';
38
+ }
39
+
40
+ const dstFileName = compilation.getPath(jsonFileName, {
41
+ chunk,
42
+ cssFileName: filename
43
+ });
44
+
45
+ compilation.assets[dstFileName] = new webpack.sources.RawSource(json);
46
+ chunk.files.add(dstFileName);
47
+ });
48
+ });
49
+
50
+ callback();
51
+ };
52
+
53
+ apply(compiler) {
54
+ compiler.hooks.compilation.tap('CssToJsonPlugin', compilation => {
55
+ compilation.hooks.processAssets.tapAsync(
56
+ {
57
+ name: 'CssToJsonPlugin',
58
+ stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE
59
+ },
60
+ (chunks, callback) => this.processAssets(compilation, callback)
61
+ );
62
+ });
63
+ }
64
+ }
65
+
66
+ module.exports = CssToJsonPlugin;
@@ -0,0 +1,42 @@
1
+ const postcss = require('postcss');
2
+ const safeParser = require('postcss-safe-parser');
3
+
4
+ const normalizeSelector = selector => {
5
+ return selector
6
+ .replace(/^\./g, '')
7
+ .replace(/:(\w+)/, (matched, group) => {
8
+ return group.charAt(0).toUpperCase() + group.slice(1);
9
+ })
10
+ /*
11
+ * #main > .item => _main__item
12
+ * input[type="text"] => input_type_text
13
+ * .button + .icon => button__icon
14
+ * .p:has(.btn) => p_has__btn_
15
+ */
16
+ .replace(/[:\.\[\]\(\)#\s>+~]/g, '_');
17
+ };
18
+
19
+ const transform = src => {
20
+ const classMap = {};
21
+ const root = postcss.parse(src, {parser: safeParser});
22
+ root.walkRules(rule => {
23
+ const selector = rule.selector;
24
+
25
+ if (!selector.startsWith('.')) {
26
+ return;
27
+ }
28
+
29
+ const normalizedKey = normalizeSelector(selector);
30
+ const props = [];
31
+ rule.walkDecls(decl => {
32
+ const important = decl.important ? ' !important' : '';
33
+ props.push(`${decl.prop}: ${decl.value}${important}`);
34
+ });
35
+
36
+ classMap[normalizedKey] = props.join('; ') + (props.length ? ';' : '');
37
+ });
38
+
39
+ return JSON.stringify(classMap, null, 2);
40
+ };
41
+
42
+ module.exports = transform;
package/utils.js CHANGED
@@ -1,7 +1,7 @@
1
1
  let prodMode;
2
2
  let verboseMode;
3
3
 
4
- module.exports = {
4
+ const utils = {
5
5
  isVerboseMode() {
6
6
  if (verboseMode === void 0) {
7
7
  verboseMode = process.argv.filter(arg => {
@@ -13,6 +13,7 @@ module.exports = {
13
13
 
14
14
  return verboseMode;
15
15
  },
16
+
16
17
  isProdMode() {
17
18
  if (prodMode !== void 0) {
18
19
  return prodMode;
@@ -26,5 +27,18 @@ module.exports = {
26
27
  });
27
28
 
28
29
  return prodMode;
30
+ },
31
+
32
+ emptyLine(length) {
33
+ return new Array(length).fill(' ').join('');
34
+ },
35
+
36
+ multiline(color, msg) {
37
+ msg = ` ${msg}`;
38
+ msg = msg + utils.emptyLine(120 - msg.length);
39
+
40
+ return `\n${color(utils.emptyLine(msg.length))}\n${color(msg)}\n${color(utils.emptyLine(msg.length))}`;
29
41
  }
30
42
  };
43
+
44
+ module.exports = utils;
@@ -1,42 +0,0 @@
1
- const { createHash } = require('crypto');
2
- const { Compilation, sources } = require('webpack');
3
- const fs = require('fs').promises;
4
- const path = require('path');
5
-
6
- class IntegrityFilePlugin {
7
- constructor({fileName = 'integrity.json', publicPath = '', algorithm = 'sha384'} = {}) {
8
- this.fileName = fileName;
9
- this.publicPath = publicPath;
10
- this.algorithm = algorithm;
11
- }
12
-
13
- apply(compiler) {
14
- compiler.hooks.thisCompilation.tap('IntegrityFilePlugin', (compilation) => {
15
- compilation.hooks.processAssets.tapPromise(
16
- { name: 'SubresourceIntegrityPlugin', stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER },
17
- async () => {
18
- const integrityData = Object.fromEntries(
19
- Object.entries(compilation.assets)
20
- .filter(([assetName]) => assetName.endsWith('.css') || assetName.endsWith('.js'))
21
- .map(([assetName, asset]) => {
22
- const hash = createHash(this.algorithm).update(asset.source()).digest('base64');
23
- const assetNormalized = assetName.split('?')[0];
24
-
25
- return [
26
- path.join(compiler.options.output.publicPath || '', assetNormalized),
27
- `${this.algorithm}-${hash}`
28
- ];
29
- })
30
- );
31
- const jsonData = JSON.stringify(integrityData, null, 2);
32
- const outputPath = path.join(this.publicPath, compiler.options.output.publicPath, this.fileName);
33
-
34
- await fs.writeFile(outputPath, jsonData, 'utf8');
35
- compilation.emitAsset(outputPath, new sources.RawSource(jsonData));
36
- }
37
- );
38
- });
39
- }
40
- }
41
-
42
- module.exports = IntegrityFilePlugin;