@@ -8,6 +8,7 @@ const { existsSync } = require('fs'); |
8 | 8 | const customConfig = path.resolve(process.cwd(), './webpack.config.js'); |
9 | 9 | const config = existsSync(customConfig) ? require(customConfig) : require('../webpack.config'); |
10 | 10 | const dotenv = require('dotenv') |
| 11 | +const fetch = require('node-fetch') |
11 | 12 |
|
12 | 13 | function getConfig(options) { |
13 | 14 | return config.map((env) => env(null, options)) |
@@ -62,6 +63,7 @@ async function start({ input, port, env, mode = 'spa', hot }) { |
62 | 63 | process.env['NULLSTACK_ENVIRONMENT_HOT'] = (!!hot).toString() |
63 | 64 | if (!process.env['NULLSTACK_PROJECT_DOMAIN']) process.env['NULLSTACK_PROJECT_DOMAIN'] = 'localhost' |
64 | 65 | if (!process.env['NULLSTACK_WORKER_PROTOCOL']) process.env['NULLSTACK_WORKER_PROTOCOL'] = 'http' |
| 66 | + const target = `${process.env['NULLSTACK_WORKER_PROTOCOL']}://${process.env['NULLSTACK_PROJECT_DOMAIN']}:${process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT']}` |
65 | 67 | const devServerOptions = { |
66 | 68 | hot: 'only', |
67 | 69 | open: false, |
@@ -80,23 +82,38 @@ async function start({ input, port, env, mode = 'spa', hot }) { |
80 | 82 | }, |
81 | 83 | proxy: { |
82 | 84 | context: () => true, |
83 | | - target: `${process.env['NULLSTACK_WORKER_PROTOCOL']}://${process.env['NULLSTACK_PROJECT_DOMAIN']}:${process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT']}`, |
| 85 | + target, |
84 | 86 | proxyTimeout: 10 * 60 * 1000, |
85 | 87 | timeout: 10 * 60 * 1000, |
86 | 88 | }, |
87 | 89 | setupMiddlewares: (middlewares, devServer) => { |
88 | 90 | if (!devServer) { |
89 | 91 | throw new Error('webpack-dev-server is not defined'); |
90 | 92 | } |
91 | | - middlewares.unshift((req, res, next) => { |
| 93 | + middlewares.unshift(async (req, res, next) => { |
92 | 94 | if (req.originalUrl.indexOf('.hot-update.') === -1) { |
93 | 95 | if (req.originalUrl.startsWith('/nullstack/')) { |
94 | 96 | console.log(` ⚙️ [${req.method}] ${req.originalUrl}`) |
95 | 97 | } else { |
96 | 98 | console.log(` 🕸️ [${req.method}] ${req.originalUrl}`) |
97 | 99 | } |
98 | 100 | } |
99 | | - next() |
| 101 | + async function waitForServer() { |
| 102 | + if (req.originalUrl.includes('.')) { |
| 103 | + return next() |
| 104 | + } |
| 105 | + try { |
| 106 | + await fetch(`${target}${req.originalUrl}`) |
| 107 | + next() |
| 108 | + } catch (error) { |
| 109 | + if (error.message.includes('ECONNREFUSED')) { |
| 110 | + setTimeout(waitForServer, 100) |
| 111 | + } else { |
| 112 | + throw error |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + waitForServer() |
100 | 117 | }); |
101 | 118 | return middlewares; |
102 | 119 | }, |
|