forked from sxfad/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
craco.config.js
201 lines (184 loc) · 7.62 KB
/
craco.config.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// create-react-app 配置修改
// https://github.com/gsoft-inc/craco
const path = require('path');
const ROOT_PATH = process.cwd();
const req = name => require(path.join(ROOT_PATH, 'node_modules', name));
const {BundleAnalyzerPlugin} = req('webpack-bundle-analyzer');
const WebpackBar = req('webpackbar');
const CracoAntDesignPlugin = req('craco-antd');
const CracoLessPlugin = req('craco-less'); // include in craco-antd
const MiniCssExtractPlugin = req('mini-css-extract-plugin');
const generator = req('@ra-lib/generator');
const packageName = require(path.join(ROOT_PATH, 'package.json')).name;
const SRC_PATH = path.join(ROOT_PATH, 'src');
const NODE_MODULES_PATH = path.join(ROOT_PATH, 'node_modules');
const PAGES_PATH = path.join(SRC_PATH, 'pages');
const BUILD_PATH = path.join(ROOT_PATH, process.env.BUILD_PATH || 'build');
const CSS_MODULE_LOCAL_IDENT_NAME = '[local]_[hash:base64:5]';
// Don't open the browser during development
// process.env.BROWSER = 'none';
module.exports = {
plugins: [
{
plugin: CracoAntDesignPlugin,
options: {
customizeThemeLessPath: path.join(SRC_PATH, 'theme.less'),
babelPluginImportOptions: {
libraryDirectory: 'es',
},
lessLoaderOptions: {
lessOptions: {
modifyVars: {
packageName,
},
},
},
modifyLessRule: function(lessRule, _context) {
// src 中less交给 CracoLessPlugin 处理
lessRule.exclude = SRC_PATH;
return lessRule;
},
},
},
{
plugin: CracoLessPlugin,
options: {
modifyLessRule: function(lessRule, _context) {
lessRule.test = /\.(less)$/;
lessRule.use = [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: {localIdentName: CSS_MODULE_LOCAL_IDENT_NAME},
},
},
{
loader: 'less-loader',
options: {
lessOptions: {
modifyVars: {
packageName,
},
javascriptEnabled: true,
},
},
},
];
lessRule.exclude = /node_modules/;
return lessRule;
},
},
},
],
devServer: (devServerConfig, {env, paths, proxy, allowedHost}) => {
if (!devServerConfig.headers) devServerConfig.headers = {};
devServerConfig.headers['Access-Control-Allow-Origin'] = '*';
// 不能使用before,会使proxy失效
devServerConfig.setup = function(app) {
generator(app);
};
return devServerConfig;
},
webpack: {
alias: {
// 使所有的react 都访问主应用安装的包
react: path.join(NODE_MODULES_PATH, 'react'),
antd: path.join(NODE_MODULES_PATH, 'antd'),
moment: path.join(NODE_MODULES_PATH, 'moment'),
'react-dom': path.join(NODE_MODULES_PATH, 'react-dom'),
'react-redux': path.join(NODE_MODULES_PATH, 'react-redux'),
'redux': path.join(NODE_MODULES_PATH, 'redux'),
'react-router-dom': path.join(NODE_MODULES_PATH, 'react-router-dom'),
'axios': path.join(NODE_MODULES_PATH, 'axios'),
root: ROOT_PATH,
src: SRC_PATH,
},
plugins: [
new WebpackBar({profile: true}),
...(process.env.ANALYZER === 'true' ?
[
new BundleAnalyzerPlugin({openAnalyzer: false}),
]
: []),
],
configure: (webpackConfig, {env, paths}) => {
paths.appBuild = webpackConfig.output.path = BUILD_PATH;
webpackConfig.output.library = packageName;
webpackConfig.output.libraryTarget = 'umd';
webpackConfig.output.jsonpFunction = `webpackJsonp_${packageName}`;
webpackConfig.module.rules.push({
test: path.join(PAGES_PATH, 'page-configs.js'),
enforce: 'pre',
use: {
loader: require.resolve('@ra-lib/config-loader'),
options: {
pagesPath: PAGES_PATH,
},
},
include: SRC_PATH,
});
if (process.env.ANALYZER_TIME === 'true') {
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const smp = new SpeedMeasurePlugin();
return smp.wrap(webpackConfig);
}
// mini-css-extract-plugin 对css引入的顺序会有提示,如果我们并不依赖于css文件顺序,这个可以关闭
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/250#issuecomment-415345126
const instanceOfMiniCssExtractPlugin = webpackConfig.plugins.find((plugin) => plugin instanceof MiniCssExtractPlugin);
if (instanceOfMiniCssExtractPlugin) instanceOfMiniCssExtractPlugin.options.ignoreOrder = true;
return webpackConfig;
},
},
babel: {
plugins: [
['@ra-lib/babel-plugin-attribute-wrapper',
{ // className={xxx} 转 className={_method(xxx)}
packageName: 'classnames',
attributeName: 'className',
},
'className',
],
['@ra-lib/babel-plugin-attribute-wrapper',
{ // 三元运算_method(xxx) ? <div> : null
importName: 'hasPermission', // import {hasPermission} from packageName
packageName: '@ra-lib/admin',
attributeName: 'r-code',
conditional: true,
},
'permission-remove',
],
['@ra-lib/babel-plugin-attribute-wrapper',
{ // 添加disabled={!_method(xxx)}属性
importName: 'hasPermission',
packageName: '@ra-lib/admin',
attributeName: 'd-code',
replaceAttributeName: 'disabled',
negation: true,
},
'permission-disabled',
],
// ['@ra-lib/babel-plugin-attribute-wrapper',
// { // r-report="用户保存" onClick={this.handleClick} => onClick={_method("用户保存", this.handleClick)}
// importName: 'report',
// packageName: 'src/commons',
// attributeName: 'r-report',
// wrapperAttributedName: 'onClick',
// },
// 'wrapper',
// ],
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
[
'@babel/plugin-proposal-decorators',
{
'legacy': true,
},
],
'@babel/plugin-syntax-jsx',
],
},
};