Skip to content

Commit 14a4323

Browse files
committed
feat(qwik-nx): setup ssg generator
1 parent 2e1f4f6 commit 14a4323

File tree

11 files changed

+170
-7
lines changed

11 files changed

+170
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"private": true,
1414
"devDependencies": {
15-
"@builder.io/qwik": "0.17.4",
15+
"@builder.io/qwik": "0.17.5",
1616
"@commitlint/cli": "^17.3.0",
1717
"@commitlint/config-angular": "^17.3.0",
1818
"@commitlint/config-conventional": "^17.3.0",

packages/qwik-nx/generators.json

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"factory": "./src/generators/integrations/cloudflare-pages-integration/generator",
5555
"schema": "./src/generators/integrations/cloudflare-pages-integration/schema.json",
5656
"description": "Qwik City Cloudflare Pages adaptor allows you to connect Qwik City to Cloudflare Pages"
57+
},
58+
"setup-ssg": {
59+
"factory": "./src/generators/setup-ssg/generator",
60+
"schema": "./src/generators/setup-ssg/schema.json",
61+
"description": "Add Static Site Generation (SSG) to a Qwik app"
5762
}
5863
}
5964
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { staticAdapter } from '@builder.io/qwik-city/adapters/static/vite';
2+
import { extendConfig } from '@builder.io/qwik-city/vite';
3+
import baseConfig from '../../vite.config';
4+
5+
export default extendConfig(baseConfig, () => {
6+
return {
7+
build: {
8+
ssr: true,
9+
rollupOptions: {
10+
input: ['@qwik-city-plan'],
11+
},
12+
},
13+
plugins: [
14+
staticAdapter({
15+
origin: 'https://yoursite.qwik.dev',
16+
}),
17+
],
18+
};
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
2+
import { Tree, readProjectConfiguration } from '@nrwl/devkit';
3+
4+
import generator from './generator';
5+
import { SetupSsgGeneratorSchema } from './schema';
6+
7+
describe('setup-ssg generator', () => {
8+
let appTree: Tree;
9+
const options: SetupSsgGeneratorSchema = { name: 'test' };
10+
11+
beforeEach(() => {
12+
appTree = createTreeWithEmptyWorkspace();
13+
});
14+
15+
it('should run successfully', async () => {
16+
await generator(appTree, options);
17+
const config = readProjectConfiguration(appTree, 'test');
18+
expect(config).toBeDefined();
19+
});
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
formatFiles,
3+
joinPathFragments,
4+
logger,
5+
readProjectConfiguration,
6+
Tree,
7+
updateProjectConfiguration,
8+
} from '@nrwl/devkit';
9+
import { addSsgFiles } from './lib/add-ssg-files';
10+
import { NormalizedSchema, SetupSsgGeneratorSchema } from './schema';
11+
12+
function normalizeOptions(
13+
tree: Tree,
14+
options: SetupSsgGeneratorSchema
15+
): NormalizedSchema {
16+
const project = readProjectConfiguration(tree, options.project);
17+
const projectRoot = project.root;
18+
19+
return {
20+
...options,
21+
projectRoot,
22+
};
23+
}
24+
25+
export async function setupSsgGenerator(
26+
tree: Tree,
27+
options: SetupSsgGeneratorSchema
28+
) {
29+
const normalizedOptions = normalizeOptions(tree, options);
30+
31+
const { projectRoot } = normalizedOptions;
32+
33+
const ssgConfigPath = joinPathFragments(
34+
projectRoot,
35+
'adapters',
36+
'static',
37+
'vite.config.ts'
38+
);
39+
40+
if (tree.exists(ssgConfigPath)) {
41+
logger.info(
42+
`Skipping setup since there is an existing static adapter configuration. For more configuration, see https://qwik.builder.io/qwikcity/guides/static-site-generation/.`
43+
);
44+
return;
45+
}
46+
47+
const projectConfig = readProjectConfiguration(tree, options.project);
48+
49+
addSsgFiles(tree, normalizedOptions);
50+
51+
updateProjectConfiguration(tree, options.project, {
52+
...projectConfig,
53+
targets: {
54+
...projectConfig.targets,
55+
'build-server': {
56+
executor: '@nrwl/vite:build',
57+
options: {
58+
outputPath: 'dist/docs',
59+
configFile: ssgConfigPath,
60+
},
61+
},
62+
},
63+
});
64+
65+
await formatFiles(tree);
66+
}
67+
68+
export default setupSsgGenerator;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { generateFiles, offsetFromRoot, Tree } from '@nrwl/devkit';
2+
import * as path from 'path';
3+
import { NormalizedSchema } from '../schema';
4+
5+
export function addSsgFiles(tree: Tree, options: NormalizedSchema) {
6+
const templateOptions = {
7+
...options,
8+
offsetFromRoot: offsetFromRoot(options.projectRoot),
9+
template: '',
10+
};
11+
generateFiles(
12+
tree,
13+
path.join(__dirname, '..', 'files'),
14+
options.projectRoot,
15+
templateOptions
16+
);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface SetupSsgGeneratorSchema {
2+
project: string;
3+
}
4+
5+
interface NormalizedSchema extends SetupSsgGeneratorSchema {
6+
projectRoot: string;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"cli": "nx",
4+
"$id": "SetupSsg",
5+
"title": "",
6+
"type": "object",
7+
"properties": {
8+
"project": {
9+
"type": "string",
10+
"description": "The name of the project to add the SSG setup for.",
11+
"alias": "p",
12+
"$default": {
13+
"$source": "argv",
14+
"index": 0
15+
},
16+
"x-dropdown": "projects",
17+
"x-prompt": "What project would you like to add the SSG setup?"
18+
}
19+
},
20+
"additionalProperties": false,
21+
"required": ["project"]
22+
}

packages/qwik-nx/src/utils/add-common-qwik-dependencies.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
nodeFetchVersion,
88
qwikCityVersion,
99
qwikVersion,
10+
undiciVersion,
1011
viteTsconfigPathsVersion,
1112
viteVersion,
1213
} from './versions';
@@ -21,6 +22,7 @@ export function addCommonQwikDependencies(host: Tree): GeneratorCallback {
2122
vite: viteVersion,
2223
'vite-tsconfig-paths': viteTsconfigPathsVersion,
2324
'node-fetch': nodeFetchVersion,
25+
undici: undiciVersion,
2426
// TODO: dependencies below should be setup correctly by Nx's generator, so not needed to provide them here?
2527
// "@types/eslint": typesEslint,
2628
// '@types/node': 'latest',

packages/qwik-nx/src/utils/versions.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// qwik packages
2-
export const qwikVersion = '0.16.2';
3-
export const qwikCityVersion = '0.0.128';
2+
export const qwikVersion = '0.17.5';
3+
export const qwikCityVersion = '0.1.0';
44
export const qwikEslintPluginVersion = '0.16.2';
55

6+
// qwik dependencies
7+
export const undiciVersion = '5.18.0';
8+
69
// css preprocessors
710
export const sassVersion = '1.56.1';
811
export const lessVersion = '4.1.3';

pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)