|
| 1 | +const webpack = require('webpack') |
1 | 2 | const ThemeColorReplacer = require('webpack-theme-color-replacer') |
2 | 3 | const generate = require('@ant-design/colors/lib/generate').default |
| 4 | +const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin') |
3 | 5 |
|
4 | | -const getAntdSerials = (color) => { |
| 6 | +const webpackPlugins = [ |
| 7 | + // Ignore all locale files of moment.js |
| 8 | + new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), |
| 9 | + new MonacoWebpackPlugin({ |
| 10 | + languages: ['python', 'shell', 'powershell', 'json', 'bat'] |
| 11 | + }) |
| 12 | +] |
| 13 | + |
| 14 | +function isMonacoAsset(assetName, asset) { |
| 15 | + if (!/\.js$/i.test(assetName)) { |
| 16 | + return false |
| 17 | + } |
| 18 | + |
| 19 | + if (/(^|[\\/])(editor|json|ts)\.worker\.js$/i.test(assetName)) { |
| 20 | + return true |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + const source = asset?.source?.()?.toString?.() || '' |
| 25 | + return source.includes('MonacoEnvironment') || source.includes('editorSimpleWorker') |
| 26 | + } catch (error) { |
| 27 | + return false |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function getAntdSerials(color) { |
5 | 32 | // Lighten (similar to less's tint) |
6 | 33 | const lightens = new Array(9).fill().map((t, i) => { |
7 | 34 | return ThemeColorReplacer.varyColor.lighten(color, i / 10) |
8 | 35 | }) |
9 | 36 | const colorPalettes = generate(color) |
10 | | - const rgb = ThemeColorReplacer.varyColor.toNum3(color.replace('#', '')).join(',') |
11 | | - return lightens.concat(colorPalettes).concat(rgb) |
| 37 | + return lightens.concat(colorPalettes) |
12 | 38 | } |
13 | 39 |
|
14 | | -const themePluginOption = { |
15 | | - fileName: 'css/theme-colors-[contenthash:8].css', |
16 | | - matchColors: getAntdSerials('#2f54eb'), // primary color series |
17 | | - // change style selectors to solve style override issues |
18 | | - changeSelector (selector) { |
19 | | - switch (selector) { |
20 | | - case '.ant-calendar-today .ant-calendar-date': |
21 | | - return ':not(.ant-calendar-selected-date):not(.ant-calendar-selected-day)' + selector |
22 | | - case '.ant-btn:focus,.ant-btn:hover': |
23 | | - return '.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger),.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger)' |
24 | | - case '.ant-btn.active,.ant-btn:active': |
25 | | - return '.ant-btn.active:not(.ant-btn-primary):not(.ant-btn-danger),.ant-btn:active:not(.ant-btn-primary):not(.ant-btn-danger)' |
26 | | - case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon': |
27 | | - case '.ant-steps-item-process .ant-steps-item-icon>.ant-steps-icon': |
28 | | - return ':not(.ant-steps-item-process)' + selector |
29 | | - case '.ant-menu-horizontal>.ant-menu-item-active,.ant-menu-horizontal>.ant-menu-item-open,.ant-menu-horizontal>.ant-menu-item-selected,.ant-menu-horizontal>.ant-menu-item:hover,.ant-menu-horizontal>.ant-menu-submenu-active,.ant-menu-horizontal>.ant-menu-submenu-open,.ant-menu-horizontal>.ant-menu-submenu-selected,.ant-menu-horizontal>.ant-menu-submenu:hover': |
30 | | - case '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal > .ant-menu-submenu-selected,.ant-menu-horizontal > .ant-menu-submenu:hover': |
31 | | - return '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu:hover' |
32 | | - case '.ant-menu-horizontal > .ant-menu-item-selected > a': |
33 | | - case '.ant-menu-horizontal>.ant-menu-item-selected>a': |
34 | | - return '.ant-menu-horizontal:not(ant-menu-light):not(.ant-menu-dark) > .ant-menu-item-selected > a' |
35 | | - case '.ant-menu-horizontal > .ant-menu-item > a:hover': |
36 | | - case '.ant-menu-horizontal>.ant-menu-item>a:hover': |
37 | | - return '.ant-menu-horizontal:not(ant-menu-light):not(.ant-menu-dark) > .ant-menu-item > a:hover' |
38 | | - default : |
39 | | - return selector |
| 40 | +function createThemeColorReplacer() { |
| 41 | + // generate theme color replacement styles |
| 42 | + const plugin = new ThemeColorReplacer({ |
| 43 | + fileName: 'css/theme-colors-[contenthash:8].css', |
| 44 | + matchColors: getAntdSerials('#2f54eb'), // primary color series |
| 45 | + // change style selectors to solve style override issues |
| 46 | + changeSelector(selector) { |
| 47 | + switch (selector) { |
| 48 | + case '.ant-calendar-today .ant-calendar-date': |
| 49 | + return ':not(.ant-calendar-selected-date):not(.ant-calendar-selected-day)' + selector |
| 50 | + case '.ant-btn:focus,.ant-btn:hover': |
| 51 | + return '.ant-btn:focus:not(.ant-btn-primary),.ant-btn:hover:not(.ant-btn-primary)' |
| 52 | + case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon': |
| 53 | + return ':not(.ant-steps-item-process)' + selector |
| 54 | + case '.ant-btn.active,.ant-btn:active': |
| 55 | + return '.ant-btn.active:not(.ant-btn-primary),.ant-btn:active:not(.ant-btn-primary)' |
| 56 | + case '.ant-menu-horizontal>.ant-menu-item-active,.ant-menu-horizontal>.ant-menu-item-open,.ant-menu-horizontal>.ant-menu-item-selected,.ant-menu-horizontal>.ant-menu-item:hover,.ant-menu-horizontal>.ant-menu-submenu-active,.ant-menu-horizontal>.ant-menu-submenu-open,.ant-menu-horizontal>.ant-menu-submenu-selected,.ant-menu-horizontal>.ant-menu-submenu:hover': |
| 57 | + case '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal > .ant-menu-submenu-selected,.ant-menu-horizontal > .ant-menu-submenu:hover': |
| 58 | + return '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu:hover' |
| 59 | + case '.ant-menu-horizontal > .ant-menu-item-selected > a': |
| 60 | + return ':not(.ant-menu-horizontal)' + selector |
| 61 | + case '.ant-menu-horizontal > .ant-menu-item > a:hover': |
| 62 | + return ':not(.ant-menu-horizontal)' + selector |
| 63 | + default: |
| 64 | + return selector |
| 65 | + } |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + const assetsExtractor = plugin.handler?.assetsExtractor |
| 70 | + const originalExtractAsset = assetsExtractor.extractAsset.bind(assetsExtractor) |
| 71 | + assetsExtractor.extractAsset = function(assetName, asset) { |
| 72 | + // Monaco generates a large runtime/worker bundle; this plugin scans all JavaScript files as potential CSS containers. |
| 73 | + // Skip Monaco-related artifacts to avoid stack overflow caused by excessive regular expression/recursive parsing during the build process. |
| 74 | + if (isMonacoAsset(assetName, asset)) { |
| 75 | + return [] |
40 | 76 | } |
| 77 | + |
| 78 | + return originalExtractAsset(assetName, asset) |
41 | 79 | } |
| 80 | + |
| 81 | + return plugin |
42 | 82 | } |
43 | 83 |
|
44 | | -const createThemeColorReplacerPlugin = () => new ThemeColorReplacer(themePluginOption) |
| 84 | +webpackPlugins.push( |
| 85 | + createThemeColorReplacer() |
| 86 | +) |
45 | 87 |
|
46 | | -module.exports = createThemeColorReplacerPlugin |
| 88 | +module.exports = { |
| 89 | + webpackPlugins |
| 90 | +} |
0 commit comments