@wayneintacart/react-hydration-overlay
Version:
Fork of React utility for descriptive hydration mismatch errors.
66 lines (61 loc) • 2.81 kB
JavaScript
var webpack = require('webpack');
var path = require('path');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var webpack__default = /*#__PURE__*/_interopDefault(webpack);
var path__default = /*#__PURE__*/_interopDefault(path);
// `entryPoint` can be a string, array of strings, or object whose `import` property is one of those two
const getEntryPoint = (entryPoint)=>{
if (typeof entryPoint === "string") {
return [
entryPoint
];
} else if (Array.isArray(entryPoint)) {
return entryPoint;
} else if (typeof entryPoint === "object" && "import" in entryPoint) {
const entryImport = entryPoint.import;
return Array.isArray(entryImport) ? entryImport : [
entryImport
];
} else {
console.error("[ReactHydrationOverlay]: Could not add hydration overlay script due to unexpected entry point: ", entryPoint);
return null;
}
};
async function addScriptToEntryProperty({ currentEntryProperty, isMainAppEntryPoint }) {
const newEntryProperty = typeof currentEntryProperty === "function" ? await currentEntryProperty() : {
...currentEntryProperty
};
// inject script into main app entry point
for(const entryPointName in newEntryProperty){
const isBrowserMainAppEntryPoint = isMainAppEntryPoint(entryPointName);
if (isBrowserMainAppEntryPoint) {
const currentEntryPoint = newEntryProperty[entryPointName];
const newEntryPoint = getEntryPoint(currentEntryPoint);
const injectedScriptPath = path__default.default.join(__dirname, "hydration-overlay-initializer.js");
if (!newEntryPoint || newEntryPoint.includes(injectedScriptPath)) {
return newEntryProperty;
}
// Nextjs dev mode breaks if you insert the entry point anywhere but at the very end.
newEntryPoint.push(injectedScriptPath);
newEntryProperty[entryPointName] = newEntryPoint;
}
}
return newEntryProperty;
}
const withHydrationOverlayWebpack = ({ appRootSelector, isMainAppEntryPoint })=>(originalWebpackConfig = {})=>{
let rawNewConfig = {
...originalWebpackConfig
};
rawNewConfig.entry = async ()=>addScriptToEntryProperty({
currentEntryProperty: originalWebpackConfig.entry,
isMainAppEntryPoint
});
rawNewConfig.plugins = [
...rawNewConfig.plugins || [],
new webpack__default.default.DefinePlugin({
"window.BUILDER_HYDRATION_OVERLAY.APP_ROOT_SELECTOR": JSON.stringify(appRootSelector)
})
];
return rawNewConfig;
};
exports.withHydrationOverlayWebpack = withHydrationOverlayWebpack;