Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(presets): new preset nuxt #397

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/presets/nuxt/azion.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { defineConfig } from 'azion';

export default defineConfig({
build: {
preset: {
name: 'nuxt',
},
},
origin: [
{
name: 'origin-storage-default',
type: 'object_storage',
},
],
rules: {
request: [
{
name: 'Set Storage Origin for All Requests',
match: '^\\/',
behavior: {
setOrigin: {
name: 'origin-storage-default',
type: 'object_storage',
},
},
},
{
name: 'Deliver Static Assets',
match:
'.(css|js|ttf|woff|woff2|pdf|svg|jpg|jpeg|gif|bmp|png|ico|mp4|json|xml|html)$',
behavior: {
setOrigin: {
name: 'origin-storage-default',
type: 'object_storage',
},
deliver: true,
},
},
{
name: 'Redirect to index.html',
match: '.*/$',
behavior: {
// eslint-disable-next-line no-template-curly-in-string
rewrite: '${uri}index.html',
},
},
{
name: 'Redirect to index.html for Subpaths',
match: '^(?!.*\\/$)(?![\\s\\S]*\\.[a-zA-Z0-9]+$).*',
behavior: {
// eslint-disable-next-line no-template-curly-in-string
rewrite: '${uri}/index.html',
},
},
],
},
});
10 changes: 10 additions & 0 deletions lib/presets/nuxt/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Config to be used in build context.
*/
const config = {
builder: 'webpack',
polyfills: false,
custom: {},
};

export default config;
17 changes: 17 additions & 0 deletions lib/presets/nuxt/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { mountMPA } from 'azion/utils';

/**
* Handles the 'fetch' event.
* @param {import('azion/types').FetchEvent} event - The fetch event.
* @returns {Promise<Response>} The response for the request.
*/
async function handler(event) {
try {
const myApp = await mountMPA(event.request.url);
return myApp;
} catch (e) {
return new Response('Not Found', { status: 404 });
}
}

export default handler;
19 changes: 19 additions & 0 deletions lib/presets/nuxt/prebuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { rm } from 'fs/promises';
import { exec, copyDirectory } from '#utils';

/**
* Runs custom prebuild actions
*/
async function prebuild() {
const newOutDir = '.edge/storage';
const outDir = '.output/public';

await exec('npx nuxt generate', true);

// move files to vulcan default path
copyDirectory(outDir, newOutDir);

rm(outDir, { recursive: true, force: true });
}

export default prebuild;
1 change: 1 addition & 0 deletions lib/utils/presets/presets.utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('getPresetsList utils', () => {
'javascript',
'jekyll',
'next',
'nuxt',
'react',
'rustwasm',
'svelte',
Expand Down
54 changes: 54 additions & 0 deletions tests/e2e/nuxt-static.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable jest/expect-expect */
import supertest from 'supertest';
import puppeteer from 'puppeteer';
import projectInitializer from '../utils/project-initializer.js';
import projectStop from '../utils/project-stop.js';
import { getContainerPort } from '../utils/docker-env-actions.js';

// timeout in minutes
const TIMEOUT = 10 * 60 * 1000;

let serverPort;
let localhostBaseUrl;
const EXAMPLE_PATH = '/examples/nuxt-static';

describe('E2E - nuxt-static project', () => {
let request;
let browser;
let page;

beforeAll(async () => {
serverPort = getContainerPort();
localhostBaseUrl = `http://0.0.0.0:${serverPort}`;

request = supertest(localhostBaseUrl);

await projectInitializer(EXAMPLE_PATH, 'nuxt', serverPort);

browser = await puppeteer.launch({ headless: 'new' });
page = await browser.newPage();
}, TIMEOUT);

afterAll(async () => {
await projectStop(serverPort, EXAMPLE_PATH.replace('/examples/', ''));

await browser.close();
}, TIMEOUT);

test('Should render home page in "/" route', async () => {
await page.goto(`${localhostBaseUrl}/`);

const pageContent = await page.content();
const pageTitle = await page.title();

expect(pageContent).toContain('Get started');
expect(pageTitle).toBe('Welcome to Nuxt!');
});

test('Should return correct asset', async () => {
await request
.get('/favicon.ico')
.expect(200)
.expect('Content-Type', /image/);
});
});