Skip to content

Commit

Permalink
fix(gatsby): don't place virtual modules in node_modules directory
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 13, 2020
1 parent da4b3ec commit 454169a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/gatsby/src/bootstrap/requires-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { match } from "@reach/router/lib/utils"
import { joinPath } from "gatsby-core-utils"
import { store, emitter } from "../redux/"
import { IGatsbyState, IGatsbyPage } from "../redux/types"
import { writeModule } from "../utils/gatsby-webpack-virtual-modules"
import {
writeModule,
getAbsolutePathForVirtualModule,
} from "../utils/gatsby-webpack-virtual-modules"
import { markWebpackStatusAsPending } from "../utils/webpack-status"

interface IGatsbyPageComponent {
Expand Down Expand Up @@ -213,7 +216,7 @@ const preferDefault = m => m && m.default || m
.map((c: IGatsbyPageComponent): string => {
// we need a relative import path to keep contenthash the same if directory changes
const relativeComponentPath = path.relative(
path.join(program.directory, `node_modules`, `$virtual`),
getAbsolutePathForVirtualModule(`$virtual`),
c.component
)
Expand Down
10 changes: 6 additions & 4 deletions packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import VirtualModulesPlugin from "webpack-virtual-modules"

import * as path from "path"
/*
* This module allows creating virtual (in memory only) modules / files
* that webpack compilation can access without the need to write module
Expand Down Expand Up @@ -32,10 +32,12 @@ export class GatsbyWebpackVirtualModules {
}
}

export function getAbsolutePathForVirtualModule(filePath: string): string {
return path.join(process.cwd(), `_this_is_virtual_fs_path_`, filePath)
}

export function writeModule(filePath: string, fileContents: string): void {
// "node_modules" added in front of filePath allow to allow importing
// those modules using same path
const adjustedFilePath = `node_modules/${filePath}`
const adjustedFilePath = getAbsolutePathForVirtualModule(filePath)

if (fileContentLookup[adjustedFilePath] === fileContents) {
// we already have this, no need to cause invalidation
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getGatsbyDependents } from "./gatsby-dependents"
const apiRunnerNode = require(`./api-runner-node`)
import { createWebpackUtils } from "./webpack-utils"
import { hasLocalEslint } from "./local-eslint-config-finder"
import { getAbsolutePathForVirtualModule } from "./gatsby-webpack-virtual-modules"

const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]

Expand Down Expand Up @@ -409,6 +410,7 @@ module.exports = async (
"socket.io-client": path.dirname(
require.resolve(`socket.io-client/package.json`)
),
$virtual: getAbsolutePathForVirtualModule(`$virtual`),
},
plugins: [
// Those two folders are special and contain gatsby-generated files
Expand Down

0 comments on commit 454169a

Please sign in to comment.