1 | #! /usr/bin/env node
|
2 |
|
3 | const {title} = require("./logging"),
|
4 | path = require("path"),
|
5 | shell = require("shelljs"),
|
6 | webpack = require("webpack");
|
7 |
|
8 | title("Bundling Server Webpack", "🔷");
|
9 |
|
10 | process.env.NODE_ENV = "development";
|
11 | const {name} = JSON.parse(shell.cat("package.json"));
|
12 | const appDir = process.cwd();
|
13 | const staticFolder = process.env.CANON_STATIC_FOLDER || "static";
|
14 | const staticPath = path.join(appDir, staticFolder);
|
15 | const canonPath = name === "@datawheel/canon-core" ? appDir : path.join(appDir, "node_modules/@datawheel/canon-core/");
|
16 | let started = false;
|
17 |
|
18 | shell.rm("-rf", path.join(staticPath, "assets/"));
|
19 | shell.mkdir("-p", path.join(staticPath, "assets/"));
|
20 |
|
21 | const webpackDevConfig = require(path.join(__dirname, "../webpack/dev-server.js"));
|
22 | const compiler = webpack(webpackDevConfig);
|
23 |
|
24 | compiler.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 | });
|