Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pwa-kit-dev): minor performance improvements and added comments #974

22 changes: 15 additions & 7 deletions packages/pwa-kit-dev/src/configs/webpack/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const createModuleReplacementPlugin = (projectDir) => {
}
return new RegExp(str)
}

// List of overridable paths
// path: The RegExp that matches the path to the overridable file
// newPath: The corresponding path in the project directory
const overridables = [
{
path: makeRegExp('pwa-kit-react-sdk(/dist)?/ssr/universal/components/_app-config$'),
Expand All @@ -48,6 +52,7 @@ export const createModuleReplacementPlugin = (projectDir) => {
]
const extensions = ['.ts', '.tsx', '.js', '.jsx']

// Find the replacement for each overridable path by checking if the file exists
const replacements = []
overridables.forEach(({path, newPath}) => {
extensions.forEach((ext) => {
Expand All @@ -59,19 +64,22 @@ export const createModuleReplacementPlugin = (projectDir) => {
})

return new webpack.NormalModuleReplacementPlugin(/.*/, (resource) => {
const resolved = path.resolve(resource.context, resource.request)

const replacement = replacements.find(({path}) => resolved.match(path))

const sdkPaths = [
path.join('packages', 'pwa-kit-react-sdk'),
path.join('node_modules', 'pwa-kit-react-sdk')
]

const requestedFromSDK = sdkPaths.some((p) => resource.context.includes(p))
if (
resource.context.includes('pwa-kit-react-sdk') &&
sdkPaths.some((p) => resource.context.includes(p))
) {
const resolved = path.resolve(resource.context, resource.request)

if (requestedFromSDK && replacement) {
resource.request = replacement.newPath
const replacement = replacements.find(({path}) => resolved.match(path))

if (replacement) {
resource.request = replacement.newPath
}
}
})
}