Skip to content

Commit 9f2ffc7

Browse files
committed
fix: handle case of outputPath not being explicitly defined
1 parent d308467 commit 9f2ffc7

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

src/helpers/fixOutputDir.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { join } = require('path')
22

33
const getAngularJson = require('./getAngularJson')
4-
const { getProject } = require('./setUpEdgeFunction')
4+
const { getBuildInformation } = require('./setUpEdgeFunction')
55

66
const fixOutputDir = async function ({
77
failBuild,
@@ -14,14 +14,9 @@ const fixOutputDir = async function ({
1414
packagePath,
1515
}) {
1616
const angularJson = getAngularJson({ failPlugin, siteRoot, workspaceType, packagePath })
17-
const project = getProject(angularJson, failBuild, workspaceType === 'nx')
1817

19-
const { outputPath } = workspaceType === 'nx' ? project.targets.build.options : project.architect.build.options
18+
const { outputPath, isApplicationBuilder } = getBuildInformation(angularJson, failBuild, workspaceType)
2019

21-
const isApplicationBuilder =
22-
workspaceType === 'nx'
23-
? project.targets.build.executor.endsWith(':application')
24-
: project.architect.build.builder.endsWith(':application')
2520
const correctPublishDir = isApplicationBuilder ? join(outputPath, 'browser') : outputPath
2621
if (correctPublishDir === PUBLISH_DIR) {
2722
return

src/helpers/setUpEdgeFunction.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@ const getAllFilesIn = (dir) =>
2222

2323
const toPosix = (path) => path.split(sep).join(posix.sep)
2424

25-
const getProject = (angularJson, failBuild, isNxWorkspace = false) => {
25+
const getProjectName = (angularJson, failBuild) => {
2626
const selectedProject = process.env.ANGULAR_PROJECT
2727

28-
if (isNxWorkspace) {
29-
return angularJson
30-
}
31-
3228
if (selectedProject) {
3329
const project = angularJson.projects[selectedProject]
3430
if (!project) {
3531
return failBuild(
3632
`Could not find project selected project "${selectedProject}" in angular.json. Please update the ANGULAR_PROJECT environment variable.`,
3733
)
3834
}
39-
return project
35+
return selectedProject
4036
}
4137

4238
const projectNames = Object.keys(angularJson.projects)
@@ -47,14 +43,47 @@ const getProject = (angularJson, failBuild, isNxWorkspace = false) => {
4743
)
4844
}
4945

46+
return projectName
47+
}
48+
49+
const getProject = (angularJson, failBuild, isNxWorkspace = false, projectName = null) => {
50+
if (isNxWorkspace) {
51+
return angularJson
52+
}
53+
if (!projectName) {
54+
projectName = getProjectName(angularJson, failBuild)
55+
}
56+
5057
return angularJson.projects[projectName]
5158
}
5259

5360
module.exports.getProject = getProject
5461

62+
const getBuildInformation = (angularJson, failBuild, workspaceType) => {
63+
const projectName = getProjectName(angularJson, failBuild)
64+
const project = getProject(angularJson, failBuild, workspaceType === 'nx', projectName)
65+
66+
let { outputPath } = workspaceType === 'nx' ? project.targets.build.options : project.architect.build.options
67+
68+
if (!outputPath && workspaceType === 'default') {
69+
// outputPath might not be explicitly defined in angular.json
70+
// so we will try default which is dist/<project-name>
71+
outputPath = join('dist', projectName)
72+
}
73+
74+
const isApplicationBuilder =
75+
workspaceType === 'nx'
76+
? project.targets.build.executor.endsWith(':application')
77+
: project.architect.build.builder.endsWith(':application')
78+
79+
return { outputPath, isApplicationBuilder }
80+
}
81+
82+
module.exports.getBuildInformation = getBuildInformation
83+
5584
// eslint-disable-next-line max-lines-per-function
56-
const setUpEdgeFunction = async ({ outputDir, constants, failBuild, usedEngine }) => {
57-
const serverDistRoot = join(outputDir, 'server')
85+
const setUpEdgeFunction = async ({ outputPath, constants, failBuild, usedEngine }) => {
86+
const serverDistRoot = join(outputPath, 'server')
5887
if (!existsSync(serverDistRoot)) {
5988
console.log('No server output generated, skipping SSR setup.')
6089
return
@@ -66,11 +95,11 @@ const setUpEdgeFunction = async ({ outputDir, constants, failBuild, usedEngine }
6695
await mkdir(edgeFunctionDir, { recursive: true })
6796

6897
const html = await readFile(join(serverDistRoot, 'index.server.html'), 'utf-8')
69-
const staticFiles = getAllFilesIn(join(outputDir, 'browser')).map(
70-
(path) => `/${relative(join(outputDir, 'browser'), path)}`,
98+
const staticFiles = getAllFilesIn(join(outputPath, 'browser')).map(
99+
(path) => `/${relative(join(outputPath, 'browser'), path)}`,
71100
)
72101

73-
const excludedPaths = ['/.netlify/*', ...staticFiles, ...Object.keys(await getPrerenderedRoutes(outputDir))].map(
102+
const excludedPaths = ['/.netlify/*', ...staticFiles, ...Object.keys(await getPrerenderedRoutes(outputPath))].map(
74103
toPosix,
75104
)
76105

@@ -115,7 +144,7 @@ const setUpEdgeFunction = async ({ outputDir, constants, failBuild, usedEngine }
115144
`
116145
} else if (usedEngine === 'CommonEngine') {
117146
const cssAssetsManifest = {}
118-
const outputBrowserDir = join(outputDir, 'browser')
147+
const outputBrowserDir = join(outputPath, 'browser')
119148
const cssFiles = getAllFilesIn(outputBrowserDir).filter((file) => file.endsWith('.css'))
120149

121150
for (const cssFile of cssFiles) {

src/helpers/setUpHeaders.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const getPrerenderedRoutes = require('./getPrerenderedRoutes')
22

3-
const setUpHeaders = async ({ outputDir, netlifyConfig }) => {
4-
const prerenderRoutes = await getPrerenderedRoutes(outputDir)
3+
const setUpHeaders = async ({ outputPath, netlifyConfig }) => {
4+
const prerenderRoutes = await getPrerenderedRoutes(outputPath)
55

66
for (const [route, routeConfig] of Object.entries(prerenderRoutes)) {
77
if (routeConfig.headers) {

src/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getAngularJson = require('./helpers/getAngularJson')
66
const getAngularRoot = require('./helpers/getAngularRoot')
77
const { getAngularVersion } = require('./helpers/getPackageVersion')
88
const { fixServerTs, revertServerTsFix } = require('./helpers/serverModuleHelpers')
9-
const { getProject, setUpEdgeFunction } = require('./helpers/setUpEdgeFunction')
9+
const { getBuildInformation, setUpEdgeFunction } = require('./helpers/setUpEdgeFunction')
1010
const setUpHeaders = require('./helpers/setUpHeaders')
1111
const validateAngularVersion = require('./helpers/validateAngularVersion')
1212

@@ -60,17 +60,15 @@ module.exports = {
6060
const { siteRoot, workspaceType } = getAngularRoot({ failBuild, netlifyConfig, onBuild: true })
6161
const angularJson = getAngularJson({ failPlugin, siteRoot, workspaceType, packagePath: constants.PACKAGE_PATH })
6262

63-
const project = getProject(angularJson, failBuild, workspaceType === 'nx')
64-
const build = workspaceType === 'nx' ? project.targets.build : project.architect.build
65-
const outputDir = build?.options?.outputPath
66-
if (!outputDir || !existsSync(outputDir)) {
63+
const { outputPath } = getBuildInformation(angularJson, failBuild, workspaceType)
64+
if (!outputPath || !existsSync(outputPath)) {
6765
return failBuild('Could not find build output directory')
6866
}
6967

70-
await setUpHeaders({ outputDir, netlifyConfig })
68+
await setUpHeaders({ outputPath, netlifyConfig })
7169

7270
await setUpEdgeFunction({
73-
outputDir,
71+
outputPath,
7472
constants,
7573
failBuild,
7674
usedEngine,

0 commit comments

Comments
 (0)