Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular v17 support #6480

Merged
merged 17 commits into from
Nov 17, 2023
58 changes: 49 additions & 9 deletions src/frameworks/angular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@

const DEFAULT_BUILD_SCRIPT = ["ng build"];

/**
*
*/
export async function discover(dir: string): Promise<Discovery | undefined> {
if (!(await pathExists(join(dir, "package.json")))) return;
if (!(await pathExists(join(dir, "angular.json")))) return;
return { mayWantBackend: true, publicDirectory: join(dir, "src", "assets") };
}

/**
*
*/
export async function init(setup: any, config: any) {

Check warning on line 51 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Missing return type on function

Check warning on line 51 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type

Check warning on line 51 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected any. Specify a different type
execSync(
`npx --yes -p @angular/cli@latest ng new ${setup.projectId} --directory ${setup.hosting.source} --skip-git`,

Check warning on line 53 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Invalid type "any" of template literal expression

Check warning on line 53 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .projectId on an `any` value

Check warning on line 53 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Invalid type "any" of template literal expression

Check warning on line 53 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .hosting on an `any` value
{
stdio: "inherit",
cwd: config.projectDir,

Check warning on line 56 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe assignment of an `any` value

Check warning on line 56 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe member access .projectDir on an `any` value
}
);
const useAngularUniversal = await promptOnce({
Expand All @@ -59,18 +65,21 @@
if (useAngularUniversal) {
execSync("ng add @nguniversal/express-engine --skip-confirmation", {
stdio: "inherit",
cwd: join(config.projectDir, setup.hosting.source),

Check warning on line 68 in src/frameworks/angular/index.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unsafe argument of type `any` assigned to a parameter of type `string`
});
}
}

/**
*
*/
export async function build(dir: string, configuration: string): Promise<BuildResult> {
Copy link
Contributor

@9kubczas4 9kubczas4 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should mark useAngularUniversal in the init function as legacy?

  const useAngularUniversal = await promptOnce({
    name: "useAngularUniversal",
    type: "confirm",
    default: false,
    message: `Would you like to setup Angular Universal?`,
  });
  if (useAngularUniversal) {
    execSync("ng add @nguniversal/express-engine --skip-confirmation", {
      stdio: "inherit",
      cwd: join(config.projectDir, setup.hosting.source),
    });
  }

Cause NG CLI in v17 is asking about Do you want to enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I should rework the bootstrap. It should be part of Angular's create routine now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are using @angular/cli@latest, @nguniversal/express-engine should not be used as this is only supported for version 16 and before.

const {
targets,
serverTarget,
serveOptimizedImages,
locales,
baseHref: baseUrl,
ssr,
} = await getBuildConfig(dir, configuration);
await warnIfCustomBuildScript(dir, name, DEFAULT_BUILD_SCRIPT);
for (const target of targets) {
Expand All @@ -84,8 +93,8 @@
if (result.status !== 0) throw new FirebaseError(`Unable to build ${target}`);
}

const wantsBackend = !!serverTarget || serveOptimizedImages;
const rewrites = serverTarget
const wantsBackend = ssr || serveOptimizedImages;
const rewrites = ssr
? []
: [
{
Expand All @@ -97,6 +106,9 @@
return { wantsBackend, i18n, rewrites, baseUrl };
}

/**
*
*/
export async function getDevModeHandle(dir: string, configuration: string) {
const { targetStringFromTarget } = relativeRequire(dir, "@angular-devkit/architect");
const { serveTarget } = await getContext(dir, configuration);
Expand All @@ -121,6 +133,9 @@
return simpleProxy(await host);
}

/**
*
*/
export async function ɵcodegenPublicDirectory(
sourceDir: string,
destDir: string,
Expand All @@ -146,11 +161,18 @@
}
}

/**
*
*/
export async function getValidBuildTargets(purpose: BUILD_TARGET_PURPOSE, dir: string) {
const validTargetNames = new Set(["development", "production"]);
try {
const { workspaceProject, browserTarget, serverTarget, serveTarget } = await getContext(dir);
const { target } = ((purpose === "emulate" && serveTarget) || serverTarget || browserTarget)!;
const { workspaceProject, buildTarget, browserTarget, serverTarget, serveTarget } =
await getContext(dir);
const { target } = ((purpose === "emulate" && serveTarget) ||
buildTarget ||
serverTarget ||
browserTarget)!;
const workspaceTarget = workspaceProject.targets.get(target)!;
Object.keys(workspaceTarget.configurations || {}).forEach((it) => validTargetNames.add(it));
} catch (e) {
Expand All @@ -160,12 +182,18 @@
return [...validTargetNames, ...allTargets];
}

/**
*
*/
export async function shouldUseDevModeHandle(targetOrConfiguration: string, dir: string) {
const { serveTarget } = await getContext(dir, targetOrConfiguration);
if (!serveTarget) return false;
return serveTarget.configuration !== "production";
}

/**
*
*/
export async function ɵcodegenFunctionsDirectory(
sourceDir: string,
destDir: string,
Expand All @@ -182,6 +210,7 @@
externalDependencies,
baseHref,
serveOptimizedImages,
serverEntry,
} = await getServerConfig(sourceDir, configuration);

const dotEnv = { __NG_BROWSER_OUTPUT_PATH__: browserOutputPath };
Expand Down Expand Up @@ -232,14 +261,25 @@
if (localizedApps.has(locale)) {
localizedApps.get(locale)(req,res);
} else {
const app = require(\`./${serverOutputPath}/\${locale}/main.js\`).app(locale);
localizedApps.set(locale, app);
app(req,res);
${
serverEntry?.endsWith(".mjs")
? `import(\`./${serverOutputPath}/\${locale}/${serverEntry}\`)`
: `Promise.resolve(require(\`./${serverOutputPath}/\${locale}/${serverEntry}\`))`
}.then(server => {
const app = server.app(locale);
localizedApps.set(locale, app);
app(req,res);
});
}
});
};\n`;
} else if (serverOutputPath) {
bootstrapScript = `exports.handle = require('./${serverOutputPath}/main.js').app();\n`;
bootstrapScript = `const app = ${
serverEntry?.endsWith(".mjs")
? `import(\`./${serverOutputPath}/${serverEntry}\`)`
: `Promise.resolve(require('./${serverOutputPath}/${serverEntry}'))`
}.then(server => server.app());
exports.handle = (req,res) => app.then(it => it(req,res));\n`;
} else {
bootstrapScript = `exports.handle = (res, req) => req.sendStatus(404);\n`;
rewriteSource = posix.join(baseHref, "__image__");
Expand Down
Loading
Loading