-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
128 lines (120 loc) · 3.93 KB
/
config-overrides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const path = require('path')
const util = require('util')
const inspect = (data) => {
throw new Error(util.inspect(data, { depth: Infinity }))
}
const getWorkspacesPaths = () => {
const package = require(path.resolve('package.json'))
const workspaces = package.workspaces
const workspacesList = []
// Normally "workspaces" in package.json is an array
if (Array.isArray(workspaces)) {
workspacesList.push(...workspaces)
}
// Sometimes "workspaces" in package.json is an object
// with a ".packages" sub-array, eg: when used with "nohoist"
// See: https://yarnpkg.com/blog/2018/02/15/nohoist
if (workspaces && !Array.isArray(workspaces) && Reflect.has(workspaces, 'packages')) {
workspacesList.push(...workspaces.packages)
}
return workspacesList.map((workspace) => path.resolve(workspace))
}
const rewireAbsolutePath = (config, env) => {
config.resolve.modules = [...config.resolve.modules, path.resolve('.')]
}
const rewireMainFields = (config, env) => {
const mainFields = ['main:src', 'browser', 'main']
config.resolve = { ...config.resolve, mainFields }
}
const rewirePreRule = (getValues) => {
return (config, env) => {
const testSource = /\.(js|mjs|jsx|ts|tsx)$/.source
const rule = config.module.rules.find(
(element) => element.test && element.test.source === testSource,
)
const values = getValues(rule)
Object.entries(values).forEach(([key, value]) => (rule[key] = value))
}
}
const rewireAppRule = (getValues) => {
return (config, env) => {
const oneOf = config.module.rules.find((element) => element.oneOf).oneOf
const testSource = /\.(js|mjs|jsx|ts|tsx)$/.source
const rule = oneOf.find((element) => element.test.source === testSource)
const values = getValues(rule)
Object.entries(values).forEach(([key, value]) => (rule[key] = value))
}
}
const rewireLess = (options = {}) => {
return (config, env) => {
const oneOf = config.module.rules.find((element) => element.oneOf).oneOf
const lessExtension = /\.less$/
const fileLoader = oneOf.find(
(element) => element.loader && element.loader.includes('file-loader'),
)
fileLoader.exclude.push(lessExtension)
const testSource = /\.css$/.source
const cssRules = oneOf.find((element) => element.test.source === testSource)
const lessRules = {
test: lessExtension,
use: [...cssRules.use, { loader: 'less-loader', options }],
}
oneOf.unshift(lessRules)
}
}
module.exports = (config, env) => {
const workspacesPaths = getWorkspacesPaths()
rewireAbsolutePath(config, env)
rewireMainFields(config, env)
rewirePreRule(({ include }) => {
return {
include: [include, ...workspacesPaths],
}
})(config, env)
rewireAppRule(({ options, include }) => {
const plugins = [...options.plugins]
// plugins.push([
// require.resolve('reshadow/babel'),
// {
// postcss: true,
// },
// ])
plugins.push([require.resolve('babel-plugin-idx')])
plugins.push([
require.resolve('babel-plugin-import'),
{
libraryName: 'antd',
style: true,
},
'import-antd',
])
// TODO: почему-то мало профита от использования (429 B)?
plugins.push([
require.resolve('babel-plugin-import'),
{
libraryName: 'lodash',
libraryDirectory: '',
camel2DashComponentName: false, // default: true
},
'import-lodash',
])
if (env === 'development') {
plugins.push([require.resolve('babel-plugin-styled-components')])
}
return {
options: {
...options,
plugins,
},
include: [include, ...workspacesPaths],
}
})(config, env)
const requireForES = require('esm')(module /*, options*/)
const antdVars = requireForES(path.resolve('src/theme.js')).antdVars
rewireLess({
modifyVars: antdVars,
javascriptEnabled: true,
sourceMap: env === 'production',
})(config, env)
return config
}