44 * SPDX-License-Identifier: BSD-3-Clause
55 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66 */
7- import { LoaderContext } from 'webpack'
7+ import { LoaderContext , web } from 'webpack'
8+ import fse from 'fs-extra'
89import os from 'os'
910import path from 'path'
1011import resolve from 'resolve'
@@ -16,7 +17,9 @@ import {buildCandidatePaths, getPackageName} from '../../shared/utils'
1617import type { ExtendedCompiler } from './types'
1718
1819// Constants
20+ const EXTENSION_PACKAGE_PREFIX = 'extension-'
1921const IMPORT_REGEX = / i m p o r t \s + (?: (?: [ \w * \s { } , ] * ) \s + f r o m \s + ) ? [ ' " ] ( \. .* ?) [ ' " ] / g
22+ const OVERRIDABLE_FILE_NAME = '.force_overrides'
2023const REQUIRES_REGEX = / r e q u i r e \( [ ' " ] ( \. .* ?) [ ' " ] \) / g
2124const SRC = 'src'
2225
@@ -115,5 +118,90 @@ const OverrideResolverLoader = function (this: LoaderContext<any>) {
115118 } )
116119}
117120
121+ const OVERRIDABLE_CACHE = {
122+ node : [ ] as string [ ] ,
123+ web : [ ] as string [ ]
124+ }
125+
126+ /**
127+ *
128+ * @param {* } source
129+ * @returns
130+ */
131+ const validateOverrideSource = ( source : string , options : any = { } ) => {
132+ let normalizedSource
133+ const { target = 'node' , overridables = [ ] } = options
134+ const isMonoRepo = true
135+ const isExtensionFile = source . includes ( `${ path . sep } ${ EXTENSION_PACKAGE_PREFIX } ` )
136+ const targetCache = OVERRIDABLE_CACHE [ target as keyof typeof OVERRIDABLE_CACHE ]
137+
138+ // Exit early if we have:
139+ // 1. Processed this file already.
140+ // 2. The file is not an extension file.
141+ // 3. TBD - The file is in the disallowed list.
142+ if ( targetCache . includes ( source ) || ! isExtensionFile ) {
143+ return false
144+ }
145+
146+ if ( isMonoRepo ) {
147+ normalizedSource = `@salesforce/${ source . replace ( '/Users/bchypak/Projects/pwa-kit/packages/' , '' ) } `
148+ } else {
149+ // we are going to do something else here?
150+ normalizedSource = source
151+ }
152+
153+ // Check if the normalized source is in the list of overridables.
154+ const hasOverride = overridables . includes ( normalizedSource )
155+
156+ // If we have an override, add it to the cache so we don't process it again.
157+ if ( hasOverride ) {
158+ console . log ( 'Manual Override for: ' , source )
159+ targetCache . push ( source )
160+ }
161+
162+ return hasOverride
163+ }
164+
165+ /**
166+ * Generates a Webpack rule for application extensibility, configuring the loader for
167+ * handling application extensions based on the target (e.g., 'node' for server-side,
168+ * 'react' for client-side).
169+ *
170+ * @param {Object } [options={}] - Options to customize the Webpack rule.
171+ * @param {Object } [options.loaderOptions={}] - Loader-specific options.
172+ * @param {string } [options.loaderOptions.target=DEFAULT_TARGET] - The target environment, either 'node' or 'react'.
173+ * @param {Object } [options.loaderOptions.appConfig] - Optional application configuration to pass to the loader.
174+ *
175+ * @returns {Object } A Webpack rule configuration object for handling application extensions.
176+ */
177+ export const ruleForOverrideResolver = ( options : any = { } ) => {
178+ const { projectDir, target} = options
179+ let overridables : string [ ] = [ ]
180+
181+ try {
182+ overridables =
183+ fse
184+ . readFileSync ( path . join ( projectDir , OVERRIDABLE_FILE_NAME ) , 'utf8' )
185+ . split ( / \r ? \n / )
186+ . filter ( ( line ) => ! line . startsWith ( '//' ) )
187+ }
188+ catch ( e ) {
189+ // If where is no .force_overrides file, we can safely return null.
190+ return null
191+ }
192+
193+ return {
194+ test : ( source : string ) => {
195+ return validateOverrideSource ( source , {
196+ target,
197+ overridables
198+ } )
199+ } ,
200+ use : {
201+ loader : '@salesforce/pwa-kit-extension-sdk/configs/webpack/overrides-resolver-loader'
202+ }
203+ }
204+ }
205+
118206// Export the loader as the default export with proper typing
119207export default OverrideResolverLoader
0 commit comments