Skip to content

Commit

Permalink
feat(qwik-nx): setup ssg generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandg7 committed Feb 13, 2023
1 parent 2e1f4f6 commit 14a4323
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"private": true,
"devDependencies": {
"@builder.io/qwik": "0.17.4",
"@builder.io/qwik": "0.17.5",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-angular": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/qwik-nx/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"factory": "./src/generators/integrations/cloudflare-pages-integration/generator",
"schema": "./src/generators/integrations/cloudflare-pages-integration/schema.json",
"description": "Qwik City Cloudflare Pages adaptor allows you to connect Qwik City to Cloudflare Pages"
},
"setup-ssg": {
"factory": "./src/generators/setup-ssg/generator",
"schema": "./src/generators/setup-ssg/schema.json",
"description": "Add Static Site Generation (SSG) to a Qwik app"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { staticAdapter } from '@builder.io/qwik-city/adapters/static/vite';
import { extendConfig } from '@builder.io/qwik-city/vite';
import baseConfig from '../../vite.config';

export default extendConfig(baseConfig, () => {
return {
build: {
ssr: true,
rollupOptions: {
input: ['@qwik-city-plan'],
},
},
plugins: [
staticAdapter({
origin: 'https://yoursite.qwik.dev',
}),
],
};
});
20 changes: 20 additions & 0 deletions packages/qwik-nx/src/generators/setup-ssg/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';

import generator from './generator';
import { SetupSsgGeneratorSchema } from './schema';

describe('setup-ssg generator', () => {
let appTree: Tree;
const options: SetupSsgGeneratorSchema = { name: 'test' };

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
});

it('should run successfully', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'test');
expect(config).toBeDefined();
});
});
68 changes: 68 additions & 0 deletions packages/qwik-nx/src/generators/setup-ssg/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
formatFiles,
joinPathFragments,
logger,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { addSsgFiles } from './lib/add-ssg-files';
import { NormalizedSchema, SetupSsgGeneratorSchema } from './schema';

function normalizeOptions(
tree: Tree,
options: SetupSsgGeneratorSchema
): NormalizedSchema {
const project = readProjectConfiguration(tree, options.project);
const projectRoot = project.root;

return {
...options,
projectRoot,
};
}

export async function setupSsgGenerator(
tree: Tree,
options: SetupSsgGeneratorSchema
) {
const normalizedOptions = normalizeOptions(tree, options);

const { projectRoot } = normalizedOptions;

const ssgConfigPath = joinPathFragments(
projectRoot,
'adapters',
'static',
'vite.config.ts'
);

if (tree.exists(ssgConfigPath)) {
logger.info(
`Skipping setup since there is an existing static adapter configuration. For more configuration, see https://qwik.builder.io/qwikcity/guides/static-site-generation/.`
);
return;
}

const projectConfig = readProjectConfiguration(tree, options.project);

addSsgFiles(tree, normalizedOptions);

updateProjectConfiguration(tree, options.project, {
...projectConfig,
targets: {
...projectConfig.targets,
'build-server': {
executor: '@nrwl/vite:build',
options: {
outputPath: 'dist/docs',
configFile: ssgConfigPath,
},
},
},
});

await formatFiles(tree);
}

export default setupSsgGenerator;
17 changes: 17 additions & 0 deletions packages/qwik-nx/src/generators/setup-ssg/lib/add-ssg-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { generateFiles, offsetFromRoot, Tree } from '@nrwl/devkit';
import * as path from 'path';
import { NormalizedSchema } from '../schema';

export function addSsgFiles(tree: Tree, options: NormalizedSchema) {
const templateOptions = {
...options,
offsetFromRoot: offsetFromRoot(options.projectRoot),
template: '',
};
generateFiles(
tree,
path.join(__dirname, '..', 'files'),
options.projectRoot,
templateOptions
);
}
7 changes: 7 additions & 0 deletions packages/qwik-nx/src/generators/setup-ssg/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface SetupSsgGeneratorSchema {
project: string;
}

interface NormalizedSchema extends SetupSsgGeneratorSchema {
projectRoot: string;
}
22 changes: 22 additions & 0 deletions packages/qwik-nx/src/generators/setup-ssg/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "SetupSsg",
"title": "",
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "The name of the project to add the SSG setup for.",
"alias": "p",
"$default": {
"$source": "argv",
"index": 0
},
"x-dropdown": "projects",
"x-prompt": "What project would you like to add the SSG setup?"
}
},
"additionalProperties": false,
"required": ["project"]
}
2 changes: 2 additions & 0 deletions packages/qwik-nx/src/utils/add-common-qwik-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
nodeFetchVersion,
qwikCityVersion,
qwikVersion,
undiciVersion,
viteTsconfigPathsVersion,
viteVersion,
} from './versions';
Expand All @@ -21,6 +22,7 @@ export function addCommonQwikDependencies(host: Tree): GeneratorCallback {
vite: viteVersion,
'vite-tsconfig-paths': viteTsconfigPathsVersion,
'node-fetch': nodeFetchVersion,
undici: undiciVersion,
// TODO: dependencies below should be setup correctly by Nx's generator, so not needed to provide them here?
// "@types/eslint": typesEslint,
// '@types/node': 'latest',
Expand Down
7 changes: 5 additions & 2 deletions packages/qwik-nx/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// qwik packages
export const qwikVersion = '0.16.2';
export const qwikCityVersion = '0.0.128';
export const qwikVersion = '0.17.5';
export const qwikCityVersion = '0.1.0';
export const qwikEslintPluginVersion = '0.16.2';

// qwik dependencies
export const undiciVersion = '5.18.0';

// css preprocessors
export const sassVersion = '1.56.1';
export const lessVersion = '4.1.3';
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 14a4323

Please sign in to comment.