Skip to content

Commit

Permalink
refactor: ssg filter option (QwikDev#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley authored Jan 9, 2023
1 parent 3ce3ba0 commit b24a65e
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 216 deletions.
6 changes: 2 additions & 4 deletions packages/qwik-city/adaptors/azure-swa/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
```ts

import type { StaticGenerateRenderOptions } from '../../../static';
import { ServerAdaptorOptions } from '../../shared/vite';

// @alpha (undocumented)
export function azureSwaAdaptor(opts?: AzureSwaAdaptorOptions): any;

// @alpha (undocumented)
export interface AzureSwaAdaptorOptions {
// (undocumented)
staticGenerate?: StaticGenerateRenderOptions | true;
export interface AzureSwaAdaptorOptions extends ServerAdaptorOptions {
}

// (No @packageDocumentation comment for this package)
Expand Down
163 changes: 48 additions & 115 deletions packages/qwik-city/adaptors/azure-swa/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,64 @@
import type { Plugin } from 'vite';
import type { QwikVitePlugin } from '@builder.io/qwik/optimizer';
import type { StaticGenerateOptions, StaticGenerateRenderOptions } from '../../../static';
import { ServerAdaptorOptions, viteAdaptor } from '../../shared/vite';
import { join } from 'node:path';
import fs from 'node:fs';

/**
* @alpha
*/
export function azureSwaAdaptor(opts: AzureSwaAdaptorOptions = {}): any {
let qwikVitePlugin: QwikVitePlugin | null = null;
let serverOutDir: string | null = null;
let renderModulePath: string | null = null;
let qwikCityPlanModulePath: string | null = null;
let azureSwaModulePath: string | null = null;
return viteAdaptor({
name: 'azure-swa',
origin: process?.env?.URL || 'https://yoursitename.region.2.azurestaticapps.net',
staticGenerate: opts.staticGenerate,
ssg: opts.ssg,
cleanStaticGenerated: true,

async function generateBundles() {
const qwikVitePluginApi = qwikVitePlugin!.api;
const clientOutDir = qwikVitePluginApi.getClientOutDir()!;
async generate({ outputEntries, serverOutDir }) {
const serverPackageJsonPath = join(serverOutDir!, 'package.json');
const serverPackageJsonCode = `{"type":"module"}`;
await fs.promises.mkdir(serverOutDir!, { recursive: true });
await fs.promises.writeFile(serverPackageJsonPath, serverPackageJsonCode);

const serverPackageJsonPath = join(serverOutDir!, 'package.json');
const serverPackageJsonCode = `{"type":"module"}`;
await fs.promises.mkdir(serverOutDir!, { recursive: true });
await fs.promises.writeFile(serverPackageJsonPath, serverPackageJsonCode);
const azureSwaModulePath = outputEntries.find((entryName) => entryName === 'entry.azure-swa');

const functionJsonPath = join(serverOutDir!, 'function.json');
await fs.promises.writeFile(
functionJsonPath,
`{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "head", "post", "put", "delete", "connect", "options", "trace", "patch"]
const funcJsonPath = join(serverOutDir!, 'function.json');
const funcJson = JSON.stringify(
{
bindings: [
{
authLevel: 'anonymous',
type: 'httpTrigger',
direction: 'in',
name: 'req',
methods: [
'get',
'head',
'post',
'put',
'delete',
'connect',
'options',
'trace',
'patch',
],
},
{
type: 'http',
direction: 'out',
name: 'response',
},
],
scriptFile: azureSwaModulePath,
},
null,
2
);
await fs.promises.writeFile(funcJsonPath, funcJson);
},
{
"type": "http",
"direction": "out",
"name": "response"
}
],
"scriptFile": "${azureSwaModulePath}"
}`
);

if (opts.staticGenerate) {
const staticGenerate = await import('../../../static');
let generateOpts: StaticGenerateOptions = {
outDir: clientOutDir,
origin: process?.env?.URL || 'https://yoursitename.region.2.azurestaticapps.net',
renderModulePath: renderModulePath!,
qwikCityPlanModulePath: qwikCityPlanModulePath!,
};

if (typeof opts.staticGenerate === 'object') {
generateOpts = {
...generateOpts,
...opts.staticGenerate,
};
}

await staticGenerate.generate(generateOpts);
}
}

const plugin: Plugin = {
name: 'vite-plugin-qwik-city-azure-swa',
enforce: 'post',
apply: 'build',

configResolved({ build, plugins }) {
qwikVitePlugin = plugins.find((p) => p.name === 'vite-plugin-qwik') as QwikVitePlugin;
if (!qwikVitePlugin) {
throw new Error('Missing vite-plugin-qwik');
}
serverOutDir = build.outDir;

if (build?.ssr !== true) {
throw new Error(
'"build.ssr" must be set to `true` in order to use the Azure Static Web Apps adaptor.'
);
}

if (!build?.rollupOptions?.input) {
throw new Error(
'"build.rollupOptions.input" must be set in order to use the Azure Static Web Apps adaptor.'
);
}
},

generateBundle(_, bundles) {
for (const fileName in bundles) {
const chunk = bundles[fileName];
if (chunk.type === 'chunk' && chunk.isEntry) {
if (chunk.name === 'entry.ssr') {
renderModulePath = join(serverOutDir!, fileName);
} else if (chunk.name === '@qwik-city-plan') {
qwikCityPlanModulePath = join(serverOutDir!, fileName);
} else if (chunk.name === 'entry.azure-swa') {
azureSwaModulePath = fileName;
}
}
}

if (!renderModulePath) {
throw new Error(
'Unable to fine "entry.ssr" entry point. Did you forget to add it to "build.rollupOptions.input"?'
);
}
if (!qwikCityPlanModulePath) {
throw new Error(
'Unable to fine "@qwik-city-plan" entry point. Did you forget to add it to "build.rollupOptions.input"?'
);
}
},

async closeBundle() {
await generateBundles();
},
};
return plugin;
});
}

/**
* @alpha
*/
export interface AzureSwaAdaptorOptions {
staticGenerate?: StaticGenerateRenderOptions | true;
}
export interface AzureSwaAdaptorOptions extends ServerAdaptorOptions {}
11 changes: 3 additions & 8 deletions packages/qwik-city/adaptors/cloud-run/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { StaticGenerateRenderOptions } from '../../../static';
import { viteAdaptor } from '../../shared/vite';
import { ServerAdaptorOptions, viteAdaptor } from '../../shared/vite';

/**
* @alpha
Expand All @@ -9,6 +8,7 @@ export function cloudRunAdaptor(opts: CloudRunAdaptorOptions = {}): any {
name: 'cloud-run',
origin: process?.env?.URL || 'https://your-app-name.run.app',
staticGenerate: opts.staticGenerate,
ssg: opts.ssg,
cleanStaticGenerated: true,

config() {
Expand All @@ -25,9 +25,4 @@ export function cloudRunAdaptor(opts: CloudRunAdaptorOptions = {}): any {
/**
* @alpha
*/
export interface CloudRunAdaptorOptions {
/**
* Determines if the adaptor should also run Static Site Generation (SSG).
*/
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
}
export interface CloudRunAdaptorOptions extends ServerAdaptorOptions {}
4 changes: 2 additions & 2 deletions packages/qwik-city/adaptors/cloudflare-pages/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
```ts

