-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.rescriptsrc.js
203 lines (187 loc) · 4.93 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const fs = require("fs");
const path = require("path");
const _get = require("lodash.get");
const _set = require("lodash.set");
const {
editWebpackPlugin,
getWebpackPlugin,
appendWebpackPlugin,
getPaths
} = require("@rescripts/utilities");
const webpack = require("webpack");
const RemovePlugin = require("remove-files-webpack-plugin");
const HtmlWebpackDeployPlugin = require("html-webpack-deploy-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { default: InjectPlugin, ENTRY_ORDER } = require("webpack-inject-plugin");
const { alias } = require("./shared-config");
const mw = fn => Object.assign(fn, { isMiddleware: true });
const isGo = process.env.GO === "true" || process.env.GO === true;
const isPublic = process.env.PUBLIC === "true" || process.env.PUBLIC === true;
const addAlias = mw(config => {
const existingAlias = _get(config, "resolve.alias", {});
config = _set(
config,
"resolve.alias",
Object.assign({}, existingAlias, alias)
);
// console.log(require("util").inspect(config, false, null, true));
return config;
});
/**
Middleware that adds a console for logged messages to the screen.
An option for debugging across multiple devices
*/
const addScreenlog = mw(config => {
const showScreenlog = process.env.SCREENLOG === "true" ? true : false;
if (showScreenlog) {
config = appendWebpackPlugin(
new InjectPlugin(
() => `
import("screenlog")
.then(() => {
screenLog.init({
fontSize: "11px",
css: "left: 0; right: 0; top: 0; width: 100%; -webkit-overflow-scrolling: touch;"
});
});
`,
{
entryOrder: ENTRY_ORDER.First
}
),
config
);
}
return config;
});
const configGo = mw(config => {
const { mode = "development" } = config;
const env = process.env.REACT_APP_FORCE_ENV || mode;
const isProd = env === "production";
const goEntry = path.resolve(__dirname, "./src/go.js");
config.entry = [
...(Array.isArray(config.entry) ? config.entry : []).filter(
entryPath => entryPath.indexOf("/src/index.js") < 0
),
...(config.entry.includes(goEntry) ? [] : [goEntry])
];
let wellKnownPath = "src/go/.well-known";
try {
const envWellKnownPath = `${wellKnownPath}/${env}`;
fs.accessSync(path.resolve(__dirname, envWellKnownPath));
wellKnownPath = envWellKnownPath;
} catch (e) {}
config = appendWebpackPlugin(
new CopyWebpackPlugin([{ from: wellKnownPath, to: "./.well-known" }]),
config
);
if (!isProd) {
return config;
}
if (isPublic) {
let publicUrl =
process.env.REACT_APP_GO_URL;
publicUrl =
publicUrl.charAt(publicUrl.length - 1) === "/"
? publicUrl.slice(0, -1)
: publicUrl;
config = editWebpackPlugin(
p => {
p.definitions["process.env"].PUBLIC_URL = JSON.stringify(publicUrl);
return p;
},
"DefinePlugin",
config
);
config = editWebpackPlugin(
p => {
p.replacements.PUBLIC_URL = publicUrl;
return p;
},
"InterpolateHtmlPlugin",
config
);
}
return config;
});
const configWidget = mw(config => {
const isProd = config.mode === "production";
if (!isProd) {
config = editWebpackPlugin(
p => {
p.options.template = path.resolve(
__dirname,
`./tools/dev-site/index.html`
);
return p;
},
"HtmlWebpackPlugin",
config
);
config = appendWebpackPlugin(
new HtmlWebpackDeployPlugin({
assets: {
copy: [{ from: "tools/dev-site", to: "." }]
}
}),
config
);
return config;
}
const definePlugin = getWebpackPlugin("DefinePlugin", config);
config.plugins = [
definePlugin,
new RemovePlugin({
after: {
include: ["build/manifest.json", "build/favicon.ico"]
}
}),
new webpack.HashedModuleIdsPlugin()
];
config.output = Object.assign({}, config.output, {
filename: "[name].[contenthash:8].js",
chunkFilename: `[name].[contenthash:8].js`
});
config.optimization = Object.assign({}, config.optimization, {
runtimeChunk: false,
splitChunks: {
chunks: "all",
maxInitialRequests: 1,
minSize: 0,
automaticNameDelimiter: ".",
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
// npm package names are URL-safe, but some servers don't like @ symbols
return `vendor.${packageName.replace("@", "")}`;
}
}
}
}
});
if ((process.env.REACT_APP_FORCE_ENV || config.mode) !== "production") {
config.devtool = "eval-source-map";
}
if (isPublic) {
config.output = Object.assign({}, config.output, {
publicPath:
process.env.REACT_APP_JS_URL
});
}
// console.log(require("util").inspect(config, false, null, true));
// process.exit(1);
return config;
});
module.exports = [
["use-babel-config", ".babelrc.js"],
["use-eslint-config", ".eslintrc.js"],
addAlias,
addScreenlog,
isGo ? configGo : configWidget
];