|
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 { createHandler as createGraphqlEndpointHandler } from "graphql-http/lib/use/express"
|
@@ -50,6 +50,7 @@ import { getPageMode } from "./page-mode"
|
50 | 50 | import { configureTrailingSlash } from "./express-middlewares"
|
51 | 51 | import type { Express } from "express"
|
52 | 52 | import { addImageRoutes } from "gatsby-plugin-utils/polyfill-remote-file"
|
| 53 | +import { isFileInsideCompilations } from "./webpack/utils/is-file-inside-compilations" |
53 | 54 |
|
54 | 55 | type ActivityTracker = any // TODO: Replace this with proper type once reporter is typed
|
55 | 56 |
|
@@ -413,6 +414,19 @@ export async function startServer(
|
413 | 414 | store.getState().program.directory,
|
414 | 415 | req.query.moduleId as string
|
415 | 416 | )
|
| 417 | + |
| 418 | + const compilation: Compilation = |
| 419 | + res.locals?.webpack?.devMiddleware?.stats?.compilation |
| 420 | + if (!compilation) { |
| 421 | + res.json(emptyResponse) |
| 422 | + return |
| 423 | + } |
| 424 | + |
| 425 | + if (!isFileInsideCompilations(absolutePath, compilation)) { |
| 426 | + res.json(emptyResponse) |
| 427 | + return |
| 428 | + } |
| 429 | + |
416 | 430 | try {
|
417 | 431 | sourceContent = fs.readFileSync(absolutePath, `utf-8`)
|
418 | 432 | } catch (e) {
|
@@ -540,7 +554,24 @@ export async function startServer(
|
540 | 554 | return
|
541 | 555 | }
|
542 | 556 |
|
543 |
| - const sourceContent = await fs.readFile(filePath, `utf-8`) |
| 557 | + const absolutePath = path.resolve( |
| 558 | + store.getState().program.directory, |
| 559 | + filePath |
| 560 | + ) |
| 561 | + |
| 562 | + const compilation: Compilation = |
| 563 | + res.locals?.webpack?.devMiddleware?.stats?.compilation |
| 564 | + if (!compilation) { |
| 565 | + res.json(emptyResponse) |
| 566 | + return |
| 567 | + } |
| 568 | + |
| 569 | + if (!isFileInsideCompilations(absolutePath, compilation)) { |
| 570 | + res.json(emptyResponse) |
| 571 | + return |
| 572 | + } |
| 573 | + |
| 574 | + const sourceContent = await fs.readFile(absolutePath, `utf-8`) |
544 | 575 |
|
545 | 576 | const codeFrame = codeFrameColumns(
|
546 | 577 | sourceContent,
|
|
0 commit comments