UNPKG

1.57 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3const {title} = require("./logging"),
4 path = require("path"),
5 shell = require("shelljs"),
6 webpack = require("webpack");
7
8title("Bundling Server Webpack", "🔷");
9
10process.env.NODE_ENV = "development";
11const {name} = JSON.parse(shell.cat("package.json"));
12const appDir = process.cwd();
13const staticFolder = process.env.CANON_STATIC_FOLDER || "static";
14const staticPath = path.join(appDir, staticFolder);
15const canonPath = name === "@datawheel/canon-core" ? appDir : path.join(appDir, "node_modules/@datawheel/canon-core/");
16let started = false;
17
18shell.rm("-rf", path.join(staticPath, "assets/"));
19shell.mkdir("-p", path.join(staticPath, "assets/"));
20
21const webpackDevConfig = require(path.join(__dirname, "../webpack/dev-server.js"));
22const compiler = webpack(webpackDevConfig);
23
24compiler.watch({}, (err, stats) => {
25
26 if (err) console.error(err);
27 shell.echo(`webpack built server ${stats.compilation.hash} in ${stats.endTime - stats.startTime}ms`);
28 stats.compilation.errors.forEach(e => {
29 console.error("\n\n 🛑 SERVER WEBPACK ERROR\n");
30 console.error(e);
31 });
32 stats.compilation.warnings.forEach(e => {
33 console.warn("\n\n ⚠️ SERVER WEBPACK ERROR\n");
34 console.warn(e);
35 });
36
37 if (!shell.test("-f", path.join(staticPath, "assets/server.js"))) {
38 console.error("\n\n 🛑 SERVER WEBPACK ERROR\n");
39 console.error("Unable to build server.js");
40 shell.exit(1);
41 }
42 else if (!started) {
43 started = true;
44 shell.exec(`node ${path.join(canonPath, "bin/server/index.js")}`);
45 }
46
47});