forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ssg filter option (QwikDev#2554)
- Loading branch information
1 parent
3ce3ba0
commit b24a65e
Showing
20 changed files
with
288 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.