-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathutils.ts
More file actions
73 lines (67 loc) · 2.65 KB
/
utils.ts
File metadata and controls
73 lines (67 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fse from 'fs-extra'
import * as p from 'path'
import {getConfiguredExtensions} from '../../shared/utils'
/**
* Constants used for building Babel extensibility arguments:
*
* 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.
*
* PLACEHOLDER_PATH: Absolute path to `application-extensions.js`, a placeholder file in the
* `@salesforce/pwa-kit-extension-sdk` package. This file acts as a stand-in for extensions
* that may or may not be present, allowing Babel to process extensibility placeholders even
* if no actual extensions are added.
*/
const NODE_MODULES_PATH = 'node_modules'
const SERVER_PATH = p.join(
NODE_MODULES_PATH,
'@salesforce',
'pwa-kit-runtime',
'ssr',
'server',
'build-remote-server.js'
)
const PLACEHOLDER_PATH = p.join(
NODE_MODULES_PATH,
'@salesforce',
'pwa-kit-extension-sdk',
'express',
'placeholders',
'application-extensions.js'
)
/**
* Builds Babel extensibility arguments for processing specific files and paths.
* Accessing files via "node_modules" ensures compatibility in both mono-repos and
* developers' environments working on templates with extensibility. Avoids relative paths
* or symlinked paths that Babel doesn't support by using realpathSync.
*/
export const buildBabelExtensibilityArgs = (config: any) => {
try {
const extensions = getConfiguredExtensions(config)
const serverPath = fse.realpathSync(p.resolve(SERVER_PATH))
const placeHolderPath = fse.realpathSync(p.resolve(PLACEHOLDER_PATH))
const extensionSrcPaths =
extensions.length > 0
? extensions.map(
([packageName]) =>
fse.realpathSync(
p.resolve(p.join(NODE_MODULES_PATH, packageName, 'src'))
) + '/**'
)
: []
const extensionsPathsStr =
extensionSrcPaths.length > 0 ? `,${extensionSrcPaths.join(',')}` : ''
const babelArgs = `--only "app/**,${serverPath},${placeHolderPath}${extensionsPathsStr}"`
return babelArgs
} catch (error) {
console.error('Error building Babel extensibility arguments:', error)
throw error
}
}