Skip to content

Commit 2f07113

Browse files
committed
chore: address some PR feedback and remove lingering schematic logic
1 parent 77ffdad commit 2f07113

10 files changed

+75
-87
lines changed

demo/angular.json

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,6 @@
109109
}
110110
}
111111
},
112-
"serverless": {
113-
"builder": "@angular-devkit/build-angular:server",
114-
"options": {
115-
"outputPath": "dist/angular-bfdx/serverless",
116-
"main": "serverless.ts",
117-
"tsConfig": "tsconfig.serverless.json"
118-
},
119-
"configurations": {
120-
"production": {
121-
"outputHashing": "media",
122-
"fileReplacements": [
123-
{
124-
"replace": "src/environments/environment.ts",
125-
"with": "src/environments/environment.prod.ts"
126-
}
127-
],
128-
"sourceMap": false,
129-
"optimization": true
130-
}
131-
}
132-
},
133112
"server": {
134113
"builder": "@angular-devkit/build-angular:server",
135114
"options": {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers/getAngularJson.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
const { existsSync, readFileSync } = require('fs')
2-
const path = require('path')
1+
const { join } = require('path')
2+
3+
const { existsSync, readJsonSync } = require('fs-extra')
34

45
// Get contents of project's angular.json file
5-
const getAngularJson = function ({ failBuild }) {
6-
const siteRoot = process.cwd()
7-
if (!existsSync(path.join(siteRoot, 'angular.json'))) {
6+
const getAngularJson = function ({ failBuild, siteRoot }) {
7+
if (!existsSync(join(siteRoot, 'angular.json'))) {
88
return failBuild(`No angular.json found at project root`)
99
}
1010
try {
11-
const angularJsonFile = readFileSync(path.join(siteRoot, 'angular.json'), 'utf-8')
12-
const angularJson = JSON.parse(angularJsonFile)
13-
return angularJson
11+
return readJsonSync(join(siteRoot, 'angular.json'))
1412
} catch (error) {
1513
return failBuild(`Could not parse contents of angular.json`)
1614
}

src/helpers/setUpBuilderFunction.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
const { existsSync } = require('fs')
21
const { join } = require('path')
32

4-
const { copySync, removeSync } = require('fs-extra')
3+
const { copySync } = require('fs-extra')
54

65
const setUpBuilderFunction = ({ FUNCTIONS_SRC }) => {
76
const TEMPLATES_DIR = join('..', 'src', 'templates')
87
const FUNCTIONS_DEST = join(FUNCTIONS_SRC, 'angular-builder.js')
9-
if (existsSync(FUNCTIONS_DEST)) {
10-
removeSync(FUNCTIONS_DEST)
11-
}
8+
129
copySync(join(TEMPLATES_DIR, 'angularBuilder.js'), join(FUNCTIONS_SRC, 'angular-builder.js'), {
13-
overwrite: false,
10+
overwrite: true,
1411
dereference: true,
1512
})
1613
}

src/helpers/setUpFunctionsConfig.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Modify [functions] of netlify config
22
const setUpFunctionsConfig = function ({ netlifyConfig }) {
3-
// netlifyConfig.functions.node_bundler = 'esbuild'
4-
// const includedDist = 'dist/netlify-serverless'
5-
// if (Array.isArray(netlifyConfig.functions.included_files)) {
6-
// netlifyConfig.functions.included_files.push(includedDist)
7-
// } else {
8-
// netlifyConfig.functions.included_files = [includedDist]
9-
// }
3+
netlifyConfig.functions.node_bundler = 'esbuild'
4+
const includedDist = 'dist/netlify-serverless'
5+
if (Array.isArray(netlifyConfig.functions.included_files)) {
6+
netlifyConfig.functions.included_files.push(includedDist)
7+
} else {
8+
netlifyConfig.functions.included_files = [includedDist]
9+
}
1010
}
1111

1212
module.exports = setUpFunctionsConfig

src/helpers/setUpRedirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const setUpRedirects = function ({ netlifyConfig }) {
66
}
77
netlifyConfig.redirects.push({
88
from: '/*',
9-
to: '/.netlify/functions/angular_builder',
9+
to: '/.netlify/functions/angular-builder',
1010
status: '200',
1111
})
1212
}

src/helpers/updateAngularJson.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { join } = require('path')
2+
3+
const { writeJsonSync } = require('fs-extra')
4+
5+
// Add necessary serverless script to angular.json
6+
const updateAngularJson = function ({ failBuild, angularJson, projectName, siteRoot }) {
7+
if (!projectName) {
8+
return failBuild('No value defined for projectName. Check plugin inputs in netlify.toml.')
9+
}
10+
11+
if (!angularJson.projects[projectName]) {
12+
return failBuild(`No project found in angular.json by the name of: ${projectName}.`)
13+
}
14+
15+
try {
16+
angularJson.projects[projectName].architect.serverless = {
17+
builder: '@angular-devkit/build-angular:server',
18+
options: {
19+
outputPath: `dist/netlify-serverless/serverless`,
20+
main: 'serverless.ts',
21+
tsConfig: 'tsconfig.serverless.json',
22+
},
23+
configurations: {
24+
production: {
25+
outputHashing: 'media',
26+
fileReplacements: [
27+
{
28+
replace: 'src/environments/environment.ts',
29+
with: 'src/environments/environment.prod.ts',
30+
},
31+
],
32+
sourceMap: false,
33+
optimization: true,
34+
},
35+
},
36+
}
37+
const angularJsonPath = join(siteRoot, 'angular.json')
38+
writeJsonSync(angularJsonPath, angularJson)
39+
} catch (error) {
40+
return failBuild(`Could not update angular.json with serverless project configuration`)
41+
}
42+
}
43+
44+
module.exports = updateAngularJson

src/helpers/validateSchematicWasRun.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,40 @@ const getAngularJson = require('./helpers/getAngularJson')
44
const setUpBuilderFunction = require('./helpers/setUpBuilderFunction')
55
const setUpFunctionsConfig = require('./helpers/setUpFunctionsConfig')
66
const setUpRedirects = require('./helpers/setUpRedirects')
7+
const updateAngularJson = require('./helpers/updateAngularJson')
78
const validateAngularUniversalUsage = require('./helpers/validateAngularUniversalUsage')
89
const validateNetlifyConfig = require('./helpers/validateNetlifyConfig')
9-
const validateSchematicWasRun = require('./helpers/validateSchematicWasRun')
1010

1111
// Currently unsupported:
1212
// - monorepo structures
1313

1414
module.exports = {
15-
async onPreBuild({ netlifyConfig, utils, inputs }) {
15+
async onPreBuild({ netlifyConfig, utils }) {
1616
const { failBuild } = utils.build
17-
const { projectName } = inputs
1817

1918
validateAngularUniversalUsage({ failBuild })
2019

21-
const angularJson = getAngularJson({ failBuild })
20+
const siteRoot = process.cwd()
21+
const angularJson = getAngularJson({ failBuild, siteRoot })
22+
const projectName = angularJson.defaultProject
2223

23-
validateSchematicWasRun({ failBuild, angularJson, projectName })
24+
updateAngularJson({ failBuild, angularJson, projectName, siteRoot })
2425

2526
validateNetlifyConfig({ failBuild, netlifyConfig, projectName })
2627

2728
addAngularServerlessFiles()
2829
},
29-
async onBuild({ netlifyConfig, constants: { FUNCTIONS_SRC = 'netlify/functions' }, inputs }) {
30-
copyProjectDist({ projectName: inputs.projectName })
30+
async onBuild({ utils, netlifyConfig, constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = 'netlify/functions' } }) {
31+
const { failBuild } = utils.build
32+
const siteRoot = process.cwd()
33+
const angularJson = getAngularJson({ failBuild, siteRoot })
34+
35+
copyProjectDist({ projectName: angularJson.defaultProject })
3136

3237
setUpRedirects({ netlifyConfig })
3338

3439
setUpFunctionsConfig({ netlifyConfig })
3540

36-
setUpBuilderFunction({ FUNCTIONS_SRC })
41+
setUpBuilderFunction({ FUNCTIONS_SRC: INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC })
3742
},
3843
}

src/templates/serverless.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ const indexHtml = existsSync(join(distFolder, 'index.original.html'))
1717
? 'index.original.html'
1818
: 'index'
1919

20-
console.log('TESTING TESTING TESTING TESTING')
21-
console.log({ rootFolder, distFolder, indexHtml, cwd: process.cwd() })
22-
console.log('EXISTS', existsSync(join(distFolder, 'index.html')))
23-
2420
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
2521
app.engine(
2622
'html',
@@ -40,20 +36,6 @@ app.get('*', (req, res) => {
4036
res,
4137
req,
4238
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
43-
},
44-
(err: Error, html: string) => {
45-
console.log({ err });
46-
res
47-
.status(html ? 200 : 500)
48-
.send(
49-
html +
50-
`<!--${process.cwd()} ${readdirSync(process.cwd())} ${
51-
existsSync(distFolder)
52-
? readdirSync(distFolder)
53-
: distFolder + `does not exist`
54-
} -->` ||
55-
err.message + JSON.stringify(process.env) + ' cwd ' + process.cwd()
56-
)
5739
}
5840
)
5941
})

0 commit comments

Comments
 (0)