-
Notifications
You must be signed in to change notification settings - Fork 146
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
Changes from 1 commit
bd778a0
d5912fb
0eef59f
ae27f75
1d7a239
5fc0d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -17,13 +17,18 @@ import fs from 'fs' | |||||||
* @returns {webpack.NormalModuleReplacementPlugin} | ||||||||
*/ | ||||||||
export const createModuleReplacementPlugin = (projectDir) => { | ||||||||
// Helper function to create a RegExp object from a string | ||||||||
const makeRegExp = (str, sep = path.sep) => { | ||||||||
// Replace unix paths with windows if needed and build a RegExp | ||||||||
if (sep === '\\') { | ||||||||
str = str.replace(/\//g, '\\\\') | ||||||||
} | ||||||||
return new RegExp(str) | ||||||||
} | ||||||||
|
||||||||
// List of overridable paths | ||||||||
// path: The RegExp that matches the path to the overridable component | ||||||||
// newPath: The path to the component in the project directory | ||||||||
const overridables = [ | ||||||||
{ | ||||||||
path: makeRegExp('pwa-kit-react-sdk(/dist)?/ssr/universal/components/_app-config$'), | ||||||||
|
@@ -48,6 +53,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) => { | ||||||||
|
@@ -58,20 +64,30 @@ export const createModuleReplacementPlugin = (projectDir) => { | |||||||
}) | ||||||||
}) | ||||||||
|
||||||||
// Return a new `webpack.NormalModuleReplacementPlugin` instance | ||||||||
return new webpack.NormalModuleReplacementPlugin(/.*/, (resource) => { | ||||||||
const resolved = path.resolve(resource.context, resource.request) | ||||||||
// We only want to replace resources that are requested from the SDK | ||||||||
if (resource.context.includes('pwa-kit-react-sdk')) { | ||||||||
// Resolve the full path of the resource | ||||||||
const resolved = path.resolve(resource.context, resource.request) | ||||||||
|
||||||||
const replacement = replacements.find(({path}) => resolved.match(path)) | ||||||||
// Find the replacement for the resolved path from the overridables list | ||||||||
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') | ||||||||
] | ||||||||
if (replacement) { | ||||||||
// Check if the resource was requested from 'packages/pwa-kit-react-sdk' or 'node_modules/pwa-kit-react-sdk' | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the comment is redundant if we repeat the paths. In the future, if there are changes to the paths, the comment may be forgotten to be updated. Sometimes it's better to comment the why, rather than what the the line of code actually does.
Suggested change
|
||||||||
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)) | ||||||||
const requestedFromSDK = sdkPaths.some((p) => resource.context.includes(p)) | ||||||||
|
||||||||
if (requestedFromSDK && replacement) { | ||||||||
resource.request = replacement.newPath | ||||||||
// If the resource was requested from SDK, replace the resource request with the replacement path | ||||||||
if (requestedFromSDK) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about moving this check earlier in the logic? I think conceptually it goes together with the first So I'm thinking something like this: if (resource.context.includes('pwa-kit-react-sdk') && sdkPaths.some((p) => resource.context.includes(p))) {
...
} That |
||||||||
resource.request = replacement.newPath | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
}) | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's not always a component...