Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/commerce-sdk-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion packages/pwa-kit-dev/src/configs/babel/babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,21 @@ export default {
presets: [require('@babel/preset-env'), require('@babel/preset-react')],
plugins: [require('babel-plugin-dynamic-import-node-babel-7')]
}
}
},
ignore: [
function (filepath) {
Copy link
Contributor

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
Contributor 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

// Return false if it's an allowed extension package @salesforce/pwa-kit-extension-sdk and extension-*
if (/node_modules\/@[^/]+\/(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
}
]
}
2 changes: 2 additions & 0 deletions packages/pwa-kit-dev/src/configs/webpack/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// This regex exclude everything in node_modules, but node_modules/extensions-*/ folders
exclude: /node_modules\/(?!(@?[^/]+\/)?extension-)[^/]+\/.*$/i,
Copy link
Contributor

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
Contributor 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.

use: [
{
loader: findDepInStack('babel-loader'),
Expand Down
22 changes: 12 additions & 10 deletions packages/pwa-kit-extension-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions packages/pwa-kit-extension-sdk/src/configs/babel/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {getConfiguredExtensions} from '../../shared/utils'
/**
* Constants used for building Babel extensibility arguments:
*
* IGNORE_PATH: Provides a placeholder path that does not exist, used in Babel's `--ignore`
* option to prevent processing of any unintended files by specifying a path that cannot match
* any actual file. This effectively disables Babel's ignore behavior when no other paths are specified.
*
* SERVER_PATH: Absolute path to the server entry point (`build-remote-server.js`) within the
* `@salesforce/pwa-kit-runtime` package. Required for Babel processing to ensure that server-side
* code, especially for SSR, is correctly transpiled as part of the extensibility setup.
Expand All @@ -25,7 +21,6 @@ import {getConfiguredExtensions} from '../../shared/utils'
* if no actual extensions are added.
*/
const NODE_MODULES_PATH = 'node_modules'
const IGNORE_PATH = `${NODE_MODULES_PATH}/does_not_exist`
const SERVER_PATH = `${NODE_MODULES_PATH}/@salesforce/pwa-kit-runtime/ssr/server/build-remote-server.js`
const PLACEHOLDER_PATH = `${NODE_MODULES_PATH}/@salesforce/pwa-kit-extension-sdk/express/placeholders/application-extensions.js`

Expand All @@ -42,12 +37,15 @@ export const buildBabelExtensibilityArgs = (config: any) => {

const extensionSrcPaths =
extensions.length > 0
? extensions.map(([packageName]) =>
fse.realpathSync(p.resolve(`${NODE_MODULES_PATH}/${packageName}/src`))
? extensions.map(
([packageName]) =>
fse.realpathSync(p.resolve(`${NODE_MODULES_PATH}/${packageName}/src`)) + '/**'
)
: []

const extensionsPathsStr = extensionSrcPaths.length > 0 ? `,${extensionSrcPaths.join(',')}` : ''

return `--ignore "${IGNORE_PATH}" --only "app/**,${serverPath},${placeHolderPath}${extensionsPathsStr}/**"`
const babelArgs = `--only "app/**,${serverPath},${placeHolderPath}${extensionsPathsStr}"`

return babelArgs
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
import * as fse from 'fs-extra'
import * as path from 'path'
import {buildBabelExtensibilityArgs} from './utils'
import {ApplicationExtensionEntryTuple} from '../../types'
import {buildBabelExtensibilityArgs} from './babel/utils'
import {ApplicationExtensionEntryTuple} from '../types'

jest.mock('fs-extra', () => ({
...jest.requireActual('fs-extra'),
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('buildBabelExtensibilityArgs', () => {
})

test('should return the correct Babel arguments string', () => {
const expectedArgs = `--ignore "node_modules/does_not_exist" --only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js,/absolute/path/to/@salesforce/extension-this/src,/absolute/path/to/@salesforce/extension-that/src/**"`
const expectedArgs = `--only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js,/absolute/path/to/@salesforce/extension-this/src/**,/absolute/path/to/@salesforce/extension-that/src/**"`
const result = buildBabelExtensibilityArgs(CONFIG)
expect(result).toBe(expectedArgs)
})
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('buildBabelExtensibilityArgs', () => {
})

test('should handle an empty list of configured extensions', () => {
const expectedArgs = `--ignore "node_modules/does_not_exist" --only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js/**"`
const expectedArgs = `--only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js"`
const result = buildBabelExtensibilityArgs({app: {extensions: []}})
expect(result).toBe(expectedArgs)
const result2 = buildBabelExtensibilityArgs({app: {}})
Expand Down
4 changes: 2 additions & 2 deletions packages/template-retail-react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading