UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2
3var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4
5const _fs = require("fs");
6const _os = require("os");
7const _path = require("path");
8
9const async = require("neo-async");
10const rimraf = require("rimraf");
11const webpack = require("webpack");
12const workerFarm = require("worker-farm");
13
14const workers = workerFarm(require.resolve("./worker"));
15
16const write = require("./write");
17
18module.exports = function ({ environment, configurations, configurationPaths }) {
19 return new Promise(function (resolve, reject) {
20 const output = configurations.antwar.output;
21
22 if (!output) {
23 return reject(new Error("Missing output directory"));
24 }
25
26 return runWebpack(require("../config/build")({ configurations })).then(generateParameters(configurations.antwar, configurations.webpack)).then(writePages(environment, configurations.antwar, configurationPaths)).then(removeSiteBundle(output)).then(resolve).catch(reject);
27 });
28};
29
30function runWebpack(config) {
31 return new Promise((resolve, reject) => {
32 webpack(config, (err, stats) => {
33 if (err) {
34 return reject(err);
35 }
36
37 if (stats.hasErrors()) {
38 return reject(stats.toString("errors-only"));
39 }
40
41 return resolve(stats);
42 });
43 });
44}
45
46function generateParameters(antwarConfiguration, webpackConfiguration) {
47 const publicPath = webpackConfiguration.output ? webpackConfiguration.output.publicPath : "";
48
49 return stats => new Promise(resolve => {
50 const assets = stats.compilation.assets;
51 const cssFiles = Object.keys(assets).map(asset => {
52 if (_path.extname(asset) === ".css") {
53 return assets[asset].existsAt;
54 }
55
56 return null;
57 }).filter(a => a);
58 const jsFiles = [];
59
60 // Copy template configuration to webpack side so HtmlWebpackPlugin picks it up
61 const template = _extends({
62 cssFiles: [],
63 jsFiles: []
64 }, antwarConfiguration.template);
65
66 const cwd = process.cwd();
67 const site = require(_path.join(cwd, antwarConfiguration.output, "site.js"));
68 const parameters = {
69 cwd,
70 renderPage: site.renderPage,
71 allPages: site.allPages,
72 output: _path.join(cwd, antwarConfiguration.output),
73 config: antwarConfiguration,
74 cssFiles,
75 jsFiles,
76 templates: {
77 page: _extends({}, template, {
78 // XXX: sync operation
79 file: _fs.readFileSync(template && template.file || _path.join(__dirname, "../../templates/page.ejs"), {
80 encoding: "utf8"
81 }),
82 cssFiles: cssFiles.map(cssFile => publicPath + "/" + _path.basename(cssFile)),
83 jsFiles
84 }),
85 // TODO: expose to the user?
86 interactive: {
87 file: _fs.readFileSync(_path.join(__dirname, "../../templates/interactive.ejs"), {
88 encoding: "utf8"
89 })
90 },
91 interactiveIndex: {
92 file: _fs.readFileSync(_path.join(__dirname, "../../templates/interactive_index.ejs"), {
93 encoding: "utf8"
94 })
95 }
96 }
97 };
98
99 resolve(parameters);
100 });
101}
102
103function writePages(environment, antwarConfiguration, configurationPaths) {
104 return parameters => new Promise((resolve, reject) => {
105 const config = parameters.config;
106 const assets = config && config.assets ? config.assets : [];
107
108 if (parameters.cssFiles) {
109 parameters.cssFiles.forEach(cssFile => {
110 assets.push({
111 from: cssFile,
112 to: "./" + _path.basename(cssFile)
113 });
114 });
115 }
116
117 write.pages(parameters, environment, configurationPaths)((err, tasks) => {
118 if (err) {
119 return reject(err);
120 }
121
122 executeTasks(tasks, antwarConfiguration.maximumWorkers, antwarConfiguration.console.log).then(() => resolve(parameters)).catch(reject);
123 });
124 });
125}
126
127function executeTasks(tasks, maximumWorkers, log) {
128 return new Promise((resolve, reject) => {
129 async.eachLimit(tasks, maximumWorkers || _os.cpus().length, (o, cb) => {
130 log("Starting to write pages");
131
132 workers(o, err => {
133 log("Finished writing pages");
134
135 cb(err);
136 });
137 }, err => {
138 workerFarm.end(workers);
139
140 if (err) {
141 return reject(err);
142 }
143
144 log("BUILD FINISHED!");
145
146 return resolve();
147 });
148 });
149}
150
151function removeSiteBundle(outputDirectory) {
152 return parameters => new Promise((resolve, reject) => {
153 rimraf(_path.join(process.cwd(), outputDirectory, "site.js"), err => {
154 if (err) {
155 return reject(err);
156 }
157
158 return resolve(parameters);
159 });
160 });
161}
\No newline at end of file