Skip to content

Commit

Permalink
feat(nx-plugin): add init schematic/generator for migrating an Angula…
Browse files Browse the repository at this point in the history
…r SPA to Analog (#1049)
  • Loading branch information
brandonroberts committed Apr 25, 2024
1 parent c45cad0 commit ca2b3df
Show file tree
Hide file tree
Showing 29 changed files with 741 additions and 80 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

[![Discord server](https://dcbadge.vercel.app/api/server/mKC2Ec48U5?style=flat-square)](https://chat.analogjs.org/) [![Twitter](https://img.shields.io/twitter/follow/analogjs)](https://twitter.com/analogjs)

Analog is a meta-framework for building applications and websites with [Angular](https://angular.dev).
Analog is the meta-framework for building applications and websites with [Angular](https://angular.dev).

Similar to other meta-frameworks such as Next.JS, Nuxt, SvelteKit and others, Analog provides a similar experience, building on top of Angular.
Similar to other meta-frameworks such as Next.JS, Nuxt, SvelteKit, Qwik City, and others, Analog provides a similar experience, building on top of Angular.

## Documentation

Expand All @@ -19,32 +19,40 @@ Visit the docs at [https://analogjs.org](https://analogjs.org)
## Features

- Supports Vite/Vitest/Playwright
- Server and deployment integrations powered by [Nitro](https://nitro.unjs.io)
- File-based routing
- Server-side data fetching
- Support for using markdown as content routes
- Support for API/server routes
- Integrated API/server routes
- Hybrid SSR/SSG support
- Supports Angular CLI or Nx workspaces
- Supports Angular CLI and Nx workspaces

## Getting Started

Use your package manager of choice to create a new project

With yarn:
With npm:

```sh
yarn create analog
npm create analog@latest
```

With npm:
With pnpm:

```sh
npm init analog
pnpm create analog@latest
```

With pnpm:
With Yarn:

```sh
pnpm create analog
yarn create analog@latest
```

With Bun:

```sh
bun create analog@latest
```

Follow the prompts to scaffold the project and start the development server.
Expand All @@ -53,9 +61,10 @@ Follow the prompts to scaffold the project and start the development server.

## Supporting Analog

Analog is an MIT-licensed open source project with its ongoing development made possible by contributors and sponsors.

[Become a Sponsor!](https://github.com/sponsors/brandonroberts).
- Star the [GitHub Repo](https://github.com/analogjs/analog)
- Join the [Discord](https://chat.analogjs.org)
- Follow us on [Twitter](https://twitter.com/analogjs)
- Become a [Sponsor](https://analogjs.org/docs/sponsoring)

### Sponsors

Expand Down
70 changes: 70 additions & 0 deletions apps/docs-app/docs/guides/migrating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Migrating an Angular app to Analog

An existing Angular Single Page Application can be configured to use Analog using a schematic/generator for Angular CLI or Nx workspaces.

## Using a Schematic/Generator

First, install the `@analogjs/platform` package:

<Tabs groupId="package-manager">
<TabItem value="npm">

```shell
npm install @analogjs/platform --save-dev
```

</TabItem>

<TabItem label="Yarn" value="yarn">

```shell
yarn add @analogjs/platform --dev
```

</TabItem>

<TabItem value="pnpm">

```shell
pnpm install -w @analogjs/platform
```

</TabItem>
</Tabs>

Next, run the command to set up the Vite config, update the build/serve targets in the project configuration, move necessary files, and optionally set up Vitest for unit testing.

```shell
npx ng generate @analogjs/platform:init --project [your-project-name]
```

For Nx projects:

```shell
npx nx generate @analogjs/platform:init --project [your-project-name]
```

## Updating Global Styles and Scripts

If you have any global scripts or styles configured in the `angular.json`, move them inside the `head` tag in the `index.html`.

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Analog app</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="/src/styles.css" />
</head>
<body>
<app-root></app-root>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
```
13 changes: 11 additions & 2 deletions apps/docs-app/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ Similar to other meta-frameworks such as Next.JS, Nuxt, SvelteKit, Qwik City, an
## Features

- Supports Vite/Vitest/Playwright
- Server and deployment integrations powered by [Nitro](https://nitro.unjs.io)
- [File-based routing](/docs/features/routing/overview)
- [Support for using markdown as content routes](/docs/features/routing/content)
- [Support for API/server routes](/docs/features/api/overview)
- [Server-side data fetching](/docs/features/data-fetching/server-side-data-fetching.md)
- [Support for using Markdown as content routes](/docs/features/routing/content)
- [API/server routes](/docs/features/api/overview)
- Hybrid [SSR](/docs/features/server/server-side-rendering)/[SSG support](/docs/features/server/static-site-generation)
- Supports Angular CLI/[Nx workspaces](/docs/integrations/nx)
- Supports [Angular components](/docs/packages/astro-angular/overview) with Astro

## Support Us

- Visit and Star the [GitHub Repo](https://github.com/analogjs/analog)
- Join the [Discord](https://chat.analogjs.org)
- Follow us on [Twitter](https://twitter.com/analogjs)
- Become a [Sponsor](/docs/sponsoring)
11 changes: 11 additions & 0 deletions apps/docs-app/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ const sidebars = {
},
],
},
{
type: 'category',
label: 'Guides',
items: [
{
type: 'doc',
id: 'guides/migrating',
label: 'Migrating an Angular app to Analog',
},
],
},
{
type: 'category',
label: 'Integrations',
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"factory": "./src/generators/setup-vitest/generator",
"schema": "./src/generators/setup-vitest/schema.json",
"description": "Setup the Vitest configuration."
},
"init": {
"factory": "./src/generators/init/generator",
"schema": "./src/generators/init/schema.json",
"description": "Setup the Analog configuration."
}
},
"schematics": {
Expand All @@ -43,6 +48,11 @@
"factory": "./src/generators/setup-vitest/compat",
"schema": "./src/generators/setup-vitest/schema.json",
"description": "Setup the Vitest configuration."
},
"init": {
"factory": "./src/generators/init/compat",
"schema": "./src/generators/init/schema.json",
"description": "Setup the Analog configuration."
}
}
}
54 changes: 11 additions & 43 deletions packages/nx-plugin/src/executors/vitest/schema.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,34 @@
{
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/schema",
"version": 2,
"cli": "nx",
"title": "Vitest executor",
"description": "Test using Vitest.",
"type": "object",
"properties": {
"config": {
"configFile": {
"type": "string",
"description": "The path to the local vitest config",
"x-completion-type": "file",
"x-completion-glob": "@(vitest|vite).config@(.js|.ts)"
"x-completion-glob": "@(vitest|vite).config@(.js|.ts)",
"aliases": ["config"]
},
"passWithNoTests": {
"type": "boolean",
"default": true,
"description": "Pass the test even if no tests are found"
},
"testNamePattern": {
"reportsDirectory": {
"type": "string",
"description": "Run tests with full names matching the pattern"
"description": "Directory to write coverage report to."
},
"mode": {
"type": "string",
"enum": ["test", "benchmark", "typecheck"],
"default": "test",
"description": "The mode that vitest will run on",
"x-priority": "important"
},
"watch": {
"type": "boolean",
"default": false,
"description": "Enable watch mode"
},
"reporters": {
"type": "array",
"items": {
"type": "string"
},
"description": "An array of reporters to pass to vitest"
},
"update": {
"type": "boolean",
"default": false,
"alias": "u",
"description": "Update snapshots",
"x-priority": "important"
},
"coverage": {
"type": "boolean",
"default": false,
"description": "Enable coverage report",
"x-priority": "important"
},
"reportsDirectory": {
"type": "string",
"description": "Directory to write coverage report to."
"description": "Mode for Vite."
},
"testFiles": {
"aliases": ["testFile"],
"type": "array",
"items": { "type": "string" }
},
"watch": {
"description": "Watch files for changes and rerun tests related to changed files.",
"type": "boolean"
}
},
"required": [],
Expand Down
5 changes: 5 additions & 0 deletions packages/nx-plugin/src/generators/init/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convertNxGenerator } from '@nx/devkit';

import setupAnalogGenerator from './generator';

export default convertNxGenerator(setupAnalogGenerator);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// <reference types="vitest" />

import { defineConfig } from 'vite';
import analog from '@analogjs/platform';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
root: __dirname,
public: 'src/assets',
cacheDir: '<%= offsetFromRoot %>node_modules/.vite',
build: {
outDir: '<%= offsetFromRoot %>dist/<%= projectRoot %>/client',
reportCompressedSize: true,
commonjsOptions: { transformMixedEsModules: true },
target: ['es2020'],
},
plugins: [
analog({
ssr: false,
static: true,
prerender: {
routes: []
}
}),
nxViteTsPaths(),
],
server: {
fs: {
allow: ['.']
}
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['src/test-setup.ts'],
include: ['**/*.spec.ts'],
reporters: ['default'],
},
define: {
'import.meta.vitest': mode !== 'production',
},
};
});

0 comments on commit ca2b3df

Please sign in to comment.