generated from little-buddy/craco-react-webapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cracoplugin.js
167 lines (152 loc) · 3.58 KB
/
cracoplugin.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
/* eslint-disable no-param-reassign */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const { addPlugins, removePlugins } = require('@craco/craco');
const PreloadWebapckPlugin = require('@vue/preload-webpack-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const addPath = dir => path.resolve(__dirname, dir);
const isEnvProduction = process.env.NODE_ENV === 'production';
const isAnalyze = process.env.ANALYZE;
const getBabelConf = () => ({
// react 默认配置了
// loaderOptions: {
// cacheDirectory: true,
// },
plugins: [
[
'import',
{
libraryName: 'lodash',
libraryDirectory: '',
camel2DashComponentName: false,
},
],
],
});
const preloadAsyncChunks = config => {
addPlugins(
config,
[
new PreloadWebapckPlugin({
rel: 'preload',
include: 'initial',
fileBlacklist: [/\.map$/, /hot-update\.js$/],
}),
'append',
],
// prefetch
[
new PreloadWebapckPlugin({
rel: 'prefetch',
include: 'asyncChunks',
}),
'append',
]
);
};
const refactorEntry = config => {
// eslint-disable-next-line no-param-reassign
config.entry = {
app: config.entry,
};
addPlugins(config, [
[
new HtmlWebpackPlugin({
inject: 'body',
template: addPath('./public/index.html'),
// external的js-cdn 可以配置在这里
cdn: {
js: [],
css: [],
},
...(isEnvProduction
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}
: undefined),
}),
'prepend',
],
new WebpackManifestPlugin({
fileName: 'asset-manifest.json',
// publicPath: paths.publicUrlOrPath,
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
// eslint-disable-next-line no-param-reassign
manifest[file.name] = file.path;
return manifest;
}, seed);
const entrypointFiles = entrypoints.app.filter(
fileName => !fileName.endsWith('.map')
);
return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
]);
removePlugins(config, (value, index, array) => {
const pluginName = Object.getPrototypeOf(array[index]).constructor.name;
if (pluginName === 'HtmlWebpackPlugin' && index !== 0) {
return true;
}
if (['WebpackManifestPlugin'].indexOf(pluginName) > -1) {
const plugin = array[index];
if (!(plugin instanceof WebpackManifestPlugin)) {
return true;
}
}
return false;
});
};
// Override webpack optimization
// See https://github.com/dilanx/craco/issues/44
const splitChunks = config => {
config.optimization.splitChunks = {
cacheGroups: {
reactLib: {
test: /[\\/]node_modules[\\/](react|react-dom|react-router-dom|@reduxjs\/toolkit\/dist)[\\/]/,
name: 'react-lib',
chunks: 'all',
enforce: true,
priority: 40,
reuseExistingChunk: true,
},
vendors: {
name: `chunk-vendors`,
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'initial',
},
common: {
name: `chunk-common`,
minChunks: 2,
priority: -20,
chunks: 'initial',
reuseExistingChunk: true,
},
},
};
};
module.exports = {
addPath,
isEnvProduction,
getBabelConf,
preloadAsyncChunks,
splitChunks,
refactorEntry,
isAnalyze,
};