({options, router})
| 27 | let watcherSubscription; |
| 28 | |
| 29 | export async function start({options, router}) { |
| 30 | const parcel = await createParcel(options, true); |
| 31 | |
| 32 | // Storybook's CLI runs an Express-style server on :9003 (the user-facing |
| 33 | // dev URL). Parcel's own dev server runs on :3000 to serve iframe.html and |
| 34 | // the bundle assets. We mount a reverse proxy on Storybook's router so that |
| 35 | // any request that isn't the manager's own /index.html falls through to |
| 36 | // Parcel. If Parcel returns 404 or HTML for a non-iframe URL we hand the |
| 37 | // request back to next() so Storybook's manager middleware can handle it. |
| 38 | router.use(async (req, res, next) => { |
| 39 | if (req.url === '/' || req.url === '/index.html') return next(); |
| 40 | |
| 41 | const proxy = createProxyMiddleware({ |
| 42 | target: 'http://localhost:3000/', |
| 43 | selfHandleResponse: true, |
| 44 | logLevel: 'warn', |
| 45 | onProxyRes(proxyRes, req, res) { |
| 46 | if ( |
| 47 | proxyRes.statusCode === 404 || |
| 48 | (proxyRes.headers['content-type']?.startsWith('text/html') && |
| 49 | !req.url.startsWith('/iframe.html')) |
| 50 | ) { |
| 51 | return next(); |
| 52 | } |
| 53 | res.statusCode = proxyRes.statusCode; |
| 54 | for (const header in proxyRes.headers) { |
| 55 | res.setHeader(header, proxyRes.headers[header]); |
| 56 | } |
| 57 | proxyRes.pipe(res); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | const {socket, connection} = req; |
| 62 | req.socket = null; |
| 63 | req.connection = null; |
| 64 | await proxy(req, res, next); |
| 65 | req.socket = socket; |
| 66 | req.connection = connection; |
| 67 | }); |
| 68 | |
| 69 | watcherSubscription = await parcel.watch(); |
| 70 | process.on('SIGINT', async () => { |
| 71 | await watcherSubscription?.unsubscribe(); |
| 72 | process.exit(); |
| 73 | }); |
| 74 | |
| 75 | return { |
| 76 | async bail() { |
| 77 | await watcherSubscription?.unsubscribe(); |
| 78 | }, |
| 79 | stats: {}, |
| 80 | totalTime: 0 |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | export async function build({options}) { |
| 85 | const parcel = await createParcel(options); |
nothing calls this directly
no test coverage detected