Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'esbuild-server.mjs')
-rw-r--r--esbuild-server.mjs144
1 files changed, 87 insertions, 57 deletions
diff --git a/esbuild-server.mjs b/esbuild-server.mjs
index 906ff63..bf73692 100644
--- a/esbuild-server.mjs
+++ b/esbuild-server.mjs
@@ -22,6 +22,7 @@ const mime = {
'.jpg': 'image/jpeg',
'.css': 'text/css; charset=utf-8',
'.html': 'text/html; charset=utf-8',
+ '.js': 'text/javascript; charset=utf-8',
};
function createServerSentEventHandler() {
@@ -220,7 +221,7 @@ const externalSvgToInternal = {
}
}
-const ctx = await esbuild.context({
+const buildOptions = {
entryPoints: ['src/index.js', 'src/soldier_record_block.js', 'src/map.js'],
bundle: true,
outdir: 'build',
@@ -233,66 +234,95 @@ const ctx = await esbuild.context({
'.svg': 'file'
},
assetNames: 'assets/images/[name]-[hash]',
-});
-
-await ctx.watch();
-
-const { host, port } = await ctx.serve({
- servedir: 'build',
- port: 3000,
- // onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
- // let statusColor = colors.red;
-
- // if (status >= 200 && status <= 299) {
- // statusColor = colors.green;
- // } else if (status >= 300 && status <= 399) {
- // statusColor = colors.yellow;
- // }
-
- // console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
- // },
-});
-
-http.createServer((req, res) => {
- const options = {
- hostname: host,
- port: port,
- path: req.url,
- method: req.method,
- headers: req.headers,
- }
+};
- const filename = req.url && req.url !== '/' ? req.url : 'index.html';
- const filepath = path.join(__dirname, 'public', filename);
+if (process.env.NODE_ENV === 'test') {
+ http.createServer((req, res) => {
+ const filename = req.url && req.url !== '/' ? req.url : 'index.html';
+ const filepath = ['public', 'build'].map(dir => path.join(__dirname, dir, filename)).find(fp => fs.existsSync(fp));
- if (fs.existsSync(filepath)) {
- const readStream = fs.createReadStream(filepath, { autoClose: true });
+ if (filepath) {
+ const readStream = fs.createReadStream(filepath, { autoClose: true });
- readStream.on('ready', () => {
- const type = mime[path.parse(filepath).ext] || 'application/octet-stream';
- console.log(`${req.method} ${req.url} => 200 ${type}`);
- res.writeHead(200, { 'content-type': type });
- readStream.pipe(res, { end: true });
- });
+ readStream.on('ready', () => {
+ const type = mime[path.parse(filepath).ext] || 'text/plain';
+ res.writeHead(200, { 'content-type': type });
+ readStream.pipe(res, { end: true });
+ });
- readStream.on('error', err => {
- console.log(`${req.method} ${req.url} => 500 ${filepath} ${err.name}`);
- res.writeHead(500, err.name);
- res.end(JSON.stringify(err));
- });
- } else {
- const proxyReq = http.request(options, proxyRes => {
- const type = proxyRes.headers['content-type'];
- console.log(`${req.method} ${req.url} => ${proxyRes.statusCode} ${type} via esbuild`);
- res.writeHead(proxyRes.statusCode, { 'content-type': type });
- // if (req.url === '/esbuild') buildListeners.setupConnection(req, res);
- proxyRes.pipe(res);
- });
+ readStream.on('error', err => {
+ res.writeHead(500, err.name);
+ res.end(JSON.stringify(err));
+ });
+ } else {
+ res.writeHead(404, { 'content-type': 'text/plain' });
+ res.end("Not found");
+ }
+ }).listen(3005, () => {
+ console.log('test server is online');
+ });
+} else {
+ buildOptions.define = { 'window.IS_DEV': 'true' };
+ const ctx = await esbuild.context(buildOptions);
+ await ctx.watch();
+
+ const { host, port } = await ctx.serve({
+ servedir: 'build',
+ port: 3000,
+ // onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
+ // let statusColor = colors.red;
+
+ // if (status >= 200 && status <= 299) {
+ // statusColor = colors.green;
+ // } else if (status >= 300 && status <= 399) {
+ // statusColor = colors.yellow;
+ // }
+
+ // console.log(`${colors.dim}${remoteAddress} - "${method} ${path}" ${colors.normal}${statusColor}${status}${colors.reset}${colors.dim} [${timeInMS}ms]${colors.reset}`);
+ // },
+ });
+
+ http.createServer((req, res) => {
+ const options = {
+ hostname: host,
+ port: port,
+ path: req.url,
+ method: req.method,
+ headers: req.headers,
+ }
+
+ const filename = req.url && req.url !== '/' ? req.url : 'index.html';
+ const filepath = path.join(__dirname, 'public', filename);
+
+ if (fs.existsSync(filepath)) {
+ const readStream = fs.createReadStream(filepath, { autoClose: true });
+
+ readStream.on('ready', () => {
+ const type = mime[path.parse(filepath).ext] || 'application/octet-stream';
+ console.log(`${req.method} ${req.url} => 200 ${type}`);
+ res.writeHead(200, { 'content-type': type });
+ readStream.pipe(res, { end: true });
+ });
- req.pipe(proxyReq, { end: true });
- }
-}).listen(8080, (e) => {
- console.log('server is online', e);
-});
+ readStream.on('error', err => {
+ console.log(`${req.method} ${req.url} => 500 ${filepath} ${err.name}`);
+ res.writeHead(500, err.name);
+ res.end(JSON.stringify(err));
+ });
+ } else {
+ const proxyReq = http.request(options, proxyRes => {
+ const type = proxyRes.headers['content-type'];
+ console.log(`${req.method} ${req.url} => ${proxyRes.statusCode} ${type} via esbuild`);
+ res.writeHead(proxyRes.statusCode, { 'content-type': type });
+ // if (req.url === '/esbuild') buildListeners.setupConnection(req, res);
+ proxyRes.pipe(res);
+ });
+
+ req.pipe(proxyReq, { end: true });
+ }
+ }).listen(8080, (e) => {
+ console.log('server is online', e);
+ });
+}
// console.log(`${process.env.NODE_ENV === 'test' ? 'Test' : 'Development'} server running at ${server.url}`);