Skip to content

Commit

Permalink
fix: use clientOutDir if provided for qwik-insights consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
intellix committed Oct 30, 2023
1 parent f1b69ec commit 5cda4c0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik-optimizer/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
}
],
"kind": "Interface",
"content": "```typescript\nexport interface QwikVitePluginApi \n```\n\n\n| Property | Modifiers | Type | Description |\n| --- | --- | --- | --- |\n| [getClientOutDir](#) | | () => string \\| null | |\n| [getClientPublicOutDir](#) | | () => string \\| null | |\n| [getInsightsManifest](#) | | () => Promise<[InsightManifest](#insightmanifest) \\| null> | |\n| [getManifest](#) | | () => [QwikManifest](#qwikmanifest) \\| null | |\n| [getOptimizer](#) | | () => [Optimizer](#optimizer) \\| null | |\n| [getOptions](#) | | () => NormalizedQwikPluginOptions | |\n| [getRootDir](#) | | () => string \\| null | |",
"content": "```typescript\nexport interface QwikVitePluginApi \n```\n\n\n| Property | Modifiers | Type | Description |\n| --- | --- | --- | --- |\n| [getClientOutDir](#) | | () => string \\| null | |\n| [getClientPublicOutDir](#) | | () => string \\| null | |\n| [getInsightsManifest](#) | | (clientOutDir?: string \\| null) => Promise<[InsightManifest](#insightmanifest) \\| null> | |\n| [getManifest](#) | | () => [QwikManifest](#qwikmanifest) \\| null | |\n| [getOptimizer](#) | | () => [Optimizer](#optimizer) \\| null | |\n| [getOptions](#) | | () => NormalizedQwikPluginOptions | |\n| [getRootDir](#) | | () => string \\| null | |",
"editUrl": "https://github.com/BuilderIO/qwik/tree/main/packages/qwik/src/optimizer/src/plugins/vite.ts",
"mdFile": "qwik.qwikvitepluginapi.md"
},
Expand Down
18 changes: 9 additions & 9 deletions packages/docs/src/routes/api/qwik-optimizer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,15 @@ export interface QwikVitePlugin
export interface QwikVitePluginApi
```

| Property | Modifiers | Type | Description |
| -------------------------- | --------- | ------------------------------------------------------------------- | ----------- |
| [getClientOutDir](#) | | () => string \| null | |
| [getClientPublicOutDir](#) | | () => string \| null | |
| [getInsightsManifest](#) | | () => Promise<[InsightManifest](#insightmanifest) \| null> | |
| [getManifest](#) | | () => [QwikManifest](#qwikmanifest) \| null | |
| [getOptimizer](#) | | () => [Optimizer](#optimizer) \| null | |
| [getOptions](#) | | () => NormalizedQwikPluginOptions | |
| [getRootDir](#) | | () => string \| null | |
| Property | Modifiers | Type | Description |
| -------------------------- | --------- | ------------------------------------------------------------------------------------------------ | ----------- |
| [getClientOutDir](#) | | () => string \| null | |
| [getClientPublicOutDir](#) | | () => string \| null | |
| [getInsightsManifest](#) | | (clientOutDir?: string \| null) => Promise<[InsightManifest](#insightmanifest) \| null> | |
| [getManifest](#) | | () => [QwikManifest](#qwikmanifest) \| null | |
| [getOptimizer](#) | | () => [Optimizer](#optimizer) \| null | |
| [getOptions](#) | | () => NormalizedQwikPluginOptions | |
| [getRootDir](#) | | () => string \| null | |

[Edit this section](https://github.com/BuilderIO/qwik/tree/main/packages/qwik/src/optimizer/src/plugins/vite.ts)

Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/buildtime/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ function qwikCityPlugin(userOpts?: QwikCityVitePluginOptions): any {
if (ctx?.target === 'ssr') {
// ssr build
const manifest = qwikPlugin!.api.getManifest();
const insightsManifest = await qwikPlugin!.api.getInsightsManifest();
const clientOutDir = qwikPlugin!.api.getClientOutDir();
const insightsManifest = await qwikPlugin!.api.getInsightsManifest(clientOutDir);

if (manifest && clientOutDir) {
const basePathRelDir = api.getBasePathname().replace(/^\/|\/$/, '');
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-labs/src-vite/insights/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function qwikInsights(qwikInsightsOpts: {
logWarn('fail to fetch manifest from Insights DB');
}
if (!existsSync(join(process.cwd(), outDir))) {
mkdirSync(join(process.cwd(), outDir));
mkdirSync(join(process.cwd(), outDir), { recursive: true });
}
await writeFile(join(process.cwd(), outDir, 'q-insights.json'), JSON.stringify(qManifest));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/optimizer/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export interface QwikVitePluginApi {
// (undocumented)
getClientPublicOutDir: () => string | null;
// (undocumented)
getInsightsManifest: () => Promise<InsightManifest | null>;
getInsightsManifest: (clientOutDir?: string | null) => Promise<InsightManifest | null>;
// (undocumented)
getManifest: () => QwikManifest | null;
// (undocumented)
Expand Down
12 changes: 7 additions & 5 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
const injections: GlobalInjections[] = [];
const qwikPlugin = createPlugin(qwikViteOpts.optimizerOptions);

async function loadQwikInsights(): Promise<InsightManifest | null> {
async function loadQwikInsights(clientOutDir?: string | null): Promise<InsightManifest | null> {
const sys = qwikPlugin.getSys();
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
const path = sys.path.join(process.cwd(), 'dist', 'q-insights.json');
const path = sys.path.join(process.cwd(), clientOutDir ?? 'dist', 'q-insights.json');
if (fs.existsSync(path)) {
return JSON.parse(await fs.promises.readFile(path, 'utf-8')) as InsightManifest;
}
Expand All @@ -70,7 +70,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
getOptimizer: () => qwikPlugin.getOptimizer(),
getOptions: () => qwikPlugin.getOptions(),
getManifest: () => manifestInput,
getInsightsManifest: () => loadQwikInsights(),
getInsightsManifest: (clientOutDir?: string | null) => loadQwikInsights(clientOutDir),
getRootDir: () => qwikPlugin.getOptions().rootDir,
getClientOutDir: () => clientOutDir,
getClientPublicOutDir: () => clientPublicOutDir,
Expand Down Expand Up @@ -118,7 +118,9 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {

if (sys.env === 'node' && !qwikViteOpts.entryStrategy) {
try {
const entryStrategy = await loadQwikInsights();
const entryStrategy = await loadQwikInsights(
!qwikViteOpts.csr ? qwikViteOpts.client?.outDir : undefined
);
if (entryStrategy) {
qwikViteOpts.entryStrategy = entryStrategy;
}
Expand Down Expand Up @@ -871,7 +873,7 @@ export interface QwikVitePluginApi {
getOptimizer: () => Optimizer | null;
getOptions: () => NormalizedQwikPluginOptions;
getManifest: () => QwikManifest | null;
getInsightsManifest: () => Promise<InsightManifest | null>;
getInsightsManifest: (clientOutDir?: string | null) => Promise<InsightManifest | null>;
getRootDir: () => string | null;
getClientOutDir: () => string | null;
getClientPublicOutDir: () => string | null;
Expand Down

0 comments on commit 5cda4c0

Please sign in to comment.