Skip to content

Commit

Permalink
Inline framework packages and check for react-dom/server usage
Browse files Browse the repository at this point in the history
The framework test was changed in gatsby 5.6 via gatsbyjs/gatsby#37508

Therefor framework.test is no longer of type RegExp and fixFrameworkCache would return early. This resulted in the non-creation of framework-[contenthash].js. Instead the framework was then located in app-[contenthash].js

Since I didn't find a good way to get the framework bundles, I just inlined them from the gatsby repos. They were not changed since 2020 (the creation of the framework chunking). So this seems pretty stable.

Further this now checks for react-dom/server and does not include it in the framework bundle as done in the  main gatsby repos by PR linked above.
  • Loading branch information
danilobuerger committed Feb 8, 2023
1 parent 298e4d0 commit af7d240
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Configuration, Options } from 'webpack';
import get from 'lodash.get';
import { createRequire } from './utils';

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

/**
* Fix missing framework in development.
* See https://github.com/Js-Brecht/gatsby-plugin-pnpm/issues/8
Expand All @@ -14,38 +16,34 @@ export const fixFrameworkCache = (config: Configuration, siteDirectory: string)

if (!framework) return;
if (typeof framework !== "object" || !framework.test) return;
if (!(framework.test instanceof RegExp)) return;

const regVal = framework.test
.toString()
.replace(/[[\\\]]/g, "")
.slice(1, -1);
const frameworkPackages = /\/\(([^)]+)\)\/$/.exec(regVal);
const frameworkList: string[] = [];

if (frameworkPackages) {
const frameworkRequire = createRequire(`${siteDirectory}/:internal:`);
Object.assign(
frameworkList,
frameworkPackages[1]
.split("|")
.map((f) => {
try {
return path.dirname(
frameworkRequire.resolve(`${f}/package.json`),
) + path.sep;
} catch (err) {
return "";
}
})
.filter(Boolean),
);
}
const frameworkRequire = createRequire(`${siteDirectory}/:internal:`);
Object.assign(
frameworkList,
FRAMEWORK_BUNDLES
.map((f) => {
try {
return path.dirname(
frameworkRequire.resolve(`${f}/package.json`),
) + path.sep;
} catch (err) {
return "";
}
})
.filter(Boolean),
);

const isRootDependency = (val?: string) => (
frameworkList.some((f) => val?.startsWith(f))
);
framework.test = (mod) => (
isRootDependency(mod.resource)
);
framework.test = (mod) => {
if (
mod.rawRequest === `react-dom/server` ||
mod.rawRequest?.includes(`/react-dom-server`)
) {
return false;
}
return isRootDependency(mod.resource);
};
};

0 comments on commit af7d240

Please sign in to comment.