Skip to content

Commit e67a2b2

Browse files
authored
Update Node.js project templates (#4149)
1. Turn on http streaming by default for v4 model 2. Update default Node types for TypeScript projects (Node 20 has been GA for a few months now) 3. Support hot reload for debugging by default
1 parent 68eaf6b commit e67a2b2

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

src/commands/createNewProject/ProjectCreateStep/JavaScriptProjectCreateStep.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ export const azureFunctionsDependencyVersion: string = '^4.0.0';
2121

2222
export class JavaScriptProjectCreateStep extends ScriptProjectCreateStep {
2323
protected gitignore: string = nodeGitignore;
24+
protected functionSubpath: string;
25+
protected shouldAddIndexFile: boolean;
2426

2527
constructor() {
2628
super();
2729
this.funcignore.push('*.js.map', '*.ts', 'tsconfig.json');
30+
// default functionSubpath value is a string
31+
this.functionSubpath = getWorkspaceSetting(functionSubpathSetting) as string;
32+
// index file only supported when using default folder structure
33+
this.shouldAddIndexFile = this.functionSubpath === 'src/functions';
2834
}
2935

3036
public async executeCore(context: IProjectWizardContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
@@ -35,6 +41,10 @@ export class JavaScriptProjectCreateStep extends ScriptProjectCreateStep {
3541
await AzExtFsExtra.writeJSON(packagePath, this.getPackageJson(context));
3642
}
3743
await this._installDependencies(context.projectPath);
44+
45+
if (isNodeV4Plus(context) && this.shouldAddIndexFile) {
46+
await this.addIndexFile(context);
47+
}
3848
}
3949

4050
private async _installDependencies(projectPath: string): Promise<void> {
@@ -56,9 +66,11 @@ export class JavaScriptProjectCreateStep extends ScriptProjectCreateStep {
5666
};
5767

5868
if (isNodeV4Plus(context)) {
59-
// default functionSubpath value is a string
60-
const functionSubpath: string = getWorkspaceSetting(functionSubpathSetting) as string;
61-
packageJson.main = path.posix.join(functionSubpath, '*.js');
69+
if (this.shouldAddIndexFile) {
70+
packageJson.main = 'src/{index.js,functions/*.js}';
71+
} else {
72+
packageJson.main = path.posix.join(this.functionSubpath, '*.js');
73+
}
6274
}
6375

6476
return packageJson;
@@ -83,6 +95,18 @@ export class JavaScriptProjectCreateStep extends ScriptProjectCreateStep {
8395
protected getPackageJsonDevDeps(_context: IProjectWizardContext): { [key: string]: string } {
8496
return {};
8597
}
98+
99+
protected async addIndexFile(context: IProjectWizardContext): Promise<void> {
100+
const indexPath: string = path.join(context.projectPath, 'src', 'index.js');
101+
if (await confirmOverwriteFile(context, indexPath)) {
102+
await AzExtFsExtra.writeFile(indexPath, `const { app } = require('@azure/functions');
103+
104+
app.setup({
105+
enableHttpStream: true,
106+
});
107+
`);
108+
}
109+
}
86110
}
87111

88112
/**

src/commands/createNewProject/ProjectCreateStep/TypeScriptProjectCreateStep.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import { AzExtFsExtra } from '@microsoft/vscode-azext-utils';
77
import * as path from 'path';
88
import { type Progress } from 'vscode';
99
import { FuncVersion } from '../../../FuncVersion';
10-
import { functionSubpathSetting, tsConfigFileName, tsDefaultOutDir } from '../../../constants';
10+
import { tsConfigFileName, tsDefaultOutDir } from '../../../constants';
1111
import { localize } from '../../../localize';
1212
import { confirmOverwriteFile } from '../../../utils/fs';
1313
import { isNodeV4Plus } from '../../../utils/programmingModelUtils';
14-
import { getWorkspaceSetting } from '../../../vsCodeConfig/settings';
1514
import { type IProjectWizardContext } from '../IProjectWizardContext';
1615
import { JavaScriptProjectCreateStep } from './JavaScriptProjectCreateStep';
1716

@@ -38,11 +37,12 @@ export class TypeScriptProjectCreateStep extends JavaScriptProjectCreateStep {
3837
protected getPackageJson(context: IProjectWizardContext): { [key: string]: unknown } {
3938
const packageJson = super.getPackageJson(context);
4039
if (isNodeV4Plus(context)) {
41-
// default functionSubpath value is a string
42-
const functionSubpath: string = getWorkspaceSetting(functionSubpathSetting) as string;
43-
4440
// this is set in the super class, but we want to override it
45-
packageJson.main = path.posix.join('dist', functionSubpath, '*.js');
41+
if (this.shouldAddIndexFile) {
42+
packageJson.main = 'dist/src/{index.js,functions/*.js}';
43+
} else {
44+
packageJson.main = path.posix.join('dist', this.functionSubpath, '*.js');
45+
}
4646
}
4747

4848
return packageJson;
@@ -71,7 +71,7 @@ export class TypeScriptProjectCreateStep extends JavaScriptProjectCreateStep {
7171
switch (context.version) {
7272
case FuncVersion.v4:
7373
funcTypesVersion = '3';
74-
nodeTypesVersion = isNodeV4Plus(context) ? '18' : '16';
74+
nodeTypesVersion = '20';
7575
break;
7676
case FuncVersion.v3:
7777
funcTypesVersion = '2';
@@ -96,4 +96,16 @@ export class TypeScriptProjectCreateStep extends JavaScriptProjectCreateStep {
9696
devDeps['rimraf'] = '^5.0.0';
9797
return devDeps;
9898
}
99+
100+
protected async addIndexFile(context: IProjectWizardContext): Promise<void> {
101+
const indexPath: string = path.join(context.projectPath, 'src', 'index.ts');
102+
if (await confirmOverwriteFile(context, indexPath)) {
103+
await AzExtFsExtra.writeFile(indexPath, `import { app } from '@azure/functions';
104+
105+
app.setup({
106+
enableHttpStream: true,
107+
});
108+
`);
109+
}
110+
}
99111
}

src/commands/initProjectForVSCode/InitVSCodeStep/TypeScriptInitVSCodeStep.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { JavaScriptInitVSCodeStep } from "./JavaScriptInitVSCodeStep";
1515
const npmPruneTaskLabel: string = convertToFunctionsTaskLabel('npm prune');
1616
const npmInstallTaskLabel: string = convertToFunctionsTaskLabel('npm install');
1717
const npmBuildTaskLabel: string = convertToFunctionsTaskLabel('npm build');
18+
const npmWatchTaskLabel: string = convertToFunctionsTaskLabel('npm watch');
1819
const npmCleanTaskLabel: string = convertToFunctionsTaskLabel('npm clean');
1920

2021
export class TypeScriptInitVSCodeStep extends JavaScriptInitVSCodeStep {
@@ -41,7 +42,7 @@ export class TypeScriptInitVSCodeStep extends JavaScriptInitVSCodeStep {
4142
command: hostStartCommand,
4243
problemMatcher: getFuncWatchProblemMatcher(language),
4344
isBackground: true,
44-
dependsOn: npmBuildTaskLabel
45+
dependsOn: npmWatchTaskLabel
4546
},
4647
{
4748
type: 'shell',
@@ -50,6 +51,18 @@ export class TypeScriptInitVSCodeStep extends JavaScriptInitVSCodeStep {
5051
dependsOn: this.hasCleanScript ? npmCleanTaskLabel : installDependsOn,
5152
problemMatcher: '$tsc'
5253
},
54+
{
55+
type: 'shell',
56+
label: npmWatchTaskLabel,
57+
command: 'npm run watch',
58+
dependsOn: this.hasCleanScript ? npmCleanTaskLabel : installDependsOn,
59+
problemMatcher: "$tsc-watch",
60+
group: {
61+
kind: "build",
62+
isDefault: true
63+
},
64+
isBackground: true
65+
},
5366
{
5467
type: 'shell',
5568
label: npmInstallTaskLabel,

src/debug/NodeDebugProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const nodeDebugConfig: DebugConfiguration = {
1414
name: localize('attachNode', 'Attach to Node Functions'),
1515
type: 'node',
1616
request: 'attach',
17+
restart: true,
1718
port: defaultNodeDebugPort,
1819
preLaunchTask: hostStartTaskName
1920
};

test/project/validateProject.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function getTypeScriptValidateOptions(options?: { version?: FuncVersion,
6969
],
7070
expectedTasks: [
7171
'npm build (functions)',
72+
'npm watch (functions)',
7273
'npm install (functions)',
7374
'npm prune (functions)',
7475
'host start'

0 commit comments

Comments
 (0)