import { ServerAdaptorOptions } from '../../shared/vite';
import type { StaticGenerateRenderOptions } from '@builder.io/qwik-city/static';

// @alpha (undocumented)
export function cloudflarePagesAdaptor(opts?: CloudflarePagesAdaptorOptions): any;

// @alpha (undocumented)
export interface CloudflarePagesAdaptorOptions {
export interface CloudflarePagesAdaptorOptions extends ServerAdaptorOptions {
functionRoutes?: boolean;
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
staticPaths?: string[];
}

Expand Down
9 changes: 3 additions & 6 deletions packages/qwik-city/adaptors/cloudflare-pages/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StaticGenerateRenderOptions } from '@builder.io/qwik-city/static';
import { viteAdaptor } from '../../shared/vite';
import { ServerAdaptorOptions, viteAdaptor } from '../../shared/vite';
import fs from 'node:fs';
import { join } from 'node:path';

Expand All @@ -11,6 +11,7 @@ export function cloudflarePagesAdaptor(opts: CloudflarePagesAdaptorOptions = {})
name: 'cloudflare-pages',
origin: process?.env?.CF_PAGES_URL || 'https://your.cloudflare.pages.dev',
staticGenerate: opts.staticGenerate,
ssg: opts.ssg,
staticPaths: opts.staticPaths,
cleanStaticGenerated: true,

