-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcraco.config.js
145 lines (135 loc) · 4.42 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
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const rewireEntries = [
{
name: 'popup',
entry: path.resolve(__dirname, 'src', 'popup', 'index.tsx'),
template: path.resolve(__dirname, 'public', 'index.html'),
outPath: 'popup.html',
},
{
name: 'options',
entry: path.resolve(__dirname, 'src', 'options', 'index.tsx'),
template: path.resolve(__dirname, 'public', 'index.html'),
outPath: 'options.html',
},
{
name: 'background',
entry: path.resolve(__dirname, 'src', 'background', 'index.ts'),
template: path.resolve(__dirname, 'public', 'index.html'),
outPath: 'ignore-this/background.html',
},
{
name: 'content-scripts',
entry: path.resolve(__dirname, 'src', 'content-scripts', 'index.ts'),
template: path.resolve(__dirname, 'public', 'index.html'),
outPath: 'ignore-this/content-scripts.html',
},
{
name: 'inject-script',
entry: path.resolve(__dirname, 'src', 'inject-script', 'index.ts'),
template: path.resolve(__dirname, 'public', 'index.html'),
outPath: 'ignore-this/inject-script.html',
},
];
const defaultEntryName = 'main';
const appIndexes = ['js', 'tsx', 'ts', 'jsx'].map((ext) =>
path.resolve(__dirname, `src/index.${ext}`)
);
function webpackMultipleEntries(config) {
// Multiple Entry JS
config.plugins.forEach((plugin) => {
if (plugin.constructor.name === 'MiniCssExtractPlugin') {
plugin.options.filename = 'static/css/[name].css';
}
});
const defaultEntryHTMLPlugin = config.plugins.filter((plugin) => {
return plugin.constructor.name === 'HtmlWebpackPlugin';
})[0];
defaultEntryHTMLPlugin.userOptions.chunks = [defaultEntryName];
// config.entry is not an array in Create React App 4
if (!Array.isArray(config.entry)) {
config.entry = [config.entry];
}
// If there is only one entry file then it should not be necessary for the rest of the entries
const necessaryEntry =
config.entry.length === 1
? []
: config.entry.filter((file) => !appIndexes.includes(file));
const multipleEntry = {};
multipleEntry[defaultEntryName] = config.entry;
rewireEntries.forEach((entry) => {
multipleEntry[entry.name] = necessaryEntry.concat(entry.entry);
// Multiple Entry HTML Plugin
config.plugins.unshift(
new defaultEntryHTMLPlugin.constructor(
Object.assign({}, defaultEntryHTMLPlugin.userOptions, {
filename: entry.outPath,
template: entry.template,
chunks: [entry.name],
})
)
);
});
config.entry = multipleEntry;
// Multiple Entry Output File
let names = config.output.filename.split('/').reverse();
if (names[0].indexOf('[name]') === -1) {
names[0] = '[name].' + names[0];
config.output.filename = names.reverse().join('/');
}
return config;
}
module.exports = {
webpack: {
alias: {
'@app': path.resolve(__dirname, 'src'),
'@assets': path.resolve(__dirname, 'src', 'assets'),
'@components': path.resolve(__dirname, 'src', 'components'),
'@constants': path.resolve(__dirname, 'src', 'constants'),
'@hooks': path.resolve(__dirname, 'src', 'hooks'),
'@services': path.resolve(__dirname, 'src', 'services'),
'@types': path.resolve(__dirname, 'src', 'types'),
'@utils': path.resolve(__dirname, 'src', 'utils'),
'@background': path.resolve(__dirname, 'src', 'background'),
'@content-scripts': path.resolve(__dirname, 'src', 'content-scripts'),
'@inject-script': path.resolve(__dirname, 'src', 'inject-script'),
'@options': path.resolve(__dirname, 'src', 'options'),
'@popup': path.resolve(__dirname, 'src', 'popup'),
},
plugins: {
add: [
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'src', 'assets'),
to: path.resolve(__dirname, 'build', 'assets'),
},
],
}),
],
},
configure: (webpackConfig, { env, paths }) => {
const newWebpackConfig = webpackMultipleEntries(webpackConfig);
return {
...newWebpackConfig,
entry: {
...newWebpackConfig.entry,
},
output: {
...newWebpackConfig.output,
filename: 'static/js/[name].js',
},
optimization: {
...newWebpackConfig.optimization,
runtimeChunk: false,
},
};
},
},
devServer: {
devMiddleware: {
writeToDisk: true,
},
},
};