-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathbuild.js
100 lines (88 loc) · 3.03 KB
/
build.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
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path');
const chalk = require('chalk');
const fs = require('fs-extra');
const globby = require('globby');
const load = require('../load');
const createSitemap = require('../core/sitemap');
const createServerConfig = require('../webpack/server');
const createClientConfig = require('../webpack/client');
const {applyConfigureWebpack} = require('../webpack/utils');
function compile(config) {
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err) {
reject(err);
}
if (stats.hasErrors()) {
stats.toJson().errors.forEach(e => {
console.error(e);
});
reject(new Error('Failed to compile with errors.'));
}
if (stats.hasWarnings()) {
stats.toJson().warnings.forEach(warning => {
console.warn(warning);
});
}
resolve(stats.toJson({modules: false}));
});
});
}
module.exports = async function build(siteDir) {
process.env.NODE_ENV = 'production';
console.log('Build command invoked ...');
const props = await load(siteDir);
// Apply user webpack config.
const {outDir, plugins} = props;
const clientConfigObj = createClientConfig(props);
// Remove/clean build folders before building bundles.
clientConfigObj
.plugin('clean')
.use(CleanWebpackPlugin, [outDir, {verbose: false, allowExternal: true}]);
let serverConfig = createServerConfig(props).toConfig();
let clientConfig = clientConfigObj.toConfig();
// Plugin lifecycle - configureWebpack
plugins.forEach(({configureWebpack}) => {
if (!configureWebpack) {
return;
}
clientConfig = applyConfigureWebpack(configureWebpack, clientConfig, false);
serverConfig = applyConfigureWebpack(configureWebpack, serverConfig, true);
});
// Build the client bundles first.
// We cannot run them in parallel because the server needs to know
// the correct client bundle name.
await compile(clientConfig);
// Build the server bundles (render the static HTML and pick client bundle),
await compile(serverConfig);
// Copy static files.
const staticDir = path.resolve(siteDir, 'static');
const staticFiles = await globby(['**'], {
cwd: staticDir,
});
await Promise.all(
staticFiles.map(async source => {
const fromPath = path.resolve(staticDir, source);
const toPath = path.resolve(outDir, source);
return fs.copy(fromPath, toPath);
}),
);
// Generate sitemap.
const sitemap = await createSitemap(props);
const sitemapPath = path.join(outDir, 'sitemap.xml');
await fs.writeFile(sitemapPath, sitemap);
const relativeDir = path.relative(process.cwd(), outDir);
console.log(
`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(
relativeDir,
)}.\n`,
);
};