Expand Down Expand Up @@ -55,7 +56,7 @@ export function cloudflarePagesAdaptor(opts: CloudflarePagesAdaptorOptions = {})
/**
* @alpha
*/
export interface CloudflarePagesAdaptorOptions {
export interface CloudflarePagesAdaptorOptions extends ServerAdaptorOptions {
/**
* Determines if the build should generate the function invocation routes `_routes.json` file.
*
Expand All @@ -64,10 +65,6 @@ export interface CloudflarePagesAdaptorOptions {
* Defaults to `true`.
*/
functionRoutes?: boolean;
/**
* Determines if the adaptor should also run Static Site Generation (SSG).
*/
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
/**
* Manually add pathnames that should be treated as static paths and not SSR.
* For example, when these pathnames are requested, their response should
Expand Down
6 changes: 2 additions & 4 deletions packages/qwik-city/adaptors/express/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
```ts

import type { StaticGenerateRenderOptions } from '../../../static';
import { ServerAdaptorOptions } from '../../shared/vite';

// @alpha (undocumented)
export function expressAdaptor(opts?: ExpressAdaptorOptions): any;

// @alpha (undocumented)
export interface ExpressAdaptorOptions {
// (undocumented)
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
export interface ExpressAdaptorOptions extends ServerAdaptorOptions {
}

// (No @packageDocumentation comment for this package)
Expand Down
8 changes: 3 additions & 5 deletions packages/qwik-city/adaptors/express/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { StaticGenerateRenderOptions } from '../../../static';
import { viteAdaptor } from '../../shared/vite';
import { ServerAdaptorOptions, viteAdaptor } from '../../shared/vite';

/**
* @alpha
Expand All @@ -9,6 +8,7 @@ export function expressAdaptor(opts: ExpressAdaptorOptions = {}): any {
name: 'express',
origin: process?.env?.URL || 'https://yoursitename.qwik.builder.io',
staticGenerate: opts.staticGenerate,
ssg: opts.ssg,
cleanStaticGenerated: true,

config() {
Expand All @@ -25,6 +25,4 @@ export function expressAdaptor(opts: ExpressAdaptorOptions = {}): any {
/**
* @alpha
*/
export interface ExpressAdaptorOptions {
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
}
export interface ExpressAdaptorOptions extends ServerAdaptorOptions {}
4 changes: 2 additions & 2 deletions packages/qwik-city/adaptors/netlify-edge/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
```ts

import { ServerAdaptorOptions } from '../../shared/vite';
import type { StaticGenerateRenderOptions } from '@builder.io/qwik-city/static';

// @alpha (undocumented)
export function netifyEdgeAdaptor(opts?: NetlifyEdgeAdaptorOptions): any;

// @alpha (undocumented)
export interface NetlifyEdgeAdaptorOptions {
export interface NetlifyEdgeAdaptorOptions extends ServerAdaptorOptions {
functionRoutes?: boolean;
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
staticPaths?: string[];
}

Expand Down
9 changes: 3 additions & 6 deletions packages/qwik-city/adaptors/netlify-edge/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StaticGenerateRenderOptions } from '@builder.io/qwik-city/static';
import { getParentDir, viteAdaptor } from '../../shared/vite';
import { getParentDir, ServerAdaptorOptions, viteAdaptor } from '../../shared/vite';
import fs from 'node:fs';
import { join } from 'node:path';
import { basePathname } from '@qwik-city-plan';
Expand All @@ -12,6 +12,7 @@ export function netifyEdgeAdaptor(opts: NetlifyEdgeAdaptorOptions = {}): any {
name: 'netlify-edge',
origin: process?.env?.URL || 'https://yoursitename.netlify.app',
staticGenerate: opts.staticGenerate,
ssg: opts.ssg,
staticPaths: opts.staticPaths,
cleanStaticGenerated: true,

Expand Down Expand Up @@ -62,7 +63,7 @@ export function netifyEdgeAdaptor(opts: NetlifyEdgeAdaptorOptions = {}): any {
/**
* @alpha
*/
export interface NetlifyEdgeAdaptorOptions {
export interface NetlifyEdgeAdaptorOptions extends ServerAdaptorOptions {
/**
* Determines if the build should generate the edge functions declarations `manifest.json` file.
*
Expand All @@ -71,10 +72,6 @@ export interface NetlifyEdgeAdaptorOptions {
* Defaults to `true`.
*/
functionRoutes?: boolean;
/**
* Determines if the adaptor should also run Static Site Generation (SSG).
*/
staticGenerate?: Omit<StaticGenerateRenderOptions, 'outDir'> | true;
/**
* Manually add pathnames that should be treated as static paths and not SSR.
* For example, when these pathnames are requested, their response should
Expand Down
Loading

0 comments on commit b24a65e

Please sign in to comment.