|
1 | 1 | import webpackHotMiddleware from "@gatsbyjs/webpack-hot-middleware"
|
2 | 2 | import webpackDevMiddleware from "webpack-dev-middleware"
|
3 | 3 | import got, { Method } from "got"
|
4 |
| -import webpack from "webpack" |
| 4 | +import webpack, { Compilation } from "webpack" |
5 | 5 | import express from "express"
|
6 | 6 | import compression from "compression"
|
7 | 7 | import { graphqlHTTP, OptionsData } from "express-graphql"
|
@@ -55,6 +55,7 @@ import { getPageMode } from "./page-mode"
|
55 | 55 | import { configureTrailingSlash } from "./express-middlewares"
|
56 | 56 | import type { Express } from "express"
|
57 | 57 | import { addImageRoutes } from "gatsby-plugin-utils/polyfill-remote-file"
|
| 58 | +import { isFileInsideCompilations } from "./webpack/utils/is-file-inside-compilations" |
58 | 59 |
|
59 | 60 | type ActivityTracker = any // TODO: Replace this with proper type once reporter is typed
|
60 | 61 |
|
@@ -502,7 +503,24 @@ export async function startServer(
|
502 | 503 | return
|
503 | 504 | }
|
504 | 505 |
|
505 |
| - const sourceContent = await fs.readFile(filePath, `utf-8`) |
| 506 | + const absolutePath = path.resolve( |
| 507 | + store.getState().program.directory, |
| 508 | + filePath |
| 509 | + ) |
| 510 | + |
| 511 | + const compilation: Compilation = |
| 512 | + res.locals?.webpack?.devMiddleware?.stats?.compilation |
| 513 | + if (!compilation) { |
| 514 | + res.json(emptyResponse) |
| 515 | + return |
| 516 | + } |
| 517 | + |
| 518 | + if (!isFileInsideCompilations(absolutePath, compilation)) { |
| 519 | + res.json(emptyResponse) |
| 520 | + return |
| 521 | + } |
| 522 | + |
| 523 | + const sourceContent = await fs.readFile(absolutePath, `utf-8`) |
506 | 524 |
|
507 | 525 | const codeFrame = codeFrameColumns(
|
508 | 526 | sourceContent,
|
|
0 commit comments