-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcraco.config.js
93 lines (82 loc) · 2.99 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
const path = require('path');
const rewireEntries = [
{
name: 'options-page',
entry: path.resolve(__dirname, './src/options-page/options_page.tsx'),
template: path.resolve(__dirname, './public/options_page.html'),
outPath: 'options_page.html',
},
{
name: 'context-page',
entry: path.resolve(__dirname, './src/context-page/context-page.tsx'),
template: path.resolve(__dirname, './public/context-page.html'),
outPath: 'context-page.html',
}
];
const defaultEntryName = 'main';
const appIndexes = ['js', 'tsx', 'ts', 'jsx'].map((ext) =>
path.resolve(__dirname, `src/index.${ext}`)
);
// Credit to this very helpful Github response for this code: https://github.com/dilanx/craco/issues/298#issuecomment-1046181546
function webpackMultipleEntries(config) {
// Multiple Entry JS
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: {
configure: (webpackConfig, {env, paths}) => {
let config = webpackMultipleEntries(webpackConfig);
return {
...config,
entry: {
...config.entry,
worker: './src/scripts/parse_worker.ts',
content: './src/scripts/scrape_content_script.ts',
},
output: {
...config.output,
filename: '[name].js',
},
optimization: {
...config.optimization,
runtimeChunk: false,
},
}
}
},
};