-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcraco.config.js
111 lines (100 loc) · 4.11 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
/* eslint-disable no-param-reassign */
/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const packageJson = require('./package.json');
const protocol = packageJson.protocol;
const BASE_URL = path.join(process.env.REPOSITORY_NAME || '', protocol);
process.env.REACT_APP_REPOSITORY_NAME = process.env.REPOSITORY_NAME || '';
if (process.env.NODE_ENV === 'production') {
process.env.PUBLIC_URL = BASE_URL;
}
const { BundleStatsWebpackPlugin } = require('bundle-stats-webpack-plugin');
const CopyPluginWebpack = require('copy-webpack-plugin');
const CreateFileWebpack = require('create-file-webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// https://github.com/Cap32/html-webpack-banner-plugin/blob/3b5f3f1adf1240b1f744aaa78eabefae50fa53b7/index.js
class HtmlWebpackBannerPlugin {
constructor(options) {
this.options = options || {};
}
apply(compiler) {
const banner = this.options.banner || undefined;
compiler.hooks.compilation.tap('HtmlWebpackBannerPlugin', (compilation) => {
const hooks = HtmlWebpackPlugin.getHooks(compilation);
hooks.beforeEmit.tap('HtmlWebpackBannerPlugin', (htmlPluginData) => {
htmlPluginData.html = `${htmlPluginData.html}${banner ? `<!--\n${banner}\n-->` : ''}`;
return htmlPluginData;
});
});
}
}
// https://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=Fuel%20Labs
const banner = `
███████╗██╗ ██╗███████╗██╗ ██╗ █████╗ ██████╗ ███████╗
██╔════╝██║ ██║██╔════╝██║ ██║ ██╔══██╗██╔══██╗██╔════╝
█████╗ ██║ ██║█████╗ ██║ ██║ ███████║██████╔╝███████╗
██╔══╝ ██║ ██║██╔══╝ ██║ ██║ ██╔══██║██╔══██╗╚════██║
██║ ╚██████╔╝███████╗███████╗ ███████╗██║ ██║██████╔╝███████║
╚═╝ ╚═════╝ ╚══════╝╚══════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ ╚══════╝
${packageJson.name} v${packageJson.version}
`;
module.exports = () => {
const isProductionBuild = process.env.NODE_ENV === 'production';
const analyze = process.env.REACT_APP_ANALYZE === 'true' || false;
const plugins = [
new HtmlWebpackBannerPlugin({
banner,
}),
new CreateFileWebpack({
// path to folder in which the file will be created
path: `./build/${packageJson.protocol}`,
// file name
fileName: 'versions',
// content of the file
content: [
`BE_VERSION="${packageJson.version}"`,
`BE_PROTOCOL="${packageJson.protocol}"`,
].join('\n'),
}),
new CopyPluginWebpack({
patterns: [
{
from: './public/404.html',
to: '../',
transform(content) {
return content
.toString()
.replace('%REPOSITORY_NAME%', process.env.REPOSITORY_NAME || '')
.replace('%PROTOCOL_NAME%', packageJson.defaultProtocol);
},
},
{
from: './public/protocol-versions.json',
to: '../',
},
],
}),
];
if (isProductionBuild && analyze) {
plugins.push(
new BundleStatsWebpackPlugin({
compare: false,
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
})
);
}
return {
webpack: {
configure: (webpackConfig, { paths }) => {
paths.appBuild = path.join(paths.appBuild, packageJson.protocol);
webpackConfig.output.path = paths.appBuild;
return webpackConfig;
},
plugins,
},
};
};