Skip to content

Commit

Permalink
feat: introduce @analogjs/platform package for initial support for AP…
Browse files Browse the repository at this point in the history
…I routes (analogjs#132)

Closes analogjs#6
  • Loading branch information
profanis authored and Villanuevand committed Sep 12, 2023
1 parent d2395a6 commit 3e12f62
Show file tree
Hide file tree
Showing 21 changed files with 1,206 additions and 38 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ stats.html

**/test-results/
**/playwright-report/
**/playwright/.cache/
**/playwright/.cache/

# Nitro
.nitro
1 change: 1 addition & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"create-analog": "packages/create-analog",
"docs-app": "apps/docs-app",
"router": "packages/router",
"platform": "packages/platform",
"vite-plugin-angular": "packages/vite-plugin-angular"
}
}
1 change: 1 addition & 0 deletions apps/analog-app/src/server/routes/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'Analog.js is amazing!';
1 change: 1 addition & 0 deletions apps/analog-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"exclude": ["./src/server/**/*"],
"references": [
{
"path": "./tsconfig.app.json"
Expand Down
12 changes: 10 additions & 2 deletions apps/analog-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/// <reference types="vitest" />

import { defineConfig, Plugin, splitVendorChunkPlugin } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
import platform from '@analogjs/platform';
import angular from '@analogjs/vite-plugin-angular';
import { offsetFromRoot } from '@nrwl/devkit';
import { visualizer } from 'rollup-plugin-visualizer';
import { defineConfig, Plugin, splitVendorChunkPlugin } from 'vite';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
Expand All @@ -29,6 +30,13 @@ export default defineConfig(({ mode }) => {
}),
visualizer() as Plugin,
splitVendorChunkPlugin(),
platform({
nitro: {
output: {
dir: `${offsetFromRoot('apps/analog-app/src/server')}/dist/server`,
},
},
}),
],
test: {
globals: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"kolorist": "^1.6.0",
"lint-staged": "^13.0.3",
"minimist": "^1.2.7",
"nitropack": "^0.5.4",
"nx": "14.8.4",
"playwright": "^1.27.1",
"prettier": "^2.7.1",
Expand Down
18 changes: 18 additions & 0 deletions packages/platform/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions packages/platform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# analog-platform

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build analog-platform` to build the library.

## Running unit tests

Run `nx test analog-platform` to execute the unit tests via [Jest](https://jestjs.io).
8 changes: 8 additions & 0 deletions packages/platform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@analogjs/platform",
"version": "0.1.0-alpha.0",
"type": "commonjs",
"peerDependencies": {
"nitropack": "^0.5.4"
}
}
42 changes: 42 additions & 0 deletions packages/platform/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/platform/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "node_modules/@analogjs/platform",
"main": "packages/platform/src/index.ts",
"tsConfig": "packages/platform/tsconfig.lib.json",
"assets": ["packages/platform/*.md"]
},
"configurations": {
"production": {
"tsConfig": "packages/platform/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "packages/platform/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/platform/**/*.ts"]
}
},
"test": {
"executor": "nx:run-commands",
"outputs": ["coverage/packages/platform"],
"options": {
"command": "vitest",
"cwd": "packages/platform"
}
}
},
"tags": []
}
3 changes: 3 additions & 0 deletions packages/platform/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { analogPlatform } from './lib/analog-platform-plugin';

export default analogPlatform;
31 changes: 31 additions & 0 deletions packages/platform/src/lib/analog-platform-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it, vi } from 'vitest';
import { analogPlatform } from './analog-platform-plugin';

const mockViteDevServer = {
middlewares: {
// eslint-disable-next-line @typescript-eslint/no-empty-function
use: () => {},
},
};

describe('analogPlatformVitePlugin', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('should work', () => {
expect(analogPlatform({}).name).toEqual('vite-nitro-plugin');
});

it(`should have the route middleware "/api" `, async () => {
// Arrange
const spy = vi.spyOn(mockViteDevServer.middlewares, 'use');

// Act
await (analogPlatform({}).configureServer as any)(mockViteDevServer);

// Assert
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('/api', expect.anything());
});
});
43 changes: 43 additions & 0 deletions packages/platform/src/lib/analog-platform-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { loadEsmModule } from '@angular-devkit/build-angular/src/utils/load-esm';
import { NitroConfig } from 'nitropack';
import { Plugin, ViteDevServer } from 'vite';

export function analogPlatform(opts: { nitro?: NitroConfig }): Plugin {
const nitroConfig: NitroConfig = {
rootDir: 'src',
srcDir: 'src/server',
scanDirs: ['src/server'],
output: {
dir: '../dist/server',
...opts.nitro?.output,
},
};
return {
name: 'vite-nitro-plugin',

async configureServer(viteServer: ViteDevServer) {
const { createNitro, createDevServer, build } = await loadEsmModule<
typeof import('nitropack')
>('nitropack');

const nitro = await createNitro({ ...nitroConfig, dev: true });
const server = createDevServer(nitro);
await build(nitro);
viteServer.middlewares.use('/api', await server.app);
console.log(
`\n\nThe '@analogjs/platform' successfully started.\nThe server endpoints are accessible under the "/api"`
);
},

async closeBundle() {
const { createNitro, build } = await loadEsmModule<
typeof import('nitropack')
>('nitropack');

const nitro = await createNitro({ ...nitroConfig, dev: false });
await build(nitro);
await nitro.close();
console.log(`\n\nThe '@analogjs/platform' successfully built.`);
},
};
}
Empty file.
23 changes: 23 additions & 0 deletions packages/platform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"target": "es2020",
"module": "es2020",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/platform/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": []
},
"include": ["**/*.ts"],
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"]
}
9 changes: 9 additions & 0 deletions packages/platform/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"compilationMode": "partial"
}
}
9 changes: 9 additions & 0 deletions packages/platform/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["vitest/globals", "node"]
},
"include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
}
23 changes: 23 additions & 0 deletions packages/platform/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference types="vitest" />

import { offsetFromRoot } from '@nrwl/devkit';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
root: 'src',
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['test-setup.ts'],
include: ['**/*.spec.ts'],
cache: {
dir: `${offsetFromRoot('packages/platform/src')}/node_modules/.vitest`,
},
},
define: {
'import.meta.vitest': mode !== 'production',
},
};
});
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"paths": {
"@analogjs/astro-angular": ["packages/astro-angular/src/index.ts"],
"@analogjs/router": ["packages/router/src/index.ts"],
"@analogjs/platform": ["packages/platform/src/index.ts"],
"@analogjs/vite-plugin-angular": [
"packages/vite-plugin-angular/src/index.ts"
]
Expand Down
Loading

0 comments on commit 3e12f62

Please sign in to comment.