Skip to content

Commit 2afbd66

Browse files
committed
Move rule into extension sdk project
1 parent 4ebb885 commit 2afbd66

File tree

3 files changed

+95
-55
lines changed

3 files changed

+95
-55
lines changed

packages/pwa-kit-dev/src/configs/webpack/config.js

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {sdkReplacementPlugin} from './plugins'
3030
import {CLIENT, SERVER, CLIENT_OPTIONAL, SSR, REQUEST_PROCESSOR} from './config-names'
3131

3232
// Utilities
33-
import {ruleForApplicationExtensibility} from '@salesforce/pwa-kit-extension-sdk/configs/webpack'
33+
import {ruleForApplicationExtensibility, ruleForOverrideResolver} from '@salesforce/pwa-kit-extension-sdk/configs/webpack'
3434
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
3535
import {
3636
buildAliases,
@@ -141,49 +141,6 @@ const findDepInStack = (pkg) => {
141141
return candidate
142142
}
143143

144-
const dontProcessAgainWeb = []
145-
146-
const dontProcessAgainNode = []
147-
/**
148-
*
149-
* @param {*} source
150-
* @returns
151-
*/
152-
const isSourceInDotFile = (source, target) => {
153-
const sourceOriginal = source
154-
const isMonoRepo = true
155-
const isExtensionFile = source.includes('packages/extension-')
156-
157-
let dontProcessAgain = target === 'web' ? dontProcessAgainWeb : dontProcessAgainNode
158-
159-
if (dontProcessAgain.includes(sourceOriginal)) {
160-
return false
161-
}
162-
163-
if (!isExtensionFile) {
164-
return false
165-
}
166-
167-
let overridables = []
168-
try {
169-
overridables = fse.readFileSync(`${projectDir}/.force_overrides`, 'utf8').split('\n')
170-
}
171-
catch (e) {
172-
// console.error('No .overridable file found')
173-
}
174-
175-
if (isMonoRepo) {
176-
source = `@salesforce/${source.replace('/Users/bchypak/Projects/pwa-kit/packages/', '')}`
177-
}
178-
179-
if (overridables.includes(source)) {
180-
console.log('Manual Override for: ', source)
181-
dontProcessAgain.push(sourceOriginal)
182-
}
183-
184-
return overridables.includes(source)
185-
}
186-
187144
const baseConfig = (target) => {
188145
if (!['web', 'node'].includes(target)) {
189146
throw Error(`The value "${target}" is not a supported webpack target`)
@@ -314,14 +271,7 @@ const baseConfig = (target) => {
314271
target: 'node'
315272
}
316273
}),
317-
{
318-
test: (source) => {
319-
return isSourceInDotFile(source, target)
320-
},
321-
use: {
322-
loader: '@salesforce/pwa-kit-extension-sdk/configs/webpack/overrides-resolver-loader'
323-
}
324-
}
274+
ruleForOverrideResolver({target, projectDir})
325275
].filter(Boolean)
326276
}
327277
}

packages/pwa-kit-extension-sdk/src/configs/webpack/overrides-resolver-loader.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
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'
89
import os from 'os'
910
import path from 'path'
1011
import resolve from 'resolve'
@@ -16,7 +17,9 @@ import {buildCandidatePaths, getPackageName} from '../../shared/utils'
1617
import type {ExtendedCompiler} from './types'
1718

1819
// Constants
20+
const EXTENSION_PACKAGE_PREFIX = 'extension-'
1921
const IMPORT_REGEX = /import\s+(?:(?:[\w*\s{},]*)\s+from\s+)?['"](\..*?)['"]/g
22+
const OVERRIDABLE_FILE_NAME = '.force_overrides'
2023
const REQUIRES_REGEX = /require\(['"](\..*?)['"]\)/g
2124
const 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
119207
export default OverrideResolverLoader

packages/template-typescript-minimal/.force_overrides

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// USE ONLY AS A TEMPORARY SOLUTION TO URGENTLY PATCH/UPDATE AN EXTENSION.
66
//
77
// USAGE:
8-
// PLACE THE RELATIVE PATH TO THE EXTENSION FILE YOU WANT TO OVERRIDE STARTING WITH THE EXTENSION PACKAGE NAME.
9-
// MULTIPLE OVERRIDES CAN BE ADDED TO THIS FILE, ONE PER LINE.
8+
// PLACE THE RELATIVE __POSIX__ PATH TO THE EXTENSION FILE YOU WANT TO OVERRIDE STARTING WITH THE EXTENSION PACKAGE NAME.
9+
// MULTIPLE OVERRIDES CAN BE ADDED TO THIS FILE, ONE PER LINE.\
10+
// EXAMPLE:
11+
// @salesforce/extension-sample/src/extensions/
1012
@salesforce/extension-chakra-store-locator/src/components/list.tsx

0 commit comments

Comments
 (0)