-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.js
More file actions
154 lines (134 loc) · 4.01 KB
/
Copy pathdev.js
File metadata and controls
154 lines (134 loc) · 4.01 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import path from "path";
import fs from "fs";
import chokidar from "chokidar";
import { fileURLToPath } from "url";
import webpack from "webpack";
import Forket from 'forket';
// import Forket from '../../../forket/index.js';
import serverConfigBase from "./webpack.server.config.js";
import clientConfigBase from "./webpack.client.config.js";
import command from "./utils/command.js";
import getAllFiles from "./utils/getAllFiles.js";
import copyFolder from './utils/copyFolder.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT = process.cwd();
const SRC = path.normalize(path.join(__dirname, "..", "src"));
const BUILD = path.normalize(path.join(__dirname, "..", "build"));
const DIST = path.normalize(path.join(__dirname, "..", "dist"));
let serverProcess;
let restart = false;
const forket = await Forket({
watch: true,
printGraph: true,
});
await forket.process();
// Watching for changes in the build directory, transpile, bundle and restart the server
chokidar.watch(`${BUILD}/**/*`, { ignoreInitial: true }).on("all", (event, file) => {
if (!restart) {
restart = true;
if (serverProcess) {
serverProcess.kill();
} else {
run();
}
}
});
run();
async function run() {
await buildServer();
await buildClient();
restart = false;
serverProcess = command(`node ${path.join(DIST, "server", "server.js")}`, ROOT, (code) => {
serverProcess = null;
if (code === null && restart) {
run();
}
});
};
async function buildServer() {
const serverBuildDir = path.join(BUILD, "server");
const files = getAllFiles(serverBuildDir);
const entryFiles = files.filter((f) => /\.(ts|tsx|js|jsx|mjs|cjs)$/.test(f));
const entries = {};
for (const absFile of entryFiles) {
const relFromBuild = path.relative(BUILD, absFile);
const noExt = relFromBuild.replace(/\.(ts|tsx|js|jsx|mjs|cjs)$/, "");
entries[noExt] = absFile;
}
const config = {
...serverConfigBase,
entry: entries,
output: {
...serverConfigBase.output,
path: DIST,
filename: "[name].js",
module: true,
chunkFormat: "module",
chunkFilename: "[name].js",
library: { type: "module" },
clean: false,
},
};
const compiler = webpack(config);
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
compiler.close(() => {});
if (err) return reject(err);
if (stats && stats.hasErrors()) {
return reject(new Error(stats.toString({ all: false, errors: true, colors: true })));
}
console.log(`Server files compiled successfully to ${DIST}/server`);
resolve();
});
});
}
async function buildClient() {
try {
const config = {
...clientConfigBase,
entry: path.join(BUILD, "client", "client.tsx"),
output: {
...clientConfigBase.output,
path: path.join(DIST, "public"),
filename: "bundle.js",
},
};
const compiler = webpack(config);
const stats = await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
compiler.close(() => {});
if (err) return reject(err);
if (stats && stats.hasErrors()) {
return reject(
new Error(stats.toString({ all: false, errors: true, colors: true }))
);
}
resolve(stats);
});
});
const meta = stats.toJson({
all: false,
assets: true,
chunks: true,
chunkModules: true,
reasons: true,
builtAt: true,
time: true,
hash: true,
moduleAssets: true,
});
fs.mkdirSync(path.join(DIST, "public"), { recursive: true });
fs.writeFileSync(
path.join(DIST, "public", "meta.json"),
JSON.stringify(meta, null, 2)
);
copyFolder(
path.join(BUILD, "client", "assets"),
path.join(DIST, "public", "assets")
);
console.log(`Client files compiled successfully to ${path.join(DIST, "public")}`);
} catch (error) {
console.error(`Error compiling client: ${error.message}`, error);
}
}