forked from chronic-care/mycareplanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.rescriptsrc.js
144 lines (135 loc) · 5.29 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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. In the case of the Pain Management
// Summary, we must modify the WebpackConfig to setup the launch.html entry point and to
// add some aliases to replace large unnecessary files with smaller stubs.
//
// See: https://github.com/harrysolovay/rescripts
// Adds the launch.js entry.
// In production, this transforms:
// entry: ["/path/to/pain-management-summary/src/index.js"]
// to
// entry: {
// main: ["/path/to/pain-management-summary/src/index.js"],
// launch: ["/path/to/pain-management-summary/src/index.js"]
// }
// 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(`${path.sep}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.map(e => e.replace(/[\/\\]index.js$/, `${path.sep}launch.js`))
}
},
[['entry']],
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,
);
}
// 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
)
}
// Stubs out files that are not needed but take up lots of space in webpacked source. This includes:
// ./fhir/models (from cql-execution)
// ./modelInfos/fhir-modelinfo-1.6.xml.js (from cql-exec-fhir)
// ./modelInfos/fhir-modelinfo-3.0.0.xml.js (from cql-exec-fhir)
// ./modelInfos/fhir-modelinfo-4.0.0.xml.js (from cql-exec-fhir)
const stubUnneededFiles = config => {
return edit(
resolve => {
// currently, resolve.alias exists (for react-native), but play it safe in case this
// changes in the future
if (resolve.alias == null) {
resolve.alias = {};
}
// Replace cql-execution's packaged FHIR model w/ a stubbed version since we
// don't use the FHIR model from cql-execution and it takes up a lot of space.
resolve.alias['./fhir/models'] = path.resolve(__dirname, './src/stubs/fhir-models.js');
// Replace cql-exec-fhir's bundled 1.6, and 3.0.0 modelinfos with stubs since we
// only use the 1.0.2 and 4.0.0 modelinfos and the others take up a lot of space.
resolve.alias['./modelInfos/fhir-modelinfo-1.6.xml.js'] = path.resolve(__dirname, './src/stubs/fhir-modelinfo-stub.xml.js');
resolve.alias['./modelInfos/fhir-modelinfo-3.0.0.xml.js'] = path.resolve(__dirname, './src/stubs/fhir-modelinfo-stub.xml.js');
return resolve;
},
[['resolve']],
config
);
}
// Logs the config, mainly useful for debugging
const logConfig = config => {
console.log(JSON.stringify(config, null, 2))
return config
}
// uncomment below to log config between each rescript
// logConfig.isMiddleware = true;
// The module.exports determines which scripts actually get run!
module.exports = [
addLaunch,
changeOutputFilenameForDev,
editChunksInHtmlWebpackPluginForIndex,
addHtmlWebpackPluginForLaunch
// ,stubUnneededFiles
// ,logConfig
];