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

[App Extensibility ⚙️] Fix Babel configuration ignore node_modules folder (@W-17373534@) #2228

Merged
merged 15 commits into from
Jan 30, 2025

Conversation

adamraya
Copy link
Collaborator

@adamraya adamraya commented Jan 29, 2025

Description

Problem

We identified an issue where Babel is transpiling all files in node_modules due to using the wrong Babel configuration, leading to two warnings when we start the template-typescript-minimal project.

...
[BABEL] Note: The code generator has deoptimised the styling of /path/to/react-dom/cjs/react-dom.development.js as it exceeds the max of 500KB
[BABEL] Note: The code generator has deoptimised the styling of /path/to/react-dom/cjs/react-dom.development.js as it exceeds the max of 500KB

Solution

The PR fixes the Babel configuration to avoid transpiling all files in node_modules, solving the two warnings we saw when starting the dev server.

The Babel warnings no longer appear when starting the dev server.

Screenshare.-.2025-01-28.6_27_27.PM.mp4

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • Updated pwa-kit-dev Webpack Babel Configuration
  • Updated pwa-kit-dev Babel Configuration

How to Test-Drive This PR

  1. Run the generator with this configuration
node packages/pwa-kit-create-app/scripts/create-mobify-app-dev.js
? What type of PWA Kit project would you like to create? PWA Kit Application
? Do you want to use Application Extensibility? Yes
? Which Application Extensions do you want to install? @salesforce/extension-chakra-store-locator, @salesforce/extension-chakra-storefront
? ⚠️ WARNING: If you choose to extract the Application Extension code,
you will NO LONGER be able to consume upgrades from NPM. All changes
made to the extracted code will be YOUR RESPONSIBILITY.

Do you want to proceed with extracting the Application Extensions code? No
? What is the name of your Project? test
Installing dependencies for test... This may take a few minutes.
  1. Copy the content of packages/extension-chakra-storefront/config/sample.json into the extension-chakra-storefront inside the generated project package.json file.
  2. Strat the project removing Babel cache.
rm -Rf node_modules/.cache && BABEL_DISABLE_CACHE=1 npm start.

Verify

  1. Observe no Babel warnings are present in the dev server logs.
  2. Smoke test the extensions by navigating through the storefront

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@adamraya adamraya added the skip changelog Skip the "Changelog Check" GitHub Actions step even if the Changelog.md files are not updated label Jan 29, 2025
@adamraya adamraya requested a review from a team as a code owner January 29, 2025 02:18
Comment on lines 62 to 76
function (filepath) {
// Ignore node_modules, but include @salesforce/extension-* and pwa-kit-extension-sdk
if (/node_modules/.test(filepath)) {
// Only ignore non-extension @salesforce packages
if (
/node_modules\/(?!@salesforce\/pwa-kit-extension-sdk|@salesforce\/extension-)/.test(
filepath
)
) {
return true
}
return false
}
return false
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked AI to invert the logic to get rid of the nested conditions. Here is the output i got:

function (filepath) {
    // Return false if it's an allowed extension package
    if (/node_modules\/@salesforce\/(pwa-kit-extension-sdk|extension-)/.test(filepath)) {
        return false;
    }
    
    // Return true if it's in node_modules (excluding allowed packages handled above)
    if (/node_modules/.test(filepath)) {
        return true;
    }
    
    // Return false for all other files
    return false;
}

Comment on lines 343 to 344
// By default babel doesn't process files in "node_modules" folder, so here we will ensure they are included.
exclude: /node_modules\/(?!(@?[^/]+\/)?extension-)[^/]+\/.*$/i,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally I wouldn't advocate to document the "what", only the "why"s.

But I feel like when it comes to regexes, we should add a quick explanation of what this regex aims to do. Because it's hard to tell unless you copy and paste the regex to a tool like https://regexr.com/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. This regex exclude everything in node_modules, but node_modules/extensions-*/ folders

}
},
ignore: [
function (filepath) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL you can pass a function into babel config ignore :-o. Any doc link about this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically creating an inline Babel plugin. See https://babeljs.io/docs/plugins#plugin-development

// Only ignore non-extension @salesforce packages
if (
/node_modules\/(?!@salesforce\/pwa-kit-extension-sdk|@salesforce\/extension-)/.test(
filepath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unblocking comment, let's look a bit further in the future when partners start to create their own extensions. Do they have to follow the pattern of @salesforce/extension-* or just extension-* or their brand name like this @company/extension?
If it is more than @salesforce/extension-* we may want to allow other pattern here too to avoid bug later down the road.

@@ -340,6 +340,8 @@ const ruleForBabelLoader = (babelPlugins) => {
id: 'babel-loader',
test: /(\.js(x?)|\.ts(x?))$/,
// NOTE: Because our extensions are just folders containing source code, we need to ensure that the babel-loader processes them.
// By default babel doesn't process files in "node_modules" folder, so here we will ensure they are included.
exclude: /node_modules\/(?!(@?[^/]+\/)?extension-)[^/]+\/.*$/i,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question, we have exclude here and ignore in the babel config? How are they working with each other? I recall they can't be used on a same directory like node_modules because exclude is exclusive to the folder and will override ignore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are different configurations that don't work with each other. One is the Webpack configuration for babel-loader, and the other is the Babel configuration for babel-node, where we cannot use the exclude option. They are used in separate stages.

Copy link
Collaborator

@alexvuong alexvuong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally. Verified there were no Babel warnings in the terminal. LGTM

@adamraya adamraya merged commit 144759b into feature/extensibility-v2 Jan 30, 2025
27 checks passed
@adamraya adamraya deleted the W-17373534-ignore-node_modules branch January 30, 2025 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip changelog Skip the "Changelog Check" GitHub Actions step even if the Changelog.md files are not updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants