-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (68 loc) · 2.06 KB
/
index.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
// eslint-disable-next-line no-unused-vars
const colors = require('colors');
const webpack = require('webpack');
const { existsSync } = require('fs');
const path = require('path');
const frontend = require('./webpack.client.config.js');
const backend = require('./webpack.server.config.js');
const getVariables = (includeWatch) => {
const IS_DEPLOYED = process.env.IS_DEPLOYED;
if (!IS_DEPLOYED) require('dotenv').config();
const APP_VERSION = process.env.APP_VERSION || process.env.npm_package_version;
const APP_ID = process.env.APP_ID || null;
const NODE_ENV = process.env.NODE_ENV || 'development';
const isProd = NODE_ENV === 'production';
const mode = isProd ? 'production' : 'development';
const devtool = isProd ? 'source-map' : 'eval-cheap-module-source-map';
const watch = !isProd;
const variables = {
APP_ID,
APP_VERSION,
devtool,
isProd,
mode,
NODE_ENV
};
const variablesWatch = {
...variables,
watch
};
return { variables, variablesWatch };
};
const cb = (error, stats) => {
if (error) console.error('error:', error);
console.log(
stats.toString({
assets: false,
chunks: false,
colors: true,
moduleAssets: false,
modules: false,
entrypoints: 'auto'
})
);
console.log('Build complete.'.brightMagenta);
};
const build = (variables) => {
console.log('Starting build.'.cyan);
return Promise.all([webpack(frontend(variables), cb), webpack(backend(variables), cb)]);
};
const prebuild = (build, variables, variablesWatch) => {
console.log('Prebuilding frontend to build manifest.'.cyan);
webpack(frontend(variables)).run((error, stats1) => {
if (error) console.error('error:', error);
return build(variablesWatch);
});
};
const startBuild = () => {
const manifestExists = existsSync(path.join('dist', 'client', 'assets-manifest.json'));
const { variables, variablesWatch } = getVariables();
if (manifestExists) {
console.log('manifest.json found.'.cyan);
return build(variablesWatch);
} else {
console.log('manifest.json not found.'.cyan);
return prebuild(build, variables, variablesWatch);
}
};
startBuild();