This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
forked from uwcirg/helloworld-react-client-sof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.rescriptsrc.js
121 lines (111 loc) · 3.81 KB
/
.rescriptsrc.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
const {edit, editWebpackPlugin, getWebpackPlugin, appendWebpackPlugin} = require('@rescripts/utilities');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
// This is a root rescript file. Together with the rescripts CLI, it allows the default
// Create React App (CRA) configurations to be modified.
// See: https://github.com/harrysolovay/rescripts
// Add support for worker loader
const addWorkerLoader = config => {
return edit(
module => {
if (!module.rules) module.rules = [];
module.rules.push({
test: /\.worker\.js$/,
use: { loader: "worker-loader" },
});
return module;
},
[['module']],
config
);
};
// In development, it is similar, but each array also contains a Webpack hotloader.
const addLaunch = config => {
return edit(
entries => {
// if (!Array.isArray(entries) || entries.filter(e => e.endsWith('/index.js')).length !== 1) {
// console.error('Cannot add launch.js to entry. Unexpected starting value for entry:', entries);
// return entries;
// }
return {
main: entries,
launch: entries.replace(/\/index.js$/, '/launch.js')
}
},
[['entry']],
config
);
}
// Adds a new HtmlWebpackPlugin for launch.html. It is configured the same as the index.html one,
// except the filename is "launch.html" and the chunks are ["launch"].
const addHtmlWebpackPluginForLaunch = config => {
const indexPlugin = getWebpackPlugin('HtmlWebpackPlugin', config);
if (indexPlugin == null || indexPlugin.options.filename !== 'index.html') {
console.error('Cannot find HtmlWebpackPlugin for index.html to use as baseline for launch plugin.');
return config;
}
const launchOptions = Object.assign({}, indexPlugin.options);
launchOptions.filename = 'launch.html';
launchOptions.chunks = ['launch'];
return appendWebpackPlugin(
new HtmlWebpackPlugin(launchOptions),
config
)
}
// Changes the output filename in development (not necessary in production).
// In development, this transforms:
// output.filename: "static/js/bundle.js"
// to
// output.filename: "static/js/[name].js"
const changeOutputFilenameForDev = config => {
if (config.mode !== 'development') {
return config;
}
return edit(
filename => {
if (!filename.endsWith('/bundle.js')) {
console.error('Cannot modify output filename. Unexpected starting value:', filename);
return filename;
}
return filename.replace(/\/bundle.js$/, '/[name].js');
},
[['output', 'filename']],
config
)
}
// Changes the existing HtmlWebpackPlugin for index.html to specify that it should use the main chunk.
// This production and development this transforms HtmlWebpackPlugin's:
// options.chunks: "all"
// to
// options.chunks: ["main"]
const editChunksInHtmlWebpackPluginForIndex = config => {
return editWebpackPlugin(
p => {
if (p.options.filename !== 'index.html') {
console.error('Cannot modify HtmlWebpackPlugin. Unexpected filename:', p.options.filename);
return p;
} else if (p.options.chunks !== 'all') {
console.error('Cannot modify HtmlWebpackPlugin. Unexpected chunks:', p.options.chunks);
return p;
}
p.options.chunks = ['main'];
return p
},
'HtmlWebpackPlugin',
config,
);
}
// Logs the config, mainly useful for debugging
const logConfig = config => {
console.log(JSON.stringify(config, null, 2))
return config
}
// The module.exports determines which scripts actually get run!
module.exports = [
addWorkerLoader,
addLaunch,
addHtmlWebpackPluginForLaunch,
changeOutputFilenameForDev,
editChunksInHtmlWebpackPluginForIndex,
//logConfig